diff 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
line wrap: on
line diff
--- a/rust/src/config.rs	Sun Sep 22 20:35:40 2019 +0800
+++ b/rust/src/config.rs	Sun Sep 22 22:06:46 2019 +0800
@@ -3,30 +3,29 @@
 use super::types::*;
 
 #[derive(Deserialize,Serialize,Debug,Clone)]
-#[allow(non_snake_case)]
 pub struct Config {
-    pub SENSOR_SLEEP: u64,
-    pub UPLOAD_SLEEP: u64,
+    pub sensor_sleep: u64,
+    pub upload_sleep: u64,
 
-    pub FRIDGE_DELAY: u64,
-    pub FRIDGE_WORT_INVALID_TIME: u64,
+    pub fridge_delay: u64,
+    pub fridge_wort_invalid_time: u64,
 
-    pub MAX_READINGS: u32,
+    pub max_readings: u32,
 
-    pub PARAMS_FILE: String,
+    pub params_file: String,
 
-    pub SENSOR_BASE_DIR: String,
-    pub FRIDGE_GPIO_PIN: u32,
+    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 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,
+    pub hmac_key: String,
+    pub server_url: String,
+    pub update_url: String, 
+    pub settings_url: String,
 }
 
 impl Config {
@@ -34,11 +33,17 @@
         include_str!("defconfig.toml")
     }
 
-    pub fn load() -> Result<Self, TemplogError> {
+    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));
-        c.merge(config::File::with_name("local.conf")).unwrap();
+        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();
+        println!("c is {:?}", c);
         Ok(c.try_into().unwrap())
     }
 }