diff --git a/rust/hg-core/src/config/config_items.rs b/rust/hg-core/src/config/config_items.rs --- a/rust/hg-core/src/config/config_items.rs +++ b/rust/hg-core/src/config/config_items.rs @@ -140,6 +140,39 @@ impl<'a> TryFrom<&'a DefaultConfigItem> } } +impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> { + type Error = HgError; + + fn try_from( + value: &'a DefaultConfigItem, + ) -> Result, Self::Error> { + match &value.default { + Some(default) => { + let err = HgError::abort( + format!( + "programming error: wrong query on config item '{}.{}'", + value.section, + value.name + ), + exit_codes::ABORT, + Some(format!( + "asked for bytes, type of default is '{}', \ + which cannot be interpreted as bytes", + default.type_str() + )), + ); + match default { + DefaultConfigItemType::Primitive(p) => { + Ok(p.as_str().map(str::as_bytes)) + } + _ => Err(err), + } + } + None => Ok(None), + } + } +} + impl TryFrom<&DefaultConfigItem> for Option { type Error = HgError; diff --git a/rust/hg-core/src/config/mod.rs b/rust/hg-core/src/config/mod.rs --- a/rust/hg-core/src/config/mod.rs +++ b/rust/hg-core/src/config/mod.rs @@ -422,7 +422,30 @@ impl Config { return Ok(None); } match self.get_default(section, item)? { - Some(default) => Ok(default.try_into()?), + Some(default) => { + // Defaults are TOML values, so they're not in the same + // shape as in the config files. + // First try to convert directly to the expected type + let as_t = default.try_into(); + match as_t { + Ok(t) => Ok(t), + Err(e) => { + // If it fails, it means that... + let as_bytes: Result, _> = + default.try_into(); + match as_bytes { + Ok(bytes_opt) => { + if let Some(bytes) = bytes_opt { + // ...we should be able to parse it + return Ok(parse(bytes)); + } + Err(e) + } + Err(_) => Err(e), + } + } + } + } None => { self.print_devel_warning(section, item)?; Ok(None) @@ -779,7 +802,6 @@ mod tests { let config = Config::load_from_explicit_sources(vec![]) .expect("expected valid config"); let ret = config.get_byte_size(b"cmdserver", b"max-log-size"); - // FIXME should be `is_ok` - assert!(ret.is_err(), "{:?}", ret); + assert!(ret.is_ok(), "{:?}", ret); } }