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

/// Represents possible errors that a webhook server may return.
#[derive(Debug, Is)]
pub enum HttpWebhook {
    /// An error while setting the webhook.
    SetWebhook(MethodCall),
    /// Calling the `setWebhook` method timed out.
    SetWebhookTimeout(Elapsed),
    /// Calling the `setMyCommands` method resulted in an error.
    SetMyCommands(MethodCall),
    /// Calling the `setMyCommands` method timed out.
    SetMyCommandsTimeout(Elapsed),
    /// An error while running the server.
    Server(hyper::Error),
}

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

impl Error for HttpWebhook {}

impl From<MethodCall> for HttpWebhook {
    #[must_use]
    fn from(error: MethodCall) -> Self {
        Self::SetWebhook(error)
    }
}

impl From<Elapsed> for HttpWebhook {
    #[must_use]
    fn from(timeout: Elapsed) -> Self {
        Self::SetWebhookTimeout(timeout)
    }
}

impl From<hyper::Error> for HttpWebhook {
    #[must_use]
    fn from(error: hyper::Error) -> Self {
        Self::Server(error)
    }
}