use std::convert::From;
use std::error::Error as StdError;
#[derive(Debug)]
pub enum Error {
XmlError(::quick_xml::Error),
Utf8Error(::std::str::Utf8Error),
IoError(::std::io::Error),
EndOfDocument,
InvalidElementClosed,
InvalidElement,
InvalidPrefix,
MissingNamespace,
NoComments,
DuplicatePrefix,
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match self {
Error::XmlError(e) => Some(e),
Error::Utf8Error(e) => Some(e),
Error::IoError(e) => Some(e),
Error::EndOfDocument => None,
Error::InvalidElementClosed => None,
Error::InvalidElement => None,
Error::InvalidPrefix => None,
Error::MissingNamespace => None,
Error::NoComments => None,
Error::DuplicatePrefix => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::XmlError(e) => write!(fmt, "XML error: {}", e),
Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
Error::IoError(e) => write!(fmt, "IO error: {}", e),
Error::EndOfDocument => {
write!(fmt, "the end of the document has been reached prematurely")
}
Error::InvalidElementClosed => {
write!(fmt, "the XML is invalid, an element was wrongly closed")
}
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
Error::NoComments => write!(
fmt,
"a comment has been found even though comments are forbidden"
),
Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
}
}
}
impl From<::quick_xml::Error> for Error {
fn from(err: ::quick_xml::Error) -> Error {
Error::XmlError(err)
}
}
impl From<::std::str::Utf8Error> for Error {
fn from(err: ::std::str::Utf8Error) -> Error {
Error::Utf8Error(err)
}
}
impl From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Error {
Error::IoError(err)
}
}
pub type Result<T> = ::std::result::Result<T, Error>;