Skip to content

Commit 1fafe75

Browse files
Add support for parsing boolean yaml hash keys
1 parent 4c11e78 commit 1fafe75

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/file/format/yaml.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fn from_yaml_value(
5050
match key {
5151
yaml::Yaml::String(k) => m.insert(k.to_owned(), from_yaml_value(uri, value)?),
5252
yaml::Yaml::Integer(k) => m.insert(k.to_string(), from_yaml_value(uri, value)?),
53+
yaml::Yaml::Boolean(k) => m.insert(k.to_string(), from_yaml_value(uri, value)?),
5354
other => Err(Box::new(UnsupportedHashKeyError(format!("{other:?}"))))?,
5455
};
5556
}

tests/testsuite/file_yaml.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,13 @@ inner_vec:
360360
}
361361

362362
#[test]
363-
fn test_yaml_parsing_bool_hash_fails() {
364-
let result = Config::builder()
363+
fn test_yaml_parsing_bool_hash() {
364+
#[derive(Debug, Deserialize)]
365+
struct TestStruct {
366+
inner_bool: HashMap<bool, String>,
367+
}
368+
369+
let config = Config::builder()
365370
.add_source(File::from_str(
366371
r#"
367372
inner_bool:
@@ -370,10 +375,10 @@ inner_bool:
370375
"#,
371376
FileFormat::Yaml,
372377
))
373-
.build();
374-
assert!(result.is_err());
375-
assert_data_eq!(
376-
result.unwrap_err().to_string(),
377-
str!["Cannot parse Boolean(true) because it is an unsupported hash key type"]
378-
);
378+
.build()
379+
.unwrap()
380+
.try_deserialize::<TestStruct>()
381+
.unwrap();
382+
assert_eq!(config.inner_bool.get(&true).unwrap(), "bool true");
383+
assert_eq!(config.inner_bool.get(&false).unwrap(), "bool false");
379384
}

0 commit comments

Comments
 (0)