Skip to content

Commit

Permalink
Remove DeError::ExpectedStart, call Visitor::visit_borrowed_str o…
Browse files Browse the repository at this point in the history
…r `visit_string` instead

It is responsibility of a `Visitor` to return an error if the provided data can not be handled
  • Loading branch information
Mingun committed Oct 28, 2023
1 parent d9520c4 commit 77e5a2c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/de/mod.rs
Expand Up @@ -2821,7 +2821,13 @@ where
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Text(_) => Err(DeError::ExpectedStart),
// Deserializer methods are only hints, if deserializer could not satisfy
// request, it should return the data that it has. It is responsibility
// of a Visitor to return an error if it does not understand the data
DeEvent::Text(e) => match e.text {
Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
Cow::Owned(s) => visitor.visit_string(s),
},
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/de/text.rs
Expand Up @@ -32,7 +32,9 @@ use std::borrow::Cow;
/// deserializer;
/// - sequences, tuples and tuple structs are deserialized using [`SimpleTypeDeserializer`]
/// (this is the difference): text content passed to the deserializer directly;
/// - structs and maps returns [`DeError::ExpectedStart`];
/// - structs and maps calls [`Visitor::visit_borrowed_str`] or [`Visitor::visit_string`],
/// it is responsibility of the type to return an error if it do not able to process
/// this data;
/// - enums:
/// - the variant name is deserialized as `$text`;
/// - the content is deserialized using the same deserializer:
Expand Down Expand Up @@ -117,12 +119,15 @@ impl<'de> Deserializer<'de> for TextDeserializer<'de> {
self,
_name: &'static str,
_fields: &'static [&'static str],
_visitor: V,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(DeError::ExpectedStart)
// Deserializer methods are only hints, if deserializer could not satisfy
// request, it should return the data that it has. It is responsibility
// of a Visitor to return an error if it does not understand the data
self.deserialize_str(visitor)
}

fn deserialize_enum<V>(
Expand Down
7 changes: 0 additions & 7 deletions src/errors.rs
Expand Up @@ -205,12 +205,6 @@ pub mod serialize {
/// [`Event::Start`]: crate::events::Event::Start
/// [`Event::End`]: crate::events::Event::End
UnexpectedEof,
/// This error indicates that [`deserialize_struct`] was called, but there
/// is no any XML element in the input. That means that you try to deserialize
/// a struct not from an XML element.
///
/// [`deserialize_struct`]: serde::de::Deserializer::deserialize_struct
ExpectedStart,
/// An attempt to deserialize to a type, that is not supported by the XML
/// store at current position, for example, attempt to deserialize `struct`
/// from attribute or attempt to deserialize binary data.
Expand Down Expand Up @@ -245,7 +239,6 @@ pub mod serialize {
f.write_str(")`")
}
DeError::UnexpectedEof => write!(f, "Unexpected `Event::Eof`"),
DeError::ExpectedStart => write!(f, "Expecting `Event::Start`"),
DeError::Unsupported(s) => write!(f, "Unsupported operation: {}", s),
#[cfg(feature = "overlapped-lists")]
DeError::TooManyEvents(s) => write!(f, "Deserializer buffers {} events, limit exceeded", s),
Expand Down
20 changes: 16 additions & 4 deletions tests/serde-de.rs
Expand Up @@ -890,8 +890,14 @@ mod struct_ {
match from_str::<Elements>(
"\nexcess text\t<root><float>42</float><string>answer</string></root>",
) {
Err(DeError::ExpectedStart) => {}
x => panic!("Expected `Err(ExpectedStart)`, but got `{:?}`", x),
Err(DeError::Custom(reason)) => assert_eq!(
reason,
"invalid type: string \"excess text\", expected struct Elements",
),
x => panic!(
r#"Expected `Err(Custom("invalid type: string \"excess text\", expected struct Elements"))`, but got `{:?}`"#,
x
),
};
}

Expand All @@ -901,8 +907,14 @@ mod struct_ {
match from_str::<Elements>(
"<![CDATA[excess cdata]]><root><float>42</float><string>answer</string></root>",
) {
Err(DeError::ExpectedStart) => {}
x => panic!("Expected `Err(ExpectedStart)`, but got `{:?}`", x),
Err(DeError::Custom(reason)) => assert_eq!(
reason,
"invalid type: string \"excess cdata\", expected struct Elements",
),
x => panic!(
r#"Expected `Err(Custom("invalid type: string \"excess cdata\", expected struct Elements"))`, but got `{:?}`"#,
x
),
};
}

Expand Down

0 comments on commit 77e5a2c

Please sign in to comment.