# HG changeset patch # User Matt Johnston # Date 1486479449 -28800 # Node ID 9c76f3cf01ea5207f2369466d1b54eab97749e33 # Parent f71cf1ad745f47a4e53e88ce232dcbff1b5ef707 slightly less ugly diff -r f71cf1ad745f -r 9c76f3cf01ea rust/src/config.rs --- a/rust/src/config.rs Tue Feb 07 22:35:29 2017 +0800 +++ b/rust/src/config.rs Tue Feb 07 22:57:29 2017 +0800 @@ -68,45 +68,25 @@ toml::to_string(self).unwrap() } - pub fn merge(&self, conf: &str) -> Result{ - println!("config {}", conf); + // convert existing and new toml into tables, combine them. 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) - }) - .map(|mut new_toml| { - let mut e = toml::Encoder::new(); - self.serialize(&mut e).unwrap(); // XXX what could go wrong? - let mut ex_toml = e.toml; - ex_toml.append(&mut new_toml); - let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml)); - Self::deserialize(&mut dec).unwrap() // could this go wrong? - }) - */ + Ok(ret) } pub fn merge_file(&self, filename: &str) -> Result{ let r = File::open(filename) - .map_err(|e| e.to_string()) .and_then(|mut f| { let mut s = String::new(); f.read_to_string(&mut s) - .map_err(|e| e.to_string()) .map(|_| s) - }); + }) + .map_err(|e| e.to_string()); r.and_then(|s| { self.merge(&s)