view 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 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();
        println!("c is {:?}", c);
        Ok(c.try_into().unwrap())
    }
}