Skip to content

Commit

Permalink
Change DeError::Unsupported to store Cow instead of plain &str
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Sep 2, 2022
1 parent 6043d57 commit 0336dcb
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

### Misc Changes

- [#468]: Content of `DeError::Unsupported` changed from `&'static str` to `Cow<'static, str>`

[#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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
18 changes: 10 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
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
4 changes: 3 additions & 1 deletion src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ impl<'r, 'w, W: Write> ser::Serializer for &'w mut Serializer<'r, W> {
fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, DeError> {
// TODO: I imagine you'd want to use base64 here.
// Not sure how to roundtrip effectively though...
Err(DeError::Unsupported("serialize_bytes"))
Err(DeError::Unsupported(
"`serialize_bytes` not supported yet".into(),
))
}

fn serialize_none(self) -> Result<Self::Ok, DeError> {
Expand Down

0 comments on commit 0336dcb

Please sign in to comment.