Implement std::error::Error for Error.

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.
This commit is contained in:
Emmanuel Gil Peyrot 2019-09-07 16:02:40 +02:00
parent 0db94e554d
commit 11a5c49470

View file

@ -1,6 +1,7 @@
//! Provides an error type for this crate. //! Provides an error type for this crate.
use std::convert::From; use std::convert::From;
use std::error::Error as StdError;
/// Our main error type. /// Our main error type.
#[derive(Debug)] #[derive(Debug)]
@ -28,6 +29,23 @@ pub enum Error {
CommentsDisabled, 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 { impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {