Skip to content

Commit

Permalink
vastly simplify and fix a logic issue
Browse files Browse the repository at this point in the history
also remove the 2nd Default fallback, which is actually wrong. if the
'ci' config exists, but 'default' is missing, that is considered error.
  • Loading branch information
srid committed Nov 14, 2024
1 parent 56f163b commit a49b5e6
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions crates/omnix-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl OmConfig {
/// get_sub_config_under("ci") will return `ci.default` (or Default instance if config is missing) without a reference. Otherwise, it will use the reference to find the correct sub-tree.
pub fn get_sub_config_under<T>(&self, root_key: &str) -> Result<(T, &[String]), OmConfigError>
where
T: Default + DeserializeOwned,
T: Default + DeserializeOwned + Clone,
{
// Get the config map, returning default if it doesn't exist
let config = match self.config.get::<T>(root_key)? {
Expand All @@ -55,21 +55,13 @@ impl OmConfig {
}
};

if let Some((k, rest)) = self.reference.split_first() {
// If a reference is provied, look up that key.
config
.into_iter()
.find(|(cfg_name, _)| cfg_name == k)
.map(|(_, v)| (v, rest))
.ok_or_else(|| OmConfigError::MissingConfigAttribute(k.to_string()))
} else {
// Else, fall back to `default` attribute or `T::default()`
let v = config
.into_iter()
.find_map(|(k, v)| if k == "default" { Some(v) } else { None })
.unwrap_or_default();
Ok((v, &[]))
}
let default = "default".to_string();
let (k, rest) = self.reference.split_first().unwrap_or((&default, &[]));

let v: &T = config
.get(k)
.ok_or(OmConfigError::MissingConfigAttribute(k.to_string()))?;
Ok((v.clone(), rest))
}
}

Expand Down

0 comments on commit a49b5e6

Please sign in to comment.