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
use crate::errors::MethodCall;
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
};
use tokio::timer::timeout;

type Timeout = timeout::Error<MethodCall>;

/// Represents possible errors that a webhook server may return.
#[derive(Debug)]
pub enum Webhook {
    /// An error during setting the webhook.
    SetWebhook(Timeout),
    /// An error while running the server.
    Server(hyper::Error),
}

impl Display for Webhook {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            Webhook::SetWebhook(timeout) => write!(
                formatter,
                "The webhook event loop failed because a call to `setWebhook` \
                 failed with an error: {}",
                timeout,
            ),
            Webhook::Server(error) => write!(
                formatter,
                "The webhook event loop failed because the server returned \
                 with an error: {}",
                error,
            ),
        }
    }
}

impl Error for Webhook {}

impl Webhook {
    /// Checks if `self` is `SetWebhook`.
    pub fn is_set_webhook(&self) -> bool {
        match self {
            Webhook::SetWebhook(..) => true,
            _ => false,
        }
    }

    /// Checks if `self` is `Server`.
    pub fn is_server(&self) -> bool {
        match self {
            Webhook::Server(..) => true,
            _ => false,
        }
    }
}

impl From<Timeout> for Webhook {
    fn from(error: Timeout) -> Self {
        Webhook::SetWebhook(error)
    }
}

impl From<hyper::Error> for Webhook {
    fn from(error: hyper::Error) -> Self {
        Webhook::Server(error)
    }
}