telegram_bot/
errors.rs

1use std::error;
2use std::fmt;
3
4use telegram_bot_raw;
5
6#[derive(Debug)]
7pub struct Error(ErrorKind);
8
9#[derive(Debug)]
10pub(crate) enum ErrorKind {
11    Raw(telegram_bot_raw::Error),
12    Hyper(hyper::Error),
13    Http(hyper::http::Error),
14    Io(std::io::Error),
15    InvalidMultipartFilename,
16}
17
18impl From<telegram_bot_raw::Error> for ErrorKind {
19    fn from(error: telegram_bot_raw::Error) -> Self {
20        ErrorKind::Raw(error)
21    }
22}
23
24impl From<hyper::Error> for ErrorKind {
25    fn from(error: hyper::Error) -> Self {
26        ErrorKind::Hyper(error)
27    }
28}
29
30impl From<hyper::http::Error> for ErrorKind {
31    fn from(error: hyper::http::Error) -> Self {
32        ErrorKind::Http(error)
33    }
34}
35
36impl From<std::io::Error> for ErrorKind {
37    fn from(error: std::io::Error) -> Self {
38        ErrorKind::Io(error)
39    }
40}
41
42impl From<ErrorKind> for Error {
43    fn from(kind: ErrorKind) -> Self {
44        Error(kind)
45    }
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match &self.0 {
51            ErrorKind::Raw(error) => write!(f, "{}", error),
52            ErrorKind::Hyper(error) => write!(f, "{}", error),
53            ErrorKind::Http(error) => write!(f, "{}", error),
54            ErrorKind::Io(error) => write!(f, "{}", error),
55            ErrorKind::InvalidMultipartFilename => write!(f, "invalid multipart filename"),
56        }
57    }
58}
59
60impl error::Error for Error {}