Skip to content

Commit

Permalink
Fix error on parsing NULL input
Browse files Browse the repository at this point in the history
Allow `YamlValueData::Null` to be parsed in `deserialize_map()`.

Integration test case included.

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Jan 4, 2025
1 parent 756007f commit cc562d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ impl<'de> Deserializer<'de> for &mut RmsdDeserializer {
// where we can use `Option::take()` to move data out.
let access = YamlValueMapAccess::new(*v.clone());
visitor.visit_map(access)
} else if let YamlValueData::Null = &self.parsed.data {
let access = YamlValueMapAccess::new(Default::default());
visitor.visit_map(access)
} else {
Err(RmsdError::unexpected_yaml_node_type(
format!("Expecting a map, got {}", self.parsed.data),
Expand Down
2 changes: 1 addition & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
YamlTokenData, YamlValue, YamlValueData,
};

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct YamlValueMap(IndexMap<YamlValue, YamlValue>);

impl std::hash::Hash for YamlValueMap {
Expand Down
5 changes: 5 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ impl YamlValue {
}
YamlTokenData::MapKeyIndicator => get_map(iter, false),
YamlTokenData::LocalTag(_) => get_tag(iter),
YamlTokenData::Null => Ok(YamlValue {
start: token.start,
end: token.end,
data: YamlValueData::Null,
}),
_ => {
if iter.data.get(1).and_then(|t| {
t.as_ref()
Expand Down
12 changes: 12 additions & 0 deletions tests/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,15 @@ fn test_signed_interger() -> Result<(), RmsdError> {
);
Ok(())
}

#[test]
fn test_empty_input() -> Result<(), RmsdError> {
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
struct FooTest {
#[serde(default)]
uint_a: u32,
}

assert_eq!(FooTest { uint_a: 0 }, rmsd_yaml::from_str::<FooTest>("")?);
Ok(())
}

0 comments on commit cc562d5

Please sign in to comment.