pub use SectionName as SN;
#[derive(Debug,Clone)]
-struct RawVal { val: Option<String>, loc: Arc<PathBuf> }
+struct RawVal { raw: Option<String>, loc: Arc<PathBuf> }
type SectionMap = HashMap<String, RawVal>;
+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<F,T>(&self, f: F) -> T
+ where F: FnOnce(Option<&'v str>) -> Result<T, AE> {
+ f(self.raw)
+ .with_context(|| format!(r#"file {:?}, section "{:?}", key "{}"#,
+ self.loc, self.section, self.key))?
+ }
+}
+
pub struct Config {
opts: Opts,
}
.or_default()
.extend(
vars.into_iter()
- .map(|(k,val)| {
+ .map(|(k,raw)| {
(k.replace('-',"_"),
- RawVal { val, loc: loc.clone() })
+ RawVal { raw, loc: loc.clone() })
})
);
}
}
impl Aggregate {
- fn lookup_raw<'n, S>(&self, key: &'static str, sections: S)
- -> Option<&RawVal>
- where S: Iterator<Item=&'n SectionName>
+ fn lookup_raw<'a,'s,S>(&'a self, key: &'static str, sections: S)
+ -> Option<RawValRef<'a,'a,'s>>
+ where S: Iterator<Item=&'s SectionName>
{
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
#[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())
}
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<RawValRef<'c,'c,'c>> {
self.agg.lookup_raw(
key,
self.all_sections.iter()
{
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)?),
}
}