Mercurial > templog
diff rust/src/config.rs @ 600:9c76f3cf01ea rust
slightly less ugly
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 07 Feb 2017 22:57:29 +0800 |
parents | f71cf1ad745f |
children | 8c21df3711e2 |
line wrap: on
line diff
--- 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<Self, String>{ - 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<Self, String>{ 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)