chiark / gitweb /
config: Fix error reporting
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 14:23:07 +0000 (15:23 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 14:23:07 +0000 (15:23 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs

index e770a13297d4edab1952e4a9b71633ca1b927556..da69bf6dd3784dc291beb756fa6932282389e3dc 100644 (file)
@@ -95,9 +95,26 @@ pub enum SectionName {
 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,
 }
@@ -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<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
@@ -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<RawValRef<'c,'c,'c>> {
     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 "{}""#, &section))
-          .dcontext(&raw.loc)?
-      }),
+      Some(raw) => Some(raw.try_map(Parseable::parse)?),
     }
   }