1use std::convert::From;
15use std::error::Error as StdError;
16
17#[derive(Debug)]
19pub enum Error {
20 XmlError(::quick_xml::Error),
22
23 Utf8Error(::std::str::Utf8Error),
25
26 IoError(::std::io::Error),
28
29 EndOfDocument,
31
32 InvalidElementClosed,
34
35 InvalidElement,
37
38 InvalidPrefix,
41
42 MissingNamespace,
44
45 DuplicatePrefix,
47}
48
49impl StdError for Error {
50 fn cause(&self) -> Option<&dyn StdError> {
51 match self {
52 Error::XmlError(e) => Some(e),
53 Error::Utf8Error(e) => Some(e),
54 Error::IoError(e) => Some(e),
55 Error::EndOfDocument => None,
56 Error::InvalidElementClosed => None,
57 Error::InvalidElement => None,
58 Error::InvalidPrefix => None,
59 Error::MissingNamespace => None,
60 Error::DuplicatePrefix => None,
61 }
62 }
63}
64
65impl std::fmt::Display for Error {
66 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
67 match self {
68 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
69 Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
70 Error::IoError(e) => write!(fmt, "IO error: {}", e),
71 Error::EndOfDocument => {
72 write!(fmt, "the end of the document has been reached prematurely")
73 }
74 Error::InvalidElementClosed => {
75 write!(fmt, "the XML is invalid, an element was wrongly closed")
76 }
77 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
78 Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
79 Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
80 Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
81 }
82 }
83}
84
85impl From<::quick_xml::Error> for Error {
86 fn from(err: ::quick_xml::Error) -> Error {
87 Error::XmlError(err)
88 }
89}
90
91impl From<::quick_xml::events::attributes::AttrError> for Error {
92 fn from(err: ::quick_xml::events::attributes::AttrError) -> Error {
93 Error::XmlError(err.into())
94 }
95}
96
97impl From<::std::str::Utf8Error> for Error {
98 fn from(err: ::std::str::Utf8Error) -> Error {
99 Error::Utf8Error(err)
100 }
101}
102
103impl From<::std::io::Error> for Error {
104 fn from(err: ::std::io::Error) -> Error {
105 Error::IoError(err)
106 }
107}
108
109pub type Result<T> = ::std::result::Result<T, Error>;