From: Ian Jackson Date: Sat, 1 Feb 2025 12:05:13 +0000 (+0000) Subject: config: Clarify Parseable's defaulting X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=74b2740320a9fe80bf3abdc4df78ebd44065d5ec;p=hippotat.git config: Clarify Parseable's defaulting Signed-off-by: Ian Jackson --- diff --git a/src/config.rs b/src/config.rs index e001dfd..9ff2d83 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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; - fn default() -> Result { + + /// 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 { 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 Parseable for Vec { .collect::,_>>()? } #[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())?, } }