rss/
error.rs

1use std::fmt;
2use std::error::Error as StdError;
3use std::str::Utf8Error;
4use std::string::FromUtf8Error;
5
6use quick_xml::error::Error as XmlError;
7
8#[derive(Debug)]
9/// Types of errors that could occur while parsing an RSS feed.
10pub enum Error {
11    /// An error occurred while converting bytes to UTF8.
12    Utf8(Utf8Error),
13    /// An XML parser error occurred.
14    XmlParsing(XmlError, usize),
15    /// The end of the input was reached without finding a complete channel element.
16    EOF,
17}
18
19impl StdError for Error {
20    fn description(&self) -> &str {
21        match *self {
22            Error::Utf8(ref err) => err.description(),
23            Error::XmlParsing(ref err, _) => err.description(),
24            Error::EOF => "reached end of input without finding a complete channel",
25        }
26    }
27
28    fn cause(&self) -> Option<&StdError> {
29        match *self {
30            Error::Utf8(ref err) => Some(err),
31            Error::XmlParsing(ref err, _) => Some(err),
32            _ => None,
33        }
34    }
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match *self {
40            Error::Utf8(ref err) => fmt::Display::fmt(err, f),
41            Error::XmlParsing(ref err, _) => fmt::Display::fmt(err, f),
42            Error::EOF => write!(f, "reached end of input without finding a complete channel"),
43        }
44    }
45}
46
47impl From<(XmlError, usize)> for Error {
48    fn from(err: (XmlError, usize)) -> Error {
49        Error::XmlParsing(err.0, err.1)
50    }
51}
52
53impl From<Utf8Error> for Error {
54    fn from(err: Utf8Error) -> Error {
55        Error::Utf8(err)
56    }
57}
58
59impl From<FromUtf8Error> for Error {
60    fn from(err: FromUtf8Error) -> Error {
61        Error::Utf8(err.utf8_error())
62    }
63}