changeset 599:f71cf1ad745f rust

updated toml serde works OK
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Feb 2017 22:35:29 +0800
parents d4fbfb5c46ff
children 9c76f3cf01ea
files rust/src/config.rs rust/src/main.rs
diffstat 2 files changed, 29 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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<Self, String>{
+        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<Self, String>{
--- 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);
 }