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 => {
56 write!(fmt, "the end of the document has been reached prematurely")
57 }
58 Error::InvalidElementClosed => {
59 write!(fmt, "the XML is invalid, an element was wrongly closed")
60 }
61 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
62 #[cfg(not(comments))]
63 Error::CommentsDisabled => write!(
64 fmt,
65 "a comment has been found even though comments are disabled by feature"
66 ),
67 }
68 }
69}
70
71impl From<::quick_xml::Error> for Error {
72 fn from(err: ::quick_xml::Error) -> Error {
73 Error::XmlError(err)
74 }
75}
76
77impl From<::std::str::Utf8Error> for Error {
78 fn from(err: ::std::str::Utf8Error) -> Error {
79 Error::Utf8Error(err)
80 }
81}
82
83impl From<::std::io::Error> for Error {
84 fn from(err: ::std::io::Error) -> Error {
85 Error::IoError(err)
86 }
87}
88
89/// Our simplified Result type.
90pub type Result<T> = ::std::result::Result<T, Error>;