comparison rust/src/config.rs @ 635:4424a8b30f9c rust

config crate wants everything to be lower case
author Matt Johnston <matt@ucc.asn.au>
date Sun, 22 Sep 2019 22:06:46 +0800
parents a5721c02d3ee
children 43eb3cfdf769
comparison
equal deleted inserted replaced
634:a5721c02d3ee 635:4424a8b30f9c
1 use serde::{Serialize,Deserialize}; 1 use serde::{Serialize,Deserialize};
2 2
3 use super::types::*; 3 use super::types::*;
4 4
5 #[derive(Deserialize,Serialize,Debug,Clone)] 5 #[derive(Deserialize,Serialize,Debug,Clone)]
6 #[allow(non_snake_case)]
7 pub struct Config { 6 pub struct Config {
8 pub SENSOR_SLEEP: u64, 7 pub sensor_sleep: u64,
9 pub UPLOAD_SLEEP: u64, 8 pub upload_sleep: u64,
10 9
11 pub FRIDGE_DELAY: u64, 10 pub fridge_delay: u64,
12 pub FRIDGE_WORT_INVALID_TIME: u64, 11 pub fridge_wort_invalid_time: u64,
13 12
14 pub MAX_READINGS: u32, 13 pub max_readings: u32,
15 14
16 pub PARAMS_FILE: String, 15 pub params_file: String,
17 16
18 pub SENSOR_BASE_DIR: String, 17 pub sensor_base_dir: String,
19 pub FRIDGE_GPIO_PIN: u32, 18 pub fridge_gpio_pin: u32,
20 19
21 pub AMBIENT_NAME: String, 20 pub ambient_name: String,
22 pub FRIDGE_NAME: String, 21 pub fridge_name: String,
23 pub WORT_NAME: String, 22 pub wort_name: String,
24 pub INTERNAL_TEMPERATURE: String, 23 pub internal_temperature: String,
25 24
26 pub HMAC_KEY: String, 25 pub hmac_key: String,
27 pub SERVER_URL: String, 26 pub server_url: String,
28 pub UPDATE_URL: String, 27 pub update_url: String,
29 pub SETTINGS_URL: String, 28 pub settings_url: String,
30 } 29 }
31 30
32 impl Config { 31 impl Config {
33 pub fn default_toml() -> &'static str { 32 pub fn default_toml() -> &'static str {
34 include_str!("defconfig.toml") 33 include_str!("defconfig.toml")
35 } 34 }
36 35
37 pub fn load() -> Result<Self, TemplogError> { 36 pub fn load(conf_file: &str) -> Result<Self, TemplogError> {
38 let mut c = config::Config::default(); 37 let mut c = config::Config::default();
39 c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)); 38 c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)).expect("Bad default config");
40 c.merge(config::File::with_name("local.conf")).unwrap(); 39 c.merge(config::File::with_name(conf_file)).or_else(|e| {
40 Err(match e {
41 config::ConfigError::NotFound(_) => TemplogError::new(&format!("Missing config {}", conf_file)),
42 _ => TemplogError::new(&format!("Problem parsing {}: {}", conf_file, e)),
43 })
44 })?;
41 c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); 45 c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap();
46 println!("c is {:?}", c);
42 Ok(c.try_into().unwrap()) 47 Ok(c.try_into().unwrap())
43 } 48 }
44 } 49 }