Mercurial > templog
diff rust/src/config.rs @ 634:a5721c02d3ee rust
build succeeds
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 22 Sep 2019 20:35:40 +0800 |
parents | f3e39e2107fd |
children | 4424a8b30f9c |
line wrap: on
line diff
--- a/rust/src/config.rs Wed Sep 04 23:24:13 2019 +0800 +++ b/rust/src/config.rs Sun Sep 22 20:35:40 2019 +0800 @@ -1,11 +1,6 @@ -extern crate toml; +use serde::{Serialize,Deserialize}; -use std::error::Error; -use std::fs::File; -use std::io::Read; -use serde::{Serialize,Deserialize,Deserializer,Serializer}; - -use types::*; +use super::types::*; #[derive(Deserialize,Serialize,Debug,Clone)] #[allow(non_snake_case)] @@ -35,58 +30,15 @@ } impl Config { - - pub fn default() -> Self { - Config { - SENSOR_SLEEP: 5, - UPLOAD_SLEEP: 83, - - FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on - FRIDGE_WORT_INVALID_TIME: 300, // 5 mins - - // 12 hours of "offline" readings stored - MAX_READINGS: 12*60*60 / 15, // 15 is SENSOR_SLEEP - - //PARAMS_FILE: os.path.join(os.path.dirname(__file__), "tempserver.conf") - PARAMS_FILE: "tempserver.conf".to_string(), - - SENSOR_BASE_DIR: "/sys/devices/w1_bus_master2".to_string(), - FRIDGE_GPIO_PIN: 17, - //WORT_NAME: "28-0000042cf4dd".to_string(), - //FRIDGE_NAME: "28-0000042cccc4".to_string(), - //AMBIENT_NAME: "28-0000042c6dbb".to_string(), - AMBIENT_NAME: "missingambient".to_string(), - FRIDGE_NAME: "28-0000042c6dbb".to_string(), - WORT_NAME: "28-0000042cccc4".to_string(), // was fridge - INTERNAL_TEMPERATURE: "/sys/class/thermal/thermal_zone0/temp".to_string(), - - HMAC_KEY: "a key".to_string(), - SERVER_URL: "https://evil.ucc.asn.au/~matt/templog".to_string(), - UPDATE_URL: "https://evil.ucc.asn.au/~matt/templog/update".to_string(), - SETTINGS_URL: "https://evil.ucc.asn.au/~matt/templog/get_settings".to_string(), - } + pub fn default_toml() -> &'static str { + include_str!("defconfig.toml") } - pub fn to_toml_string(&self) -> String { - toml::to_string(self).unwrap() - } - - pub fn merge(&self, conf: &str) -> Result<Self, TemplogError> { - // convert existing and new toml into tables, combine them. - let mut new_toml = toml::from_str(conf)?; - 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); - // TODO: wrap the error with a better message? - let ret = toml::Value::Table(ex_toml.clone()).try_into()?; - Ok(ret) - } - - pub fn merge_file(&self, filename: &str) -> Result<Self, TemplogError> { - - let mut s = String::new(); - File::open(filename)?.read_to_string(&mut s)?; - - self.merge(&s) + pub fn load() -> Result<Self, TemplogError> { + let mut c = config::Config::default(); + c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)); + c.merge(config::File::with_name("local.conf")).unwrap(); + c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); + Ok(c.try_into().unwrap()) } }