Mercurial > templog
annotate 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 |
rev | line source |
---|---|
634 | 1 use serde::{Serialize,Deserialize}; |
595 | 2 |
634 | 3 use super::types::*; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
4 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
5 #[derive(Deserialize,Serialize,Debug,Clone)] |
595 | 6 pub struct Config { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
7 pub sensor_sleep: u64, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
8 pub upload_sleep: u64, |
595 | 9 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
10 pub fridge_delay: u64, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
11 pub fridge_wort_invalid_time: u64, |
595 | 12 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
13 pub max_readings: u32, |
595 | 14 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
15 pub params_file: String, |
595 | 16 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
17 pub sensor_base_dir: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
18 pub fridge_gpio_pin: u32, |
595 | 19 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
20 pub ambient_name: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
21 pub fridge_name: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
22 pub wort_name: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
23 pub internal_temperature: String, |
595 | 24 |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
25 pub hmac_key: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
26 pub server_url: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
27 pub update_url: String, |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
28 pub settings_url: String, |
595 | 29 } |
30 | |
31 impl Config { | |
634 | 32 pub fn default_toml() -> &'static str { |
33 include_str!("defconfig.toml") | |
595 | 34 } |
35 | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
36 pub fn load(conf_file: &str) -> Result<Self, TemplogError> { |
634 | 37 let mut c = config::Config::default(); |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
38 c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)).expect("Bad default config"); |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
39 c.merge(config::File::with_name(conf_file)).or_else(|e| { |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
40 Err(match e { |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
41 config::ConfigError::NotFound(_) => TemplogError::new(&format!("Missing config {}", conf_file)), |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
42 _ => TemplogError::new(&format!("Problem parsing {}: {}", conf_file, e)), |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
43 }) |
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
44 })?; |
634 | 45 c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
46 println!("c is {:?}", c); |
634 | 47 Ok(c.try_into().unwrap()) |
597 | 48 } |
595 | 49 } |