From 9d1ef340465c57bd29dc7415b66918539e2ce5e1 Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Sun, 29 Dec 2019 15:18:54 -0800 Subject: [PATCH 1/3] Remove dependency on thiserror --- Cargo.toml | 1 - src/error.rs | 53 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 06db597..3740fa1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ quick-xml = { version = "0.17", features = ["encoding"] } derive_builder = "0.9" serde = { version = "1.0", optional = true, features = ["derive"] } chrono = "0.4" -thiserror = "1.0" [features] with-serde = ["serde", "chrono/serde"] diff --git a/src/error.rs b/src/error.rs index 0ad3333..d79d026 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,24 +1,59 @@ +use std::error::Error as StdError; +use std::fmt; use std::str::Utf8Error; use quick_xml::Error as XmlError; -use thiserror::Error; -#[derive(Debug, Error)] +#[derive(Debug)] /// An error that occurred while performing an Atom operation. pub enum Error { /// Unable to parse XML. - #[error("{0}")] - Xml(#[from] XmlError), + Xml(XmlError), /// Unable to parse UTF8 in to a string. - #[error("{0}")] - Utf8(#[from] Utf8Error), + Utf8(Utf8Error), /// Input did not begin with an opening feed tag. - #[error("input did not begin with an opening feed tag")] InvalidStartTag, /// Unexpected end of input. - #[error("unexpected end of input")] Eof, /// The format of the timestamp is wrong. - #[error("timestamps must be formatted by RFC3339, rather than {0}")] WrongDatetime(String), } + +impl StdError for Error { + fn cause(&self) -> Option<&dyn StdError> { + match *self { + Error::Xml(ref err) => Some(err), + Error::Utf8(ref err) => Some(err), + Error::InvalidStartTag => None, + Error::Eof => None, + Error::WrongDatetime(_) => None, + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Xml(ref err) => err.fmt(f), + Error::Utf8(ref err) => err.fmt(f), + Error::InvalidStartTag => + write!(f, "input did not begin with an opening feed tag"), + Error::Eof => + write!(f, "unexpected end of input"), + Error::WrongDatetime(ref datetime) => + write!(f, "timestamps must be formatted by RFC3339, rather than {}", datetime), + } + } +} + +impl From for Error { + fn from(err: XmlError) -> Error { + Error::Xml(err) + } +} + +impl From for Error { + fn from(err: Utf8Error) -> Error { + Error::Utf8(err) + } +} From e7b6800965341332c6c35819066f76988926d448 Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Mon, 30 Dec 2019 20:27:06 -0800 Subject: [PATCH 2/3] Conform to rustfmt --- src/error.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index d79d026..f19ab2f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -36,12 +36,13 @@ impl fmt::Display for Error { match *self { Error::Xml(ref err) => err.fmt(f), Error::Utf8(ref err) => err.fmt(f), - Error::InvalidStartTag => - write!(f, "input did not begin with an opening feed tag"), - Error::Eof => - write!(f, "unexpected end of input"), - Error::WrongDatetime(ref datetime) => - write!(f, "timestamps must be formatted by RFC3339, rather than {}", datetime), + Error::InvalidStartTag => write!(f, "input did not begin with an opening feed tag"), + Error::Eof => write!(f, "unexpected end of input"), + Error::WrongDatetime(ref datetime) => write!( + f, + "timestamps must be formatted by RFC3339, rather than {}", + datetime + ), } } } From 081b5634b36790ff94fc711aa2c9e591189c52fb Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Fri, 3 Jan 2020 09:26:33 -0800 Subject: [PATCH 3/3] Use newer StdError API and ensure no ambiguity in fmt --- src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index f19ab2f..8fdfeae 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,7 +20,7 @@ pub enum Error { } impl StdError for Error { - fn cause(&self) -> Option<&dyn StdError> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match *self { Error::Xml(ref err) => Some(err), Error::Utf8(ref err) => Some(err), @@ -34,8 +34,8 @@ impl StdError for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::Xml(ref err) => err.fmt(f), - Error::Utf8(ref err) => err.fmt(f), + Error::Xml(ref err) => fmt::Display::fmt(err, f), + Error::Utf8(ref err) => fmt::Display::fmt(err, f), Error::InvalidStartTag => write!(f, "input did not begin with an opening feed tag"), Error::Eof => write!(f, "unexpected end of input"), Error::WrongDatetime(ref datetime) => write!(