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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use {
    isahc::http::uri::InvalidUri,
    std::{error, fmt, io, str::Utf8Error},
};

#[derive(Debug)]
pub enum Error {
    Scrape(ScrapeError),
    Request(RequestError),

    Uri(InvalidUri),

    Epub(epub_builder::Error),
    Json(serde_json::Error),
    MessagePack(rmps::encode::Error),

    Canceled,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Scrape(ref err) => err.fmt(f),
            Error::Request(ref err) => err.fmt(f),

            Error::Uri(ref err) => err.fmt(f),

            Error::Epub(ref err) => err.fmt(f),
            Error::Json(ref err) => err.fmt(f),
            Error::MessagePack(ref err) => err.fmt(f),

            Error::Canceled => write!(f, "Thread pool is gone"),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::Scrape(ref err) => err.source(),
            Error::Request(ref err) => err.source(),

            Error::Uri(ref err) => err.source(),

            Error::Epub(ref err) => err.source(),
            Error::Json(ref err) => err.source(),
            Error::MessagePack(ref err) => err.source(),

            Error::Canceled => None,
        }
    }
}

impl From<ScrapeError> for Error {
    fn from(err: ScrapeError) -> Error {
        Error::Scrape(err)
    }
}

impl From<RequestError> for Error {
    fn from(err: RequestError) -> Error {
        Error::Request(err)
    }
}

impl From<InvalidUri> for Error {
    fn from(err: InvalidUri) -> Error {
        Error::Uri(err)
    }
}

impl From<epub_builder::Error> for Error {
    fn from(err: epub_builder::Error) -> Error {
        Error::Epub(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::Json(err)
    }
}

impl From<rmps::encode::Error> for Error {
    fn from(err: rmps::encode::Error) -> Error {
        Error::MessagePack(err)
    }
}

#[derive(Debug)]
pub enum ScrapeError {
    Io(io::Error),
    Utf8(Utf8Error),
}

impl fmt::Display for ScrapeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ScrapeError::Io(ref err) => err.fmt(f),
            ScrapeError::Utf8(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for ScrapeError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

impl From<io::Error> for ScrapeError {
    fn from(err: io::Error) -> ScrapeError {
        ScrapeError::Io(err)
    }
}

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

#[derive(Debug)]
pub enum RequestError {
    Http(isahc::http::Error),
    Io(io::Error),
    Isahc(isahc::Error),
}

impl fmt::Display for RequestError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RequestError::Http(ref err) => err.fmt(f),
            RequestError::Io(ref err) => err.fmt(f),
            RequestError::Isahc(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for RequestError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            RequestError::Http(ref err) => Some(err),
            RequestError::Io(ref err) => Some(err),
            RequestError::Isahc(ref err) => Some(err),
        }
    }
}

impl From<isahc::http::Error> for RequestError {
    fn from(err: isahc::http::Error) -> RequestError {
        RequestError::Http(err)
    }
}

impl From<io::Error> for RequestError {
    fn from(err: io::Error) -> RequestError {
        RequestError::Io(err)
    }
}

impl From<isahc::Error> for RequestError {
    fn from(err: isahc::Error) -> RequestError {
        RequestError::Isahc(err)
    }
}