Mercurial > templog
diff rust/src/config.rs @ 598:d4fbfb5c46ff rust
broken update of versions of things
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 07 Feb 2017 21:56:58 +0800 |
parents | a440eafa84a9 |
children | f71cf1ad745f |
line wrap: on
line diff
--- a/rust/src/config.rs Sat Jan 07 00:56:39 2017 +0800 +++ b/rust/src/config.rs Tue Feb 07 21:56:58 2017 +0800 @@ -1,6 +1,8 @@ extern crate toml; -use serde::Serialize; +use std::fs::File; +use std::io::Read; +use serde::{Serialize,Deserialize,Deserializer,Serializer}; #[derive(Deserialize,Serialize,Debug,Clone)] #[allow(non_snake_case)] @@ -31,7 +33,7 @@ impl Config { - pub fn new() -> Self { + pub fn default() -> Self { Config { SENSOR_SLEEP: 5, UPLOAD_SLEEP: 83, @@ -68,34 +70,40 @@ toml::Value::Table(e.toml).to_string() } - /* - pub fn parse(&mut self, s: &str) { - let filename = "tempserver.conf"; - let f = File::open(filename) + pub fn merge(&self, conf: &str) -> Result<Self, String>{ + let mut p = toml::Parser::new(&conf); + 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? + }) + } + + 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) - }) - .and_then(|s| { - - .map_err(|e| e.to_string()) }); - match f { - Ok() => { - - }, - Err(e) => { - debug!("Error loading config file {}: {}", - filename, e); - - } - } - + r.and_then(|s| { + self.merge(&s) + }) + .map_err(|e| { + format!("Error loading config file {}: {}", + filename, e) + }) } - */ }