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
use failure::{Backtrace, Context, Error, Fail};
use std::fmt;

#[derive(Debug)]
pub struct FeedParserError {
    inner: Context<FeedParserErrorKind>,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum FeedParserErrorKind {
    #[fail(display = "Failed to parse an Url")]
    Url,
    #[fail(display = "Failed to parse bytes to valid utf8")]
    Utf8,
    #[fail(display = "Http request failed")]
    Http,
    #[fail(display = "Failed to parse feed url from HTML")]
    Html,
    #[fail(display = "Failed to parse feed")]
    Feed,
    #[fail(display = "Unknown Error")]
    Unknown,
}

impl Fail for FeedParserError {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for FeedParserError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl FeedParserError {
    #[allow(dead_code)]
    pub fn kind(&self) -> FeedParserErrorKind {
        *self.inner.get_context()
    }
}

impl From<FeedParserErrorKind> for FeedParserError {
    fn from(kind: FeedParserErrorKind) -> FeedParserError {
        FeedParserError { inner: Context::new(kind) }
    }
}

impl From<Context<FeedParserErrorKind>> for FeedParserError {
    fn from(inner: Context<FeedParserErrorKind>) -> FeedParserError {
        FeedParserError { inner }
    }
}

impl From<Error> for FeedParserError {
    fn from(_: Error) -> FeedParserError {
        FeedParserError {
            inner: Context::new(FeedParserErrorKind::Unknown),
        }
    }
}