error.rs

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