Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Factor out error trait impls to work without outer Error wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 2, 2021
1 parent b16a401 commit 7ea4775
Showing 1 changed file with 54 additions and 44 deletions.
98 changes: 54 additions & 44 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,51 @@ pub(crate) fn fix_marker(mut error: Error, marker: Marker, path: Path) -> Error

impl error::Error for Error {
// TODO: deprecated, remove in next major version.
fn description(&self) -> &str {
self.0.description()
}

fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.0.source()
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.display(f)
}
}

// Remove two layers of verbosity from the debug representation. Humans often
// end up seeing this representation because it is what unwrap() shows.
impl Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.debug(f)
}
}

impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}

impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}

impl ErrorImpl {
#[allow(deprecated)]
fn description(&self) -> &str {
match self.0.as_ref() {
match self {
ErrorImpl::Message(msg, _) => msg,
ErrorImpl::Emit(_) => "emit error",
ErrorImpl::Scan(_) => "scan error",
ErrorImpl::Io(err) => err.description(),
ErrorImpl::Utf8(err) => err.description(),
ErrorImpl::FromUtf8(err) => err.description(),
ErrorImpl::Io(err) => error::Error::description(err),
ErrorImpl::Utf8(err) => error::Error::description(err),
ErrorImpl::FromUtf8(err) => error::Error::description(err),
ErrorImpl::EndOfStream => "EOF while parsing a value",
ErrorImpl::MoreThanOneDocument => {
"deserializing from YAML containing more than one document is not supported"
Expand All @@ -161,19 +197,17 @@ impl error::Error for Error {
}

fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self.0.as_ref() {
match self {
ErrorImpl::Scan(err) => Some(err),
ErrorImpl::Io(err) => Some(err),
ErrorImpl::Utf8(err) => Some(err),
ErrorImpl::FromUtf8(err) => Some(err),
_ => None,
}
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0.as_ref() {
fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrorImpl::Message(msg, None) => Display::fmt(msg, f),
ErrorImpl::Message(msg, Some(Pos { marker, path })) => {
if path == "." {
Expand All @@ -195,42 +229,18 @@ impl Display for Error {
ErrorImpl::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
}
}
}

// Remove two layers of verbosity from the debug representation. Humans often
// end up seeing this representation because it is what unwrap() shows.
impl Debug for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.0.as_ref() {
ErrorImpl::Message(msg, pos) => formatter
.debug_tuple("Message")
.field(msg)
.field(pos)
.finish(),
ErrorImpl::Emit(emit) => formatter.debug_tuple("Emit").field(emit).finish(),
ErrorImpl::Scan(scan) => formatter.debug_tuple("Scan").field(scan).finish(),
ErrorImpl::Io(io) => formatter.debug_tuple("Io").field(io).finish(),
ErrorImpl::Utf8(utf8) => formatter.debug_tuple("Utf8").field(utf8).finish(),
ErrorImpl::FromUtf8(from_utf8) => {
formatter.debug_tuple("FromUtf8").field(from_utf8).finish()
}
ErrorImpl::EndOfStream => formatter.debug_tuple("EndOfStream").finish(),
ErrorImpl::MoreThanOneDocument => formatter.debug_tuple("MoreThanOneDocument").finish(),
ErrorImpl::RecursionLimitExceeded => {
formatter.debug_tuple("RecursionLimitExceeded").finish()
}
fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrorImpl::Message(msg, pos) => f.debug_tuple("Message").field(msg).field(pos).finish(),
ErrorImpl::Emit(emit) => f.debug_tuple("Emit").field(emit).finish(),
ErrorImpl::Scan(scan) => f.debug_tuple("Scan").field(scan).finish(),
ErrorImpl::Io(io) => f.debug_tuple("Io").field(io).finish(),
ErrorImpl::Utf8(utf8) => f.debug_tuple("Utf8").field(utf8).finish(),
ErrorImpl::FromUtf8(from_utf8) => f.debug_tuple("FromUtf8").field(from_utf8).finish(),
ErrorImpl::EndOfStream => f.debug_tuple("EndOfStream").finish(),
ErrorImpl::MoreThanOneDocument => f.debug_tuple("MoreThanOneDocument").finish(),
ErrorImpl::RecursionLimitExceeded => f.debug_tuple("RecursionLimitExceeded").finish(),
}
}
}

impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}

impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}

0 comments on commit 7ea4775

Please sign in to comment.