Mercurial > templog
diff 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 |
line wrap: on
line diff
--- a/rust/src/config.rs Sat Nov 09 11:35:59 2019 +0800 +++ b/rust/src/config.rs Thu Nov 28 23:57:00 2019 +0800 @@ -1,6 +1,5 @@ use serde::{Serialize,Deserialize}; - -use super::types::*; +use anyhow::{Context, anyhow, Result, Error}; #[derive(Deserialize,Serialize,Debug,Clone)] pub struct Config { @@ -33,16 +32,18 @@ include_str!("defconfig.toml") } - pub fn load(conf_file: &str) -> Result<Self, TemplogError> { + pub fn load(conf_file: &str) -> Result<Self> { 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)), + config::ConfigError::NotFound(_) => anyhow!("Missing config {}", conf_file), + // XXX this is ugly, why won't e.with_context() work? + _ => Error::new(e).context(format!("Problem parsing {}", conf_file)), }) })?; - c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); - c.try_into().or_else(|e| Err(TemplogError::new(&format!("Problem loading config {}: {}", conf_file, e)))) + c.merge(config::Environment::with_prefix("TEMPLOG")) + .context("Failed loading from TEMPLOG_ environment variables")?; + Ok(c.try_into().with_context(|| format!("Problem loading config {}", conf_file))?) } }