use std::borrow::Cow;
#[derive(Clone, Debug)]
pub enum Denial {
Unauthorized(Cow<'static, str>),
Forbidden(Cow<'static, str>),
RateLimited {
retry_after_secs: u32,
reason: Cow<'static, str>,
},
Internal(Cow<'static, str>),
}
impl Denial {
pub fn unauthorized(reason: impl Into<Cow<'static, str>>) -> Self {
Self::Unauthorized(reason.into())
}
pub fn forbidden(reason: impl Into<Cow<'static, str>>) -> Self {
Self::Forbidden(reason.into())
}
pub fn rate_limited(retry_after_secs: u32, reason: impl Into<Cow<'static, str>>) -> Self {
Self::RateLimited {
retry_after_secs,
reason: reason.into(),
}
}
pub fn internal(reason: impl Into<Cow<'static, str>>) -> Self {
Self::Internal(reason.into())
}
pub fn http_status(&self) -> u16 {
match self {
Self::Unauthorized(_) => 401,
Self::Forbidden(_) => 403,
Self::RateLimited { .. } => 429,
Self::Internal(_) => 500,
}
}
pub fn message(&self) -> &str {
match self {
Self::Unauthorized(s) | Self::Forbidden(s) | Self::Internal(s) => s.as_ref(),
Self::RateLimited { reason, .. } => reason.as_ref(),
}
}
}