Mercurial > templog
view rust/src/config.rs @ 636:43eb3cfdf769 rust
some progress, better error handling
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 16 Oct 2019 22:33:06 +0800 |
parents | 4424a8b30f9c |
children | 89818a14648b |
line wrap: on
line source
use serde::{Serialize,Deserialize}; use super::types::*; #[derive(Deserialize,Serialize,Debug,Clone)] pub struct Config { pub sensor_sleep: u64, pub upload_sleep: u64, pub fridge_delay: u64, pub fridge_wort_invalid_time: u64, pub max_readings: u32, pub params_file: String, pub sensor_base_dir: String, pub fridge_gpio_pin: u32, pub ambient_name: String, pub fridge_name: String, pub wort_name: String, pub internal_temperature: String, pub hmac_key: String, pub server_url: String, pub update_url: String, pub settings_url: String, } impl Config { pub fn default_toml() -> &'static str { include_str!("defconfig.toml") } pub fn load(conf_file: &str) -> Result<Self, TemplogError> { let mut c = config::Config::default(); c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)).expect("Bad default config"); c.merge(config::File::with_name(conf_file)).or_else(|e| { Err(match e { config::ConfigError::NotFound(_) => TemplogError::new(&format!("Missing config {}", conf_file)), _ => TemplogError::new(&format!("Problem parsing {}: {}", conf_file, e)), }) })?; c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); c.try_into().or_else(|e| Err(TemplogError::new(&format!("Problem loading config {}: {}", conf_file, e)))) } }