Skip to content

Commit

Permalink
Use ? operator instead of map_err
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jun 19, 2022
1 parent 2f39331 commit 93d412f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/de/mod.rs
Expand Up @@ -624,7 +624,7 @@ where
allow_start: bool,
) -> Result<BytesCData<'de>, DeError> {
match self.next()? {
DeEvent::Text(e) if unescape => e.unescape().map_err(|e| DeError::InvalidXml(e.into())),
DeEvent::Text(e) if unescape => e.unescape().map_err(Into::into),
DeEvent::Text(e) => Ok(BytesCData::new(e.into_inner())),
DeEvent::CData(e) => Ok(e),
DeEvent::Start(e) if allow_start => {
Expand Down
38 changes: 32 additions & 6 deletions src/errors.rs
Expand Up @@ -4,6 +4,7 @@ use crate::escape::EscapeError;
use crate::events::attributes::AttrError;
use crate::utils::write_byte_string;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

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

impl From<FromUtf8Error> for Error {
/// Creates a new `Error::Utf8` from the given error
#[inline]
fn from(error: FromUtf8Error) -> Error {
error.utf8_error().into()
}
}

impl From<EscapeError> for Error {
/// Creates a new `Error::EscapeError` from the given error
#[inline]
Expand Down Expand Up @@ -227,6 +236,7 @@ pub mod serialize {
}

impl From<Error> for DeError {
#[inline]
fn from(e: Error) -> Self {
Self::InvalidXml(e)
}
Expand All @@ -239,15 +249,17 @@ pub mod serialize {
}
}

impl From<ParseIntError> for DeError {
fn from(e: ParseIntError) -> Self {
Self::InvalidInt(e)
impl From<Utf8Error> for DeError {
#[inline]
fn from(e: Utf8Error) -> Self {
Self::InvalidXml(e.into())
}
}

impl From<ParseFloatError> for DeError {
fn from(e: ParseFloatError) -> Self {
Self::InvalidFloat(e)
impl From<FromUtf8Error> for DeError {
#[inline]
fn from(e: FromUtf8Error) -> Self {
Self::InvalidXml(e.into())
}
}

Expand All @@ -257,4 +269,18 @@ pub mod serialize {
Self::InvalidXml(e.into())
}
}

impl From<ParseIntError> for DeError {
#[inline]
fn from(e: ParseIntError) -> Self {
Self::InvalidInt(e)
}
}

impl From<ParseFloatError> for DeError {
#[inline]
fn from(e: ParseFloatError) -> Self {
Self::InvalidFloat(e)
}
}
}
5 changes: 2 additions & 3 deletions src/events/attributes.rs
Expand Up @@ -119,9 +119,8 @@ impl<'a> Attribute<'a> {
#[cfg(not(feature = "encoding"))]
let decoded = reader.decoder().decode(&*self.value)?;

let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
Ok(String::from_utf8(unescaped.into_owned())?)
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/events/mod.rs
Expand Up @@ -323,9 +323,8 @@ impl<'a> BytesStart<'a> {
#[cfg(not(feature = "encoding"))]
let decoded = reader.decoder().decode(&*self)?;

let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
Ok(String::from_utf8(unescaped.into_owned())?)
}

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

let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
let unescaped = do_unescape(decoded.as_bytes(), custom_entities)?;
Ok(String::from_utf8(unescaped.into_owned())?)
}

/// Gets escaped content.
Expand Down
11 changes: 6 additions & 5 deletions src/reader.rs
Expand Up @@ -1444,7 +1444,7 @@ impl Decoder {
///
/// If you instead want to use XML declared encoding, use the `encoding` feature
pub fn decode<'c>(&self, bytes: &'c [u8]) -> Result<&'c str> {
from_utf8(bytes).map_err(Error::Utf8)
Ok(from_utf8(bytes)?)
}

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

Expand Down
3 changes: 1 addition & 2 deletions src/se/mod.rs
Expand Up @@ -23,8 +23,7 @@ pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> Result<(), DeE
pub fn to_string<S: Serialize>(value: &S) -> Result<String, DeError> {
let mut writer = Vec::new();
to_writer(&mut writer, value)?;
let s = String::from_utf8(writer).map_err(|e| crate::errors::Error::Utf8(e.utf8_error()))?;
Ok(s)
Ok(String::from_utf8(writer)?)
}

/// A Serializer
Expand Down

0 comments on commit 93d412f

Please sign in to comment.