# HG changeset patch # User Matt Johnston # Date 1486478129 -28800 # Node ID f71cf1ad745f47a4e53e88ce232dcbff1b5ef707 # Parent d4fbfb5c46ff8c4754e97d2987afde74bc1335b6 updated toml serde works OK diff -r d4fbfb5c46ff -r f71cf1ad745f rust/src/config.rs --- a/rust/src/config.rs Tue Feb 07 21:56:58 2017 +0800 +++ b/rust/src/config.rs Tue Feb 07 22:35:29 2017 +0800 @@ -65,14 +65,23 @@ } pub fn to_toml_string(&self) -> String { - let mut e = toml::Encoder::new(); - self.serialize(&mut e).unwrap(); - toml::Value::Table(e.toml).to_string() + toml::to_string(self).unwrap() } pub fn merge(&self, conf: &str) -> Result{ + println!("config {}", conf); + let mut new_toml = try!(toml::from_str(conf).map_err(|e| e.to_string())); + let mut ex_val = toml::Value::try_from(self).unwrap(); + let mut ex_toml = ex_val.as_table_mut().unwrap(); + ex_toml.append(&mut new_toml); + let ret: Self = toml::Value::Table(ex_toml.clone()).try_into().unwrap(); + return Ok(ret) + + + /* let mut p = toml::Parser::new(&conf); + let p = p.parse() .ok_or_else(|| { format!("toml parsing failed: {:?}", p.errors) @@ -85,6 +94,7 @@ let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml)); Self::deserialize(&mut dec).unwrap() // could this go wrong? }) + */ } pub fn merge_file(&self, filename: &str) -> Result{ diff -r d4fbfb5c46ff -r f71cf1ad745f rust/src/main.rs --- a/rust/src/main.rs Tue Feb 07 21:56:58 2017 +0800 +++ b/rust/src/main.rs Tue Feb 07 22:35:29 2017 +0800 @@ -95,13 +95,13 @@ #[derive(RustcDecodable)] struct Args { - new: bool, - daemon: bool, - debug: bool, - test: bool, - defconf: bool, - thisconf: bool, - nowait: bool, + flag_new: bool, + flag_daemon: bool, + flag_debug: bool, + flag_test: bool, + flag_defconf: bool, + flag_thisconf: bool, + flag_nowait: bool, } fn setup_log(debug: bool) { @@ -126,36 +126,37 @@ fn handle_args() -> Args { let args: Args = docopt::Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); - if args.defconf { + if args.flag_defconf { println!("Default configuration:\n{}\n\n{}", "(custom options go in tempserver.conf)", - config::Config::new().to_toml_string()); + config::Config::default().to_toml_string()); std::process::exit(0); } + args } fn main() { - let mut config = config::Config::new(); + let mut config = config::Config::default(); let args = handle_args(); - setup_log(args.debug); + setup_log(args.flag_debug); //env_logger::init().unwrap(); info!("wort-templog"); debug!("debug mode"); let conf_filename = "tempserver.conf"; - println!("parse config"); - config.parse(conf_filename) + config = config.merge_file(conf_filename) .unwrap_or_else(|e| { panic!("Couldn't parse {}: {}", conf_filename, e); }); - if args.thisconf { + if args.flag_thisconf { println!("Current configuration:\n\n{}", config.to_toml_string()); + std::process::exit(0); } - run(&config, args.nowait, args.test); + run(&config, args.flag_nowait, args.flag_test); }