comparison rust/src/config.rs @ 611:f3e39e2107fd rust

still doesn't compile, improvements to TemplogError and tokio curl though
author Matt Johnston <matt@ucc.asn.au>
date Tue, 28 Feb 2017 22:58:47 +0800
parents 8c21df3711e2
children a5721c02d3ee
comparison
equal deleted inserted replaced
610:af0dac00d40b 611:f3e39e2107fd
2 2
3 use std::error::Error; 3 use std::error::Error;
4 use std::fs::File; 4 use std::fs::File;
5 use std::io::Read; 5 use std::io::Read;
6 use serde::{Serialize,Deserialize,Deserializer,Serializer}; 6 use serde::{Serialize,Deserialize,Deserializer,Serializer};
7
8 use types::*;
7 9
8 #[derive(Deserialize,Serialize,Debug,Clone)] 10 #[derive(Deserialize,Serialize,Debug,Clone)]
9 #[allow(non_snake_case)] 11 #[allow(non_snake_case)]
10 pub struct Config { 12 pub struct Config {
11 pub SENSOR_SLEEP: u64, 13 pub SENSOR_SLEEP: u64,
67 69
68 pub fn to_toml_string(&self) -> String { 70 pub fn to_toml_string(&self) -> String {
69 toml::to_string(self).unwrap() 71 toml::to_string(self).unwrap()
70 } 72 }
71 73
72 pub fn merge(&self, conf: &str) -> Result<Self, Box<Error>> { 74 pub fn merge(&self, conf: &str) -> Result<Self, TemplogError> {
73 // convert existing and new toml into tables, combine them. 75 // convert existing and new toml into tables, combine them.
74 let mut new_toml = toml::from_str(conf)?; 76 let mut new_toml = toml::from_str(conf)?;
75 let mut ex_val = toml::Value::try_from(self).unwrap(); 77 let mut ex_val = toml::Value::try_from(self).unwrap();
76 let mut ex_toml = ex_val.as_table_mut().unwrap(); 78 let mut ex_toml = ex_val.as_table_mut().unwrap();
77 ex_toml.append(&mut new_toml); 79 ex_toml.append(&mut new_toml);
78 // TODO: wrap the error with a better message? 80 // TODO: wrap the error with a better message?
79 let ret = toml::Value::Table(ex_toml.clone()).try_into()?; 81 let ret = toml::Value::Table(ex_toml.clone()).try_into()?;
80 Ok(ret) 82 Ok(ret)
81 } 83 }
82 84
83 pub fn merge_file(&self, filename: &str) -> Result<Self, Box<Error>> { 85 pub fn merge_file(&self, filename: &str) -> Result<Self, TemplogError> {
84 86
85 let mut s = String::new(); 87 let mut s = String::new();
86 File::open(filename)?.read_to_string(&mut s)?; 88 File::open(filename)?.read_to_string(&mut s)?;
87 89
88 self.merge(&s) 90 self.merge(&s)