Skip to content

Commit

Permalink
Deserializing options is now fuzzier
Browse files Browse the repository at this point in the history
Some() is now implied if it is not present when deserialize_option is
called.
  • Loading branch information
ecton committed Jun 14, 2023
1 parent aeb5e56 commit 71b5685
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,23 @@ impl<'de> serde::de::Deserializer<'de> for &mut Deserializer<'de> {
where
V: serde::de::Visitor<'de>,
{
self.with_error_context(|de| match de.parser.next().transpose()? {
Some(Event {
self.with_error_context(|de| match de.parser.peek() {
Some(Ok(Event {
kind: EventKind::Primitive(Primitive::Identifier(str)),
..
}) if str == "None" => visitor.visit_none(),
Some(Event {
})) if *str == "None" => {
de.parser.next();
visitor.visit_none()
}
Some(Ok(Event {
kind:
EventKind::BeginNested {
name,
kind: Nested::Tuple,
},
..
}) if matches!(name, Some(Name { name: "Some", .. })) => {
})) if matches!(name, Some(Name { name: "Some", .. })) => {
de.parser.next();
let result = visitor.visit_some(&mut *de)?;
match de.parser.next().transpose()? {
Some(Event {
Expand All @@ -439,11 +443,8 @@ impl<'de> serde::de::Deserializer<'de> for &mut Deserializer<'de> {
None => unreachable!("parser errors on early eof"),
}
}
Some(evt) => Err(DeserializerError::new(
evt.location,
ErrorKind::ExpectedOption,
)),
None => Err(DeserializerError::new(None, ErrorKind::ExpectedOption)),
_ => visitor.visit_some(de),
})
}

Expand Down Expand Up @@ -1446,6 +1447,9 @@ mod tests {
let parsed =
crate::from_str::<Option<BasicNamed>>(r#"Some(BasicNamed{ a: 1, b: -1 })"#).unwrap();
assert_eq!(parsed, Some(BasicNamed { a: 1, b: -1 }));

let parsed = crate::from_str::<Option<BasicNamed>>(r#"BasicNamed{ a: 1, b: -1 }"#).unwrap();
assert_eq!(parsed, Some(BasicNamed { a: 1, b: -1 }));
}

#[test]
Expand Down

0 comments on commit 71b5685

Please sign in to comment.