chiark / gitweb /
config: Clarify Parseable's defaulting
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Feb 2025 12:05:13 +0000 (12:05 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Feb 2025 12:37:23 +0000 (12:37 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs

index e001dfd4b3a9f5135a398d0140c19b8f3146ac02..9ff2d83ccf5a012ef53d1ec6860bcda8593ce653 100644 (file)
@@ -240,7 +240,7 @@ impl Parseable for Secret {
     Secret(s.into())
   }
   #[throws(AE)]
-  fn default() -> Self { Secret(default()) }
+  fn default_for_ordinary() -> Self { Secret(default()) }
 }
 impl Debug for Secret {
   #[throws(fmt::Error)]
@@ -613,13 +613,14 @@ struct ResolveContext<'c> {
 
 trait Parseable: Sized {
   fn parse(s: Option<&str>) -> Result<Self, AE>;
-  fn default() -> Result<Self, AE> {
+
+  /// Used for lookups with [`ResolveContest::ordinary`] etc.
+  ///
+  /// Fails, if this setting ought to have been specified.
+  /// The caller will add a key name to the error.
+  fn default_for_ordinary() -> Result<Self, AE> {
     Err(anyhow!("setting must be specified"))
   }
-  #[throws(AE)]
-  fn default_for_key(key: &str) -> Self {
-    Self::default().with_context(|| key.to_string())?
-  }
 }
 
 impl Parseable for Duration {
@@ -633,7 +634,7 @@ macro_rules! parseable_from_str { ($t:ty $(, $def:expr)? ) => {
   impl Parseable for $t {
     #[throws(AE)]
     fn parse(s: Option<&str>) -> $t { s.value()?.parse()? }
-    $( #[throws(AE)] fn default() -> Self { $def } )?
+    $( #[throws(AE)] fn default_for_ordinary() -> Self { $def } )?
   }
 } }
 parseable_from_str!{u16, default() }
@@ -652,7 +653,7 @@ impl<T:Parseable> Parseable for Vec<T> {
       .collect::<Result<Vec<_>,_>>()?
   }
   #[throws(AE)]
-  fn default() -> Self { default() }
+  fn default_for_ordinary() -> Self { default() }
 }
 
 
@@ -760,7 +761,8 @@ impl<'c> ResolveContext<'c> {
   {
     match self.first_of(key, skl)? {
       Some(y) => y,
-      None => Parseable::default_for_key(key)?,
+      None => Parseable::default_for_ordinary()
+        .with_context(|| key.to_string())?,
     }
   }