Implement std::error::Error for Error.

Emmanuel Gil Peyrot created

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.

Change summary

src/error.rs | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

Detailed changes

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 {