From d27bea2c4391fded42e770116d6a689b89ae121a Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 17 Jul 2025 09:59:52 -0500 Subject: [PATCH] Util to get sometimes-empty config property The get_property function needs to say that there is no property so that the caller can respond appropriately. I'm going to need to frequently respond to the "no such property" path by treating it as *not* an error. If the config file doesn't specify a property, that's not an error, it's just not specified and the default should be used instead. This util fn makes that a bit more ergonomic. --- src/config.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/config.rs b/src/config.rs index 80361da..e9106b4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,6 +35,23 @@ fn get_table<'outer>(outer: &'outer Table, table_name: impl ToString) -> Result< .ok_or(Error::BadFormat)?) } + +/// Similar to `get_property()` but maps the "Error::NoSuchProperty" result to +/// Option::None. Some properties aren't specified, and that's okay... sometimes. +fn get_maybe_property<'outer> (outer: &'outer Table, property: impl ToString) -> Result> { + let maybe_prop = get_property(outer, property); + match maybe_prop { + Ok(value) => Ok(Some(value)), + Err(e) => { + if let Error::NoSuchProperty = e { + return Ok(None); + } else { + return Err(e); + } + } + } +} + /// The config properties are individual strings. This gets the named property, /// or an error explaining why it couldn't be fetched. fn get_property<'outer>(outer: &'outer Table, property: impl ToString) -> Result<&'outer String> {