use std::borrow::Cow;
use zino_core::SharedString;
pub trait ResponseCode {
const OK: Self;
const BAD_REQUEST: Self;
const INTERNAL_SERVER_ERROR: Self;
fn status_code(&self) -> u16;
fn is_success(&self) -> bool;
fn type_uri(&self) -> Option<SharedString> {
None
}
fn title(&self) -> Option<SharedString> {
None
}
fn message(&self) -> Option<SharedString> {
None
}
}
macro_rules! impl_response_code {
($Ty:ty) => {
impl ResponseCode for $Ty {
const OK: Self = Self::OK;
const BAD_REQUEST: Self = Self::BAD_REQUEST;
const INTERNAL_SERVER_ERROR: Self = Self::INTERNAL_SERVER_ERROR;
#[inline]
fn status_code(&self) -> u16 {
self.as_u16()
}
#[inline]
fn is_success(&self) -> bool {
self.is_success()
}
#[inline]
fn type_uri(&self) -> Option<SharedString> {
None
}
#[inline]
fn title(&self) -> Option<SharedString> {
if self.is_success() {
None
} else {
self.canonical_reason().map(Cow::Borrowed)
}
}
#[inline]
fn message(&self) -> Option<SharedString> {
if self.is_success() {
self.canonical_reason().map(Cow::Borrowed)
} else {
None
}
}
}
};
}
impl_response_code!(http::StatusCode);
#[cfg(feature = "http02")]
impl_response_code!(http02::StatusCode);