use super::MethodCall;
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
use tokio::time::Elapsed;
#[derive(Debug)]
pub enum HttpsWebhook {
SetWebhook(MethodCall),
SetWebhookTimeout(Elapsed),
Tls(native_tls::Error),
Bind(std::io::Error),
Server(hyper::Error),
}
impl HttpsWebhook {
#[must_use]
pub fn is_set_webhook(&self) -> bool {
match self {
Self::SetWebhook(..) => true,
_ => false,
}
}
#[must_use]
pub fn is_set_webhook_timeout(&self) -> bool {
match self {
Self::SetWebhookTimeout(..) => true,
_ => false,
}
}
#[must_use]
pub fn is_tls(&self) -> bool {
match self {
Self::Tls(..) => true,
_ => false,
}
}
#[must_use]
pub fn is_bind(&self) -> bool {
match self {
Self::Bind(..) => true,
_ => false,
}
}
#[must_use]
pub fn is_server(&self) -> bool {
match self {
Self::Server(..) => true,
_ => false,
}
}
}
impl Display for HttpsWebhook {
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::Tls(timeout) => write!(
formatter,
"The webhook event loop failed because TLS initialization \
failed with an error: {}",
timeout,
),
Self::Bind(timeout) => write!(
formatter,
"The webhook event loop failed because failed to bind to a \
port: {}",
timeout,
),
Self::Server(error) => write!(
formatter,
"The webhook event loop failed because the server returned \
with an error: {}",
error,
),
}
}
}
impl Error for HttpsWebhook {}
impl From<MethodCall> for HttpsWebhook {
#[must_use]
fn from(error: MethodCall) -> Self {
Self::SetWebhook(error)
}
}
impl From<Elapsed> for HttpsWebhook {
#[must_use]
fn from(error: Elapsed) -> Self {
Self::SetWebhookTimeout(error)
}
}
impl From<native_tls::Error> for HttpsWebhook {
#[must_use]
fn from(error: native_tls::Error) -> Self {
Self::Tls(error)
}
}
impl From<std::io::Error> for HttpsWebhook {
#[must_use]
fn from(error: std::io::Error) -> Self {
Self::Bind(error)
}
}
impl From<hyper::Error> for HttpsWebhook {
#[must_use]
fn from(error: hyper::Error) -> Self {
Self::Server(error)
}
}