annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
1 use serde::{Serialize,Deserialize};
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 }
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 impl Config {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
32 pub fn default_toml() -> &'static str {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
33 include_str!("defconfig.toml")
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 }
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
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
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
45 c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap();
636
43eb3cfdf769 some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
46 c.try_into().or_else(|e| Err(TemplogError::new(&format!("Problem loading config {}: {}", conf_file, e))))
597
a440eafa84a9 progress for debug
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
47 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 }