Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handwrite Error conformance so thiserror is not needed #18

Merged
merged 3 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
54 changes: 45 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
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> {
SSheldon marked this conversation as resolved.
Show resolved Hide resolved
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),
SSheldon marked this conversation as resolved.
Show resolved Hide resolved
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<XmlError> for Error {
fn from(err: XmlError) -> Error {
Error::Xml(err)
}
}

impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Error {
Error::Utf8(err)
}
}