Skip to content

Commit be9ec0c

Browse files
committed
Use ? operator instead of map_err
1 parent 8d5aaaa commit be9ec0c

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

src/de/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ where
624624
allow_start: bool,
625625
) -> Result<BytesCData<'de>, DeError> {
626626
match self.next()? {
627-
DeEvent::Text(e) if unescape => e.unescape().map_err(|e| DeError::InvalidXml(e.into())),
627+
DeEvent::Text(e) if unescape => e.unescape().map_err(Into::into),
628628
DeEvent::Text(e) => Ok(BytesCData::new(e.into_inner())),
629629
DeEvent::CData(e) => Ok(e),
630630
DeEvent::Start(e) if allow_start => {

src/errors.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::escape::EscapeError;
44
use crate::events::attributes::AttrError;
55
use crate::utils::write_byte_string;
66
use std::str::Utf8Error;
7+
use std::string::FromUtf8Error;
78

89
/// The error type used by this crate.
910
#[derive(Debug)]
@@ -53,6 +54,14 @@ impl From<Utf8Error> for Error {
5354
}
5455
}
5556

57+
impl From<FromUtf8Error> for Error {
58+
/// Creates a new `Error::Utf8` from the given error
59+
#[inline]
60+
fn from(error: FromUtf8Error) -> Error {
61+
error.utf8_error().into()
62+
}
63+
}
64+
5665
impl From<EscapeError> for Error {
5766
/// Creates a new `Error::EscapeError` from the given error
5867
#[inline]
@@ -227,6 +236,7 @@ pub mod serialize {
227236
}
228237

229238
impl From<Error> for DeError {
239+
#[inline]
230240
fn from(e: Error) -> Self {
231241
Self::InvalidXml(e)
232242
}
@@ -239,15 +249,17 @@ pub mod serialize {
239249
}
240250
}
241251

242-
impl From<ParseIntError> for DeError {
243-
fn from(e: ParseIntError) -> Self {
244-
Self::InvalidInt(e)
252+
impl From<Utf8Error> for DeError {
253+
#[inline]
254+
fn from(e: Utf8Error) -> Self {
255+
Self::InvalidXml(e.into())
245256
}
246257
}
247258

248-
impl From<ParseFloatError> for DeError {
249-
fn from(e: ParseFloatError) -> Self {
250-
Self::InvalidFloat(e)
259+
impl From<FromUtf8Error> for DeError {
260+
#[inline]
261+
fn from(e: FromUtf8Error) -> Self {
262+
Self::InvalidXml(e.into())
251263
}
252264
}
253265

@@ -257,4 +269,18 @@ pub mod serialize {
257269
Self::InvalidXml(e.into())
258270
}
259271
}
272+
273+
impl From<ParseIntError> for DeError {
274+
#[inline]
275+
fn from(e: ParseIntError) -> Self {
276+
Self::InvalidInt(e)
277+
}
278+
}
279+
280+
impl From<ParseFloatError> for DeError {
281+
#[inline]
282+
fn from(e: ParseFloatError) -> Self {
283+
Self::InvalidFloat(e)
284+
}
285+
}
260286
}

src/events/attributes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ impl<'a> Attribute<'a> {
119119
#[cfg(not(feature = "encoding"))]
120120
let decoded = reader.decoder().decode(&*self.value)?;
121121

122-
let unescaped =
123-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
124-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
122+
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
123+
Ok(String::from_utf8(unescaped.into_owned())?)
125124
}
126125
}
127126

src/events/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,8 @@ impl<'a> BytesStart<'a> {
323323
#[cfg(not(feature = "encoding"))]
324324
let decoded = reader.decoder().decode(&*self)?;
325325

326-
let unescaped =
327-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
328-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
326+
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
327+
Ok(String::from_utf8(unescaped.into_owned())?)
329328
}
330329

331330
/// Edit the name of the BytesStart in-place
@@ -512,7 +511,7 @@ impl<'a> BytesDecl<'a> {
512511
Some(Ok(a)) if a.key.as_ref() == b"version" => Ok(a.value),
513512
// first attribute was not "version"
514513
Some(Ok(a)) => {
515-
let found = from_utf8(a.key.as_ref()).map_err(Error::Utf8)?.to_string();
514+
let found = from_utf8(a.key.as_ref())?.to_string();
516515
Err(Error::XmlDeclWithoutVersion(Some(found)))
517516
}
518517
// error parsing attributes
@@ -887,9 +886,8 @@ impl<'a> BytesText<'a> {
887886
#[cfg(not(feature = "encoding"))]
888887
let decoded = reader.decoder().decode(&*self)?;
889888

890-
let unescaped =
891-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
892-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
889+
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
890+
Ok(String::from_utf8(unescaped.into_owned())?)
893891
}
894892

895893
/// Gets escaped content.

src/reader.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ impl Decoder {
14731473
///
14741474
/// If you instead want to use XML declared encoding, use the `encoding` feature
14751475
pub fn decode<'c>(&self, bytes: &'c [u8]) -> Result<&'c str> {
1476-
from_utf8(bytes).map_err(Error::Utf8)
1476+
Ok(from_utf8(bytes)?)
14771477
}
14781478

14791479
/// Decodes a slice regardless of XML declaration with BOM removal if
@@ -1483,11 +1483,12 @@ impl Decoder {
14831483
///
14841484
/// If you instead want to use XML declared encoding, use the `encoding` feature
14851485
pub fn decode_with_bom_removal<'b>(&self, bytes: &'b [u8]) -> Result<&'b str> {
1486-
if bytes.starts_with(b"\xEF\xBB\xBF") {
1487-
from_utf8(&bytes[3..]).map_err(Error::Utf8)
1486+
let bytes = if bytes.starts_with(b"\xEF\xBB\xBF") {
1487+
&bytes[3..]
14881488
} else {
1489-
from_utf8(bytes).map_err(Error::Utf8)
1490-
}
1489+
bytes
1490+
};
1491+
self.decode(bytes)
14911492
}
14921493
}
14931494

src/se/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> Result<(), DeE
2323
pub fn to_string<S: Serialize>(value: &S) -> Result<String, DeError> {
2424
let mut writer = Vec::new();
2525
to_writer(&mut writer, value)?;
26-
let s = String::from_utf8(writer).map_err(|e| crate::errors::Error::Utf8(e.utf8_error()))?;
27-
Ok(s)
26+
Ok(String::from_utf8(writer)?)
2827
}
2928

3029
/// A Serializer

0 commit comments

Comments
 (0)