Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start rewrite serde serialization tests #468

Merged
merged 11 commits into from Sep 4, 2022
Merged
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -186,6 +186,10 @@ required-features = ["serialize"]
name = "serde-de"
required-features = ["serialize"]

[[test]]
name = "serde-se"
required-features = ["serialize"]

[[test]]
name = "serde-migrated"
required-features = ["serialize"]
Expand Down
5 changes: 5 additions & 0 deletions Changelog.md
Expand Up @@ -16,6 +16,11 @@

### Misc Changes

- [#468]: Content of `DeError::Unsupported` changed from `&'static str` to `Cow<'static, str>`
- [#468]: Ensure that map keys are restricted to only types that can be serialized as primitives

[#468]: https://github.com/tafia/quick-xml/pull/468

## 0.24.0 -- 2022-08-28

### New Features
Expand Down
2 changes: 1 addition & 1 deletion src/de/escape.rs
Expand Up @@ -81,7 +81,7 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"binary data content is not supported by XML format",
"binary data content is not supported by XML format".into(),
))
}

Expand Down
4 changes: 3 additions & 1 deletion src/de/map.rs
Expand Up @@ -752,7 +752,9 @@ where
self.map.de.reader.decoder(),
)
.deserialize_seq(visitor),
e => Err(DeError::Custom(format!("Unsupported event {:?}", e))),
e => Err(DeError::Unsupported(
format!("unsupported event {:?}", e).into(),
)),
};
// TODO: May be assert that here we expect only matching closing tag?
self.map.de.read_to_end(e.name())?;
Expand Down
2 changes: 1 addition & 1 deletion src/de/mod.rs
Expand Up @@ -81,7 +81,7 @@ macro_rules! deserialize_primitives {
where
V: Visitor<'de>,
{
Err(DeError::Unsupported("binary data content is not supported by XML format"))
Err(DeError::Unsupported("binary data content is not supported by XML format".into()))
}

/// Forwards deserialization to the [`deserialize_bytes`](#method.deserialize_bytes).
Expand Down
16 changes: 8 additions & 8 deletions src/de/simple_type.rs
Expand Up @@ -47,7 +47,7 @@ macro_rules! unsupported {
$($(_: $type,)*)?
_visitor: V
) -> Result<V::Value, Self::Error> {
Err(DeError::Unsupported($message))
Err(DeError::Unsupported($message.into()))
}
};
}
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<'de> VariantAccess<'de> for AtomicUnitOnly {
T: DeserializeSeed<'de>,
{
Err(DeError::Unsupported(
"enum newtype variants are not supported as `xs:list` items",
"enum newtype variants are not supported as `xs:list` items".into(),
))
}

Expand All @@ -354,7 +354,7 @@ impl<'de> VariantAccess<'de> for AtomicUnitOnly {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"enum tuple variants are not supported as `xs:list` items",
"enum tuple variants are not supported as `xs:list` items".into(),
))
}

Expand All @@ -367,7 +367,7 @@ impl<'de> VariantAccess<'de> for AtomicUnitOnly {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"enum struct variants are not supported as `xs:list` items",
"enum struct variants are not supported as `xs:list` items".into(),
))
}
}
Expand Down Expand Up @@ -648,7 +648,7 @@ impl<'de, 'a> Deserializer<'de> for SimpleTypeDeserializer<'de, 'a> {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"binary data content is not supported by XML format",
"binary data content is not supported by XML format".into(),
))
}

Expand Down Expand Up @@ -796,7 +796,7 @@ impl<'de> VariantAccess<'de> for SimpleTypeUnitOnly {
T: DeserializeSeed<'de>,
{
Err(DeError::Unsupported(
"enum newtype variants are not supported for XSD `simpleType`s",
"enum newtype variants are not supported for XSD `simpleType`s".into(),
))
}

Expand All @@ -805,7 +805,7 @@ impl<'de> VariantAccess<'de> for SimpleTypeUnitOnly {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"enum tuple variants are not supported for XSD `simpleType`s",
"enum tuple variants are not supported for XSD `simpleType`s".into(),
))
}

Expand All @@ -818,7 +818,7 @@ impl<'de> VariantAccess<'de> for SimpleTypeUnitOnly {
V: Visitor<'de>,
{
Err(DeError::Unsupported(
"enum struct variants are not supported for XSD `simpleType`s",
"enum struct variants are not supported for XSD `simpleType`s".into(),
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/de/var.rs
Expand Up @@ -43,7 +43,7 @@ where
}
_ => {
return Err(DeError::Unsupported(
"Invalid event for Enum, expecting `Text` or `Start`",
"Invalid event for Enum, expecting `Text` or `Start`".into(),
))
}
};
Expand Down
25 changes: 17 additions & 8 deletions src/errors.rs
Expand Up @@ -3,14 +3,16 @@
use crate::escape::EscapeError;
use crate::events::attributes::AttrError;
use crate::utils::write_byte_string;
use std::fmt;
use std::io::Error as IoError;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

/// The error type used by this crate.
#[derive(Debug)]
pub enum Error {
/// IO error
Io(::std::io::Error),
Io(IoError),
/// Input decoding error. If `encoding` feature is disabled, contains `None`,
/// otherwise contains the UTF-8 decoding error
NonDecodable(Option<Utf8Error>),
Expand Down Expand Up @@ -39,10 +41,10 @@ pub enum Error {
UnknownPrefix(Vec<u8>),
}

impl From<::std::io::Error> for Error {
impl From<IoError> for Error {
/// Creates a new `Error::Io` from the given error
#[inline]
fn from(error: ::std::io::Error) -> Error {
fn from(error: IoError) -> Error {
Error::Io(error)
}
}
Expand Down Expand Up @@ -81,8 +83,8 @@ impl From<AttrError> for Error {
/// A specialized `Result` type where the error is hard-wired to [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::NonDecodable(None) => write!(f, "Malformed input, decoding impossible"),
Expand Down Expand Up @@ -132,7 +134,7 @@ pub mod serialize {

use super::*;
use crate::utils::write_byte_string;
use std::fmt;
use std::borrow::Cow;
#[cfg(feature = "overlapped-lists")]
use std::num::NonZeroUsize;
use std::num::{ParseFloatError, ParseIntError};
Expand Down Expand Up @@ -186,7 +188,7 @@ pub mod serialize {
/// 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.
Unsupported(&'static str),
Unsupported(Cow<'static, str>),
/// Too many events were skipped while deserializing a sequence, event limit
/// exceeded. The limit was provided as an argument
#[cfg(feature = "overlapped-lists")]
Expand Down Expand Up @@ -214,7 +216,7 @@ pub mod serialize {
}
DeError::UnexpectedEof => write!(f, "Unexpected `Event::Eof`"),
DeError::ExpectedStart => write!(f, "Expecting `Event::Start`"),
DeError::Unsupported(s) => write!(f, "Unsupported operation {}", s),
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 Expand Up @@ -292,4 +294,11 @@ pub mod serialize {
Self::InvalidFloat(e)
}
}

impl From<fmt::Error> for DeError {
#[inline]
fn from(e: fmt::Error) -> Self {
Self::Custom(e.to_string())
}
}
}