From: Ian Jackson Date: Sat, 24 Jul 2021 14:23:07 +0000 (+0100) Subject: config: Fix error reporting X-Git-Tag: hippotat/1.0.0~476 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=0fb6c4e0c973cfc3b68362b82d332bb95fe2de6c;p=hippotat.git config: Fix error reporting Signed-off-by: Ian Jackson --- diff --git a/src/config.rs b/src/config.rs index e770a13..da69bf6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -95,9 +95,26 @@ pub enum SectionName { pub use SectionName as SN; #[derive(Debug,Clone)] -struct RawVal { val: Option, loc: Arc } +struct RawVal { raw: Option, loc: Arc } type SectionMap = HashMap; +struct RawValRef<'v,'l,'s> { + raw: Option<&'v str>, + key: &'static str, + loc: &'l Path, + section: &'s SectionName, +} + +impl<'v> RawValRef<'v,'_,'_> { + #[throws(AE)] + fn try_map(&self, f: F) -> T + where F: FnOnce(Option<&'v str>) -> Result { + f(self.raw) + .with_context(|| format!(r#"file {:?}, section "{:?}", key "{}"#, + self.loc, self.section, self.key))? + } +} + pub struct Config { opts: Opts, } @@ -177,9 +194,9 @@ impl Aggregate { .or_default() .extend( vars.into_iter() - .map(|(k,val)| { + .map(|(k,raw)| { (k.replace('-',"_"), - RawVal { val, loc: loc.clone() }) + RawVal { raw, loc: loc.clone() }) }) ); } @@ -372,16 +389,20 @@ impl SectionKindList { } impl Aggregate { - fn lookup_raw<'n, S>(&self, key: &'static str, sections: S) - -> Option<&RawVal> - where S: Iterator + fn lookup_raw<'a,'s,S>(&'a self, key: &'static str, sections: S) + -> Option> + where S: Iterator { for section in sections { if let Some(raw) = self.sections .get(section) .and_then(|vars: &SectionMap| vars.get(key)) { - return Some(raw) + return Some(RawValRef { + raw: raw.raw.as_deref(), + loc: &raw.loc, + section, key, + }) } } None @@ -389,11 +410,12 @@ impl Aggregate { #[throws(AE)] pub fn establish_server_name(&self) -> ServerName { + let key = "server"; let raw = match self.lookup_raw( - "server", + key, [ &SectionName::Common, &SN::special_server_section() ].iter().cloned() ) { - Some(raw) => raw.val.as_deref().value()?, + Some(raw) => raw.try_map(|os| os.value())?, None => SPECIAL_SERVER_SECTION, }; ServerName(raw.into()) @@ -401,8 +423,8 @@ impl Aggregate { } impl<'c> ResolveContext<'c> { - fn first_of_raw(&self, key: &'static str, sections: SectionKindList) - -> Option<&'c RawVal> { + fn first_of_raw(&'c self, key: &'static str, sections: SectionKindList) + -> Option> { self.agg.lookup_raw( key, self.all_sections.iter() @@ -417,12 +439,7 @@ impl<'c> ResolveContext<'c> { { match self.first_of_raw(key, sections) { None => None, - Some(raw) => Some({ - Parseable::parse(raw.val.as_deref()) - .context(key) -// .with_context(|| format!(r#"in section "{}""#, §ion)) - .dcontext(&raw.loc)? - }), + Some(raw) => Some(raw.try_map(Parseable::parse)?), } }