annotate rust/src/config.rs @ 639:89818a14648b rust tip

- switch to using anyhow for errors, surf for http runs but surf has problems
author Matt Johnston <matt@ucc.asn.au>
date Thu, 28 Nov 2019 23:57:00 +0800
parents 43eb3cfdf769
children
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};
639
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
2 use anyhow::{Context, anyhow, Result, Error};
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
3
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
4 #[derive(Deserialize,Serialize,Debug,Clone)]
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 pub struct Config {
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
6 pub sensor_sleep: u64,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
7 pub upload_sleep: u64,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
9 pub fridge_delay: u64,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
10 pub fridge_wort_invalid_time: u64,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
12 pub max_readings: u32,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
14 pub params_file: String,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
16 pub sensor_base_dir: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
17 pub fridge_gpio_pin: u32,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
19 pub ambient_name: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
20 pub fridge_name: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
21 pub wort_name: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
22 pub internal_temperature: String,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
24 pub hmac_key: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
25 pub server_url: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
26 pub update_url: String,
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
27 pub settings_url: String,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 }
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 impl Config {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
31 pub fn default_toml() -> &'static str {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
32 include_str!("defconfig.toml")
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 }
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
639
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
35 pub fn load(conf_file: &str) -> Result<Self> {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
36 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
37 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
38 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
39 Err(match e {
639
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
40 config::ConfigError::NotFound(_) => anyhow!("Missing config {}", conf_file),
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
41 // XXX this is ugly, why won't e.with_context() work?
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
42 _ => Error::new(e).context(format!("Problem parsing {}", conf_file)),
635
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 })?;
639
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
45 c.merge(config::Environment::with_prefix("TEMPLOG"))
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
46 .context("Failed loading from TEMPLOG_ environment variables")?;
89818a14648b - switch to using anyhow for errors, surf for http
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
47 Ok(c.try_into().with_context(|| format!("Problem loading config {}", conf_file))?)
597
a440eafa84a9 progress for debug
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
48 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 }