mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
fix(config): unrecognized config properties don't cause config error (#4547)
* Fix #4481, config does not error when unrecognized properties are present * cleanup: use stuct update syntax to improve readability from review feedback Co-authored-by: David Knaack <davidkna@users.noreply.github.com> * cleanup: renamed ValueDeserializer func w/ better name * cleanup: added test to cover unknown key retry condition Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
+29
-1
@@ -48,7 +48,17 @@ impl<'a, T: Deserialize<'a> + Default> ModuleConfig<'a, ValueError> for T {
|
||||
/// Create `ValueDeserializer` wrapper and use it to call `Deserialize::deserialize` on it.
|
||||
fn from_config(config: &'a Value) -> Result<Self, ValueError> {
|
||||
let deserializer = ValueDeserializer::new(config);
|
||||
T::deserialize(deserializer)
|
||||
T::deserialize(deserializer).or_else(|err| {
|
||||
// If the error is an unrecognized key, print a warning and run
|
||||
// deserialize ignoring that error. Otherwise, just return the error
|
||||
if err.to_string().contains("Unknown key") {
|
||||
log::warn!("{}", err);
|
||||
let deserializer2 = ValueDeserializer::new(config).with_allow_unknown_keys();
|
||||
T::deserialize(deserializer2)
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -582,6 +592,24 @@ mod tests {
|
||||
assert_eq!(rust_config.switch_c, Switch::Off);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_unknown_key_config() {
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
#[serde(default)]
|
||||
struct TestConfig<'a> {
|
||||
pub foo: &'a str,
|
||||
}
|
||||
|
||||
let config = toml::toml! {
|
||||
foo = "test"
|
||||
bar = "ignore me"
|
||||
};
|
||||
let rust_config = TestConfig::from_config(&config);
|
||||
|
||||
assert!(rust_config.is_ok());
|
||||
assert_eq!(rust_config.unwrap().foo, "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_string() {
|
||||
let config = Value::String(String::from("S"));
|
||||
|
||||
Reference in New Issue
Block a user