comparison rust/src/config.rs @ 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
comparison
equal deleted inserted replaced
598:d4fbfb5c46ff 599:f71cf1ad745f
63 SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(), 63 SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(),
64 } 64 }
65 } 65 }
66 66
67 pub fn to_toml_string(&self) -> String { 67 pub fn to_toml_string(&self) -> String {
68 let mut e = toml::Encoder::new(); 68 toml::to_string(self).unwrap()
69 self.serialize(&mut e).unwrap();
70 toml::Value::Table(e.toml).to_string()
71 } 69 }
72 70
73 71
74 pub fn merge(&self, conf: &str) -> Result<Self, String>{ 72 pub fn merge(&self, conf: &str) -> Result<Self, String>{
73 println!("config {}", conf);
74 let mut new_toml = try!(toml::from_str(conf).map_err(|e| e.to_string()));
75 let mut ex_val = toml::Value::try_from(self).unwrap();
76 let mut ex_toml = ex_val.as_table_mut().unwrap();
77 ex_toml.append(&mut new_toml);
78 let ret: Self = toml::Value::Table(ex_toml.clone()).try_into().unwrap();
79 return Ok(ret)
80
81
82 /*
75 let mut p = toml::Parser::new(&conf); 83 let mut p = toml::Parser::new(&conf);
84 let p =
76 p.parse() 85 p.parse()
77 .ok_or_else(|| { 86 .ok_or_else(|| {
78 format!("toml parsing failed: {:?}", p.errors) 87 format!("toml parsing failed: {:?}", p.errors)
79 }) 88 })
80 .map(|mut new_toml| { 89 .map(|mut new_toml| {
83 let mut ex_toml = e.toml; 92 let mut ex_toml = e.toml;
84 ex_toml.append(&mut new_toml); 93 ex_toml.append(&mut new_toml);
85 let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml)); 94 let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml));
86 Self::deserialize(&mut dec).unwrap() // could this go wrong? 95 Self::deserialize(&mut dec).unwrap() // could this go wrong?
87 }) 96 })
97 */
88 } 98 }
89 99
90 pub fn merge_file(&self, filename: &str) -> Result<Self, String>{ 100 pub fn merge_file(&self, filename: &str) -> Result<Self, String>{
91 101
92 let r = File::open(filename) 102 let r = File::open(filename)