1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::fmt;
use std::error::Error as StdError;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

use quick_xml::error::Error as XmlError;

#[derive(Debug)]
/// Types of errors that could occur while parsing an RSS feed.
pub enum Error {
    /// An error occurred while converting bytes to UTF8.
    Utf8(Utf8Error),
    /// An XML parser error occurred at the specified byte offset.
    XmlParsing(XmlError, usize),
    /// An XML error occurred.
    Xml(XmlError),
    /// The end of the input was reached without finding a complete channel element.
    EOF,
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Utf8(ref err) => err.description(),
            Error::XmlParsing(ref err, _) => err.description(),
            Error::Xml(ref err) => err.description(),
            Error::EOF => "reached end of input without finding a complete channel",
        }
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::Utf8(ref err) => Some(err),
            Error::XmlParsing(ref err, _) => Some(err),
            Error::Xml(ref err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Utf8(ref err) => fmt::Display::fmt(err, f),
            Error::XmlParsing(ref err, _) => fmt::Display::fmt(err, f),
            Error::Xml(ref err) => fmt::Display::fmt(err, f),
            Error::EOF => write!(f, "reached end of input without finding a complete channel"),
        }
    }
}

impl From<(XmlError, usize)> for Error {
    fn from(err: (XmlError, usize)) -> Error {
        Error::XmlParsing(err.0, err.1)
    }
}

impl From<XmlError> for Error {
    fn from(err: XmlError) -> Error {
        Error::Xml(err)
    }
}

impl From<Utf8Error> for Error {
    fn from(err: Utf8Error) -> Error {
        Error::Utf8(err)
    }
}

impl From<FromUtf8Error> for Error {
    fn from(err: FromUtf8Error) -> Error {
        Error::Utf8(err.utf8_error())
    }
}