From 11a5c49470d1e3d8875a7ccefc0cbd34855eafe7 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 7 Sep 2019 16:02:40 +0200 Subject: [PATCH] Implement std::error::Error for Error. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was removed in 0.11.1 with the removal of failure, but is used by people so let’s reintroduce it. The cause of an XmlError is pending on this PR from quick-xml: https://github.com/tafia/quick-xml/pull/170 Fixes #15. Fixes #18. --- src/error.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/error.rs b/src/error.rs index fc96a08bd1b69638cf3a5c0580a80e02d7b48bac..579cee405aca8c68af1aa1f97dd0ff02329774df 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ //! Provides an error type for this crate. use std::convert::From; +use std::error::Error as StdError; /// Our main error type. #[derive(Debug)] @@ -28,6 +29,23 @@ pub enum Error { CommentsDisabled, } +impl StdError for Error { + fn cause(&self) -> Option<&dyn StdError> { + match self { + // TODO: return Some(e) for this case after the merge of + // https://github.com/tafia/quick-xml/pull/170 + Error::XmlError(_e) => None, + Error::Utf8Error(e) => Some(e), + Error::IoError(e) => Some(e), + Error::EndOfDocument => None, + Error::InvalidElementClosed => None, + Error::InvalidElement => None, + #[cfg(not(comments))] + Error::CommentsDisabled => None, + } + } +} + impl std::fmt::Display for Error { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { match self {