Skip to content

Commit 0336dcb

Browse files
committed
Change DeError::Unsupported to store Cow instead of plain &str
1 parent 6043d57 commit 0336dcb

File tree

8 files changed

+31
-21
lines changed

8 files changed

+31
-21
lines changed

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
### Misc Changes
1818

19+
- [#468]: Content of `DeError::Unsupported` changed from `&'static str` to `Cow<'static, str>`
20+
21+
[#468]: https://github.com/tafia/quick-xml/pull/468
22+
1923
## 0.24.0 -- 2022-08-28
2024

2125
### New Features

src/de/escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
8181
V: Visitor<'de>,
8282
{
8383
Err(DeError::Unsupported(
84-
"binary data content is not supported by XML format",
84+
"binary data content is not supported by XML format".into(),
8585
))
8686
}
8787

src/de/map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,9 @@ where
752752
self.map.de.reader.decoder(),
753753
)
754754
.deserialize_seq(visitor),
755-
e => Err(DeError::Custom(format!("Unsupported event {:?}", e))),
755+
e => Err(DeError::Unsupported(
756+
format!("unsupported event {:?}", e).into(),
757+
)),
756758
};
757759
// TODO: May be assert that here we expect only matching closing tag?
758760
self.map.de.read_to_end(e.name())?;

src/de/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ macro_rules! deserialize_primitives {
8181
where
8282
V: Visitor<'de>,
8383
{
84-
Err(DeError::Unsupported("binary data content is not supported by XML format"))
84+
Err(DeError::Unsupported("binary data content is not supported by XML format".into()))
8585
}
8686

8787
/// Forwards deserialization to the [`deserialize_bytes`](#method.deserialize_bytes).

src/de/simple_type.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ macro_rules! unsupported {
4747
$($(_: $type,)*)?
4848
_visitor: V
4949
) -> Result<V::Value, Self::Error> {
50-
Err(DeError::Unsupported($message))
50+
Err(DeError::Unsupported($message.into()))
5151
}
5252
};
5353
}
@@ -345,7 +345,7 @@ impl<'de> VariantAccess<'de> for AtomicUnitOnly {
345345
T: DeserializeSeed<'de>,
346346
{
347347
Err(DeError::Unsupported(
348-
"enum newtype variants are not supported as `xs:list` items",
348+
"enum newtype variants are not supported as `xs:list` items".into(),
349349
))
350350
}
351351

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

@@ -367,7 +367,7 @@ impl<'de> VariantAccess<'de> for AtomicUnitOnly {
367367
V: Visitor<'de>,
368368
{
369369
Err(DeError::Unsupported(
370-
"enum struct variants are not supported as `xs:list` items",
370+
"enum struct variants are not supported as `xs:list` items".into(),
371371
))
372372
}
373373
}
@@ -648,7 +648,7 @@ impl<'de, 'a> Deserializer<'de> for SimpleTypeDeserializer<'de, 'a> {
648648
V: Visitor<'de>,
649649
{
650650
Err(DeError::Unsupported(
651-
"binary data content is not supported by XML format",
651+
"binary data content is not supported by XML format".into(),
652652
))
653653
}
654654

@@ -796,7 +796,7 @@ impl<'de> VariantAccess<'de> for SimpleTypeUnitOnly {
796796
T: DeserializeSeed<'de>,
797797
{
798798
Err(DeError::Unsupported(
799-
"enum newtype variants are not supported for XSD `simpleType`s",
799+
"enum newtype variants are not supported for XSD `simpleType`s".into(),
800800
))
801801
}
802802

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

@@ -818,7 +818,7 @@ impl<'de> VariantAccess<'de> for SimpleTypeUnitOnly {
818818
V: Visitor<'de>,
819819
{
820820
Err(DeError::Unsupported(
821-
"enum struct variants are not supported for XSD `simpleType`s",
821+
"enum struct variants are not supported for XSD `simpleType`s".into(),
822822
))
823823
}
824824
}

src/de/var.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ where
4343
}
4444
_ => {
4545
return Err(DeError::Unsupported(
46-
"Invalid event for Enum, expecting `Text` or `Start`",
46+
"Invalid event for Enum, expecting `Text` or `Start`".into(),
4747
))
4848
}
4949
};

src/errors.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
use crate::escape::EscapeError;
44
use crate::events::attributes::AttrError;
55
use crate::utils::write_byte_string;
6+
use std::fmt;
7+
use std::io::Error as IoError;
68
use std::str::Utf8Error;
79
use std::string::FromUtf8Error;
810

911
/// The error type used by this crate.
1012
#[derive(Debug)]
1113
pub enum Error {
1214
/// IO error
13-
Io(::std::io::Error),
15+
Io(IoError),
1416
/// Input decoding error. If `encoding` feature is disabled, contains `None`,
1517
/// otherwise contains the UTF-8 decoding error
1618
NonDecodable(Option<Utf8Error>),
@@ -39,10 +41,10 @@ pub enum Error {
3941
UnknownPrefix(Vec<u8>),
4042
}
4143

42-
impl From<::std::io::Error> for Error {
44+
impl From<IoError> for Error {
4345
/// Creates a new `Error::Io` from the given error
4446
#[inline]
45-
fn from(error: ::std::io::Error) -> Error {
47+
fn from(error: IoError) -> Error {
4648
Error::Io(error)
4749
}
4850
}
@@ -81,8 +83,8 @@ impl From<AttrError> for Error {
8183
/// A specialized `Result` type where the error is hard-wired to [`Error`].
8284
pub type Result<T> = std::result::Result<T, Error>;
8385

84-
impl std::fmt::Display for Error {
85-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
86+
impl fmt::Display for Error {
87+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8688
match self {
8789
Error::Io(e) => write!(f, "I/O error: {}", e),
8890
Error::NonDecodable(None) => write!(f, "Malformed input, decoding impossible"),
@@ -132,7 +134,7 @@ pub mod serialize {
132134
133135
use super::*;
134136
use crate::utils::write_byte_string;
135-
use std::fmt;
137+
use std::borrow::Cow;
136138
#[cfg(feature = "overlapped-lists")]
137139
use std::num::NonZeroUsize;
138140
use std::num::{ParseFloatError, ParseIntError};
@@ -186,7 +188,7 @@ pub mod serialize {
186188
/// An attempt to deserialize to a type, that is not supported by the XML
187189
/// store at current position, for example, attempt to deserialize `struct`
188190
/// from attribute or attempt to deserialize binary data.
189-
Unsupported(&'static str),
191+
Unsupported(Cow<'static, str>),
190192
/// Too many events were skipped while deserializing a sequence, event limit
191193
/// exceeded. The limit was provided as an argument
192194
#[cfg(feature = "overlapped-lists")]
@@ -214,7 +216,7 @@ pub mod serialize {
214216
}
215217
DeError::UnexpectedEof => write!(f, "Unexpected `Event::Eof`"),
216218
DeError::ExpectedStart => write!(f, "Expecting `Event::Start`"),
217-
DeError::Unsupported(s) => write!(f, "Unsupported operation {}", s),
219+
DeError::Unsupported(s) => write!(f, "Unsupported operation: {}", s),
218220
#[cfg(feature = "overlapped-lists")]
219221
DeError::TooManyEvents(s) => write!(f, "Deserializer buffers {} events, limit exceeded", s),
220222
}

src/se/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ impl<'r, 'w, W: Write> ser::Serializer for &'w mut Serializer<'r, W> {
209209
fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, DeError> {
210210
// TODO: I imagine you'd want to use base64 here.
211211
// Not sure how to roundtrip effectively though...
212-
Err(DeError::Unsupported("serialize_bytes"))
212+
Err(DeError::Unsupported(
213+
"`serialize_bytes` not supported yet".into(),
214+
))
213215
}
214216

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

0 commit comments

Comments
 (0)