Skip to content

Commit

Permalink
fix(cli): use default if prop not in config file
Browse files Browse the repository at this point in the history
Fixes a "property X not found in config" error when the missing
property has a default value.
  • Loading branch information
dan-da committed Dec 7, 2022
1 parent 29249e7 commit 755a3e9
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions kindelia/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ where
}
if let (Some(prop_path), Some(config_values)) = (self.prop, config_values) {
// If config file and argument prop path are set, read from config file
return Self::resolve_from_config_aux(config_values, &prop_path);
if let Some(v) = Self::resolve_from_config_aux(config_values, &prop_path)?
{
return Ok(v);
}
}
(self.default_value)()
}
Expand All @@ -66,10 +69,13 @@ where
{
if let Some(prop_path) = self.prop {
if let Some(config_values) = config_values {
Self::resolve_from_config_aux(config_values, &prop_path)
} else {
(self.default_value)()
if let Some(v) =
Self::resolve_from_config_aux(config_values, &prop_path)?
{
return Ok(v);
}
}
(self.default_value)()
} else {
Err(anyhow!(
"Cannot resolve from config file config without 'prop' field set"
Expand Down Expand Up @@ -105,18 +111,17 @@ where
fn resolve_from_config_aux(
config_values: &toml::Value,
prop_path: &str,
) -> anyhow::Result<T>
) -> anyhow::Result<Option<T>>
where
T: ArgumentFrom<toml::Value>,
{
let value = Self::get_prop(config_values, prop_path).context(anyhow!(
"Could not find prop '{}' in config file.",
prop_path
))?;
T::arg_from(value).context(anyhow!(
"Could not convert value of '{}' into desired type",
prop_path,
))
match Self::get_prop(config_values, prop_path) {
Some(value) => Ok(Some(T::arg_from(value).context(anyhow!(
"Could not convert value of '{}' into desired type",
prop_path,
))?)),
None => Ok(None),
}
}

fn get_prop(mut value: &toml::Value, prop_path: &str) -> Option<toml::Value> {
Expand Down

0 comments on commit 755a3e9

Please sign in to comment.