Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to tomllib #4

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 152 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ version = "0.0.1"
[dependencies]
clap = "~1.5.4"
semver = "0.2.0"
toml = "0.1"
tomllib = "0.1.2"
66 changes: 32 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
extern crate clap;
extern crate semver;
extern crate toml;
extern crate tomllib;

mod config;
mod version;

use std::fs::OpenOptions;
use std::io::Write;
use toml::Value;
use std::path::Path;
use std::io::{Read, Write};
use std::fs::{File, OpenOptions};

use semver::Version;
use tomllib::TOMLParser;
use tomllib::types::{Value, ParseResult};

fn main() {
let conf = config::get_config();
let (mut v, mut t) = version::get_current_version(&conf.manifest);
let old_version = v.clone();
version::update_version(&mut v, conf.version);
println!("Version {} -> {}", old_version, v);

match t {
Value::Table(ref mut top) => {
match top.get_mut("package").unwrap() {
&mut Value::Table(ref mut package) => {
let version = package.entry("version".to_owned())
.or_insert_with(|| panic!("missing package"));
*version = toml::Value::String(v.to_string());
}
_ => panic!("package not a table")
}
}
_ => panic!("top not a table")
let raw_data = read_file(&conf.manifest);
let parser = TOMLParser::new();
let (mut parser, result) = parser.parse(&raw_data);
match result {
ParseResult::Full => {},
_ => panic!("couldn't parse Cargo.toml"),
}

let new_manifest = build_new_manifest(t).unwrap();
let new_manifest_bytes = new_manifest.as_bytes();
let raw_value = parser.get_value("package.version").expect("package.version missing");
let mut version = match raw_value {
Value::String(raw_version, _) => Version::parse(&raw_version).unwrap(),
_ => panic!("version not a string"),
};

let old_version = version.clone();
version::update_version(&mut version, conf.version);
println!("Version {} -> {}", old_version, version);

parser.set_value("package.version", Value::basic_string(version.to_string()).unwrap());

let mut f = OpenOptions::new()
.write(true)
.truncate(true)
.open(&conf.manifest)
.unwrap();
f.write_all(new_manifest_bytes).unwrap();
f.write_all(format!("{}", parser).as_bytes()).unwrap();
}

fn build_new_manifest(toml: Value) -> Option<String> {
match toml {
Value::Table(mut data) => {
let package_value = data.remove("package").unwrap();
Some(format!("[package]\n{}{}",
package_value,
toml::Value::Table(data)))
}
_ => None
}
fn read_file(file: &Path) -> String {
let mut file = File::open(file).unwrap();
let mut raw_data = String::new();
file.read_to_string(&mut raw_data).unwrap();
raw_data
}
21 changes: 1 addition & 20 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
use config::NewVersion;
use semver::Version;
use std::fs;
use std::io::Read;
use std::path::Path;
use toml::{Parser, Value};

pub fn get_current_version(file: &Path) -> (Version, Value) {
let mut file = fs::File::open(file).unwrap();
let mut raw_data = String::new();
file.read_to_string(&mut raw_data).unwrap();
let mut parser = Parser::new(&raw_data);
let parsed_data = Value::Table(parser.parse()
.unwrap_or_else(|| panic!("couldn't parse Cargo.toml, {:?}", parser.errors)));
let raw_version = parsed_data.lookup("package.version")
.unwrap_or_else(|| panic!("package.version missing"))
.as_str()
.unwrap_or_else(|| panic!("version not a string"));

(Version::parse(raw_version).unwrap(), parsed_data.clone())
}
use config::NewVersion;

pub fn update_version(old: &mut Version, by: NewVersion) {
match by {
Expand Down