1//! Provides an error type for this crate.
2
3use std::convert::From;
4use std::error::Error as StdError;
5
6/// Our main error type.
7#[derive(Debug)]
8pub enum Error {
9 /// An error from quick_xml.
10 XmlError(::quick_xml::Error),
11
12 /// An UTF-8 conversion error.
13 Utf8Error(::std::str::Utf8Error),
14
15 /// An I/O error, from std::io.
16 IoError(::std::io::Error),
17
18 /// An error which is returned when the end of the document was reached prematurely.
19 EndOfDocument,
20
21 /// An error which is returned when an element is closed when it shouldn't be
22 InvalidElementClosed,
23
24 /// An error which is returned when an elemet's name contains more than one colon
25 InvalidElement,
26
27 /// An error which is returned when a comment is to be parsed by minidom
28 #[cfg(not(comments))]
29 CommentsDisabled,
30}
31
32impl StdError for Error {
33 fn cause(&self) -> Option<&dyn StdError> {
34 match self {
35 // TODO: return Some(e) for this case after the merge of
36 // https://github.com/tafia/quick-xml/pull/170
37 Error::XmlError(_e) => None,
38 Error::Utf8Error(e) => Some(e),
39 Error::IoError(e) => Some(e),
40 Error::EndOfDocument => None,
41 Error::InvalidElementClosed => None,
42 Error::InvalidElement => None,
43 #[cfg(not(comments))]
44 Error::CommentsDisabled => None,
45 }
46 }
47}
48
49impl std::fmt::Display for Error {
50 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
51 match self {
52 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
53 Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
54 Error::IoError(e) => write!(fmt, "IO error: {}", e),
55 Error::EndOfDocument => write!(fmt, "the end of the document has been reached prematurely"),
56 Error::InvalidElementClosed => write!(fmt, "the XML is invalid, an element was wrongly closed"),
57 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
58 #[cfg(not(comments))]
59 Error::CommentsDisabled => write!(fmt, "a comment has been found even though comments are disabled by feature"),
60 }
61 }
62}
63
64impl From<::quick_xml::Error> for Error {
65 fn from(err: ::quick_xml::Error) -> Error {
66 Error::XmlError(err)
67 }
68}
69
70impl From<::std::str::Utf8Error> for Error {
71 fn from(err: ::std::str::Utf8Error) -> Error {
72 Error::Utf8Error(err)
73 }
74}
75
76impl From<::std::io::Error> for Error {
77 fn from(err: ::std::io::Error) -> Error {
78 Error::IoError(err)
79 }
80}
81
82/// Our simplified Result type.
83pub type Result<T> = ::std::result::Result<T, Error>;