use crate::errors::ParseError;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ElifStatusCode(axum::http::StatusCode);
impl ElifStatusCode {
pub const OK: Self = Self(axum::http::StatusCode::OK);
pub const CREATED: Self = Self(axum::http::StatusCode::CREATED);
pub const ACCEPTED: Self = Self(axum::http::StatusCode::ACCEPTED);
pub const NO_CONTENT: Self = Self(axum::http::StatusCode::NO_CONTENT);
pub const MOVED_PERMANENTLY: Self = Self(axum::http::StatusCode::MOVED_PERMANENTLY);
pub const FOUND: Self = Self(axum::http::StatusCode::FOUND);
pub const SEE_OTHER: Self = Self(axum::http::StatusCode::SEE_OTHER);
pub const NOT_MODIFIED: Self = Self(axum::http::StatusCode::NOT_MODIFIED);
pub const BAD_REQUEST: Self = Self(axum::http::StatusCode::BAD_REQUEST);
pub const UNAUTHORIZED: Self = Self(axum::http::StatusCode::UNAUTHORIZED);
pub const FORBIDDEN: Self = Self(axum::http::StatusCode::FORBIDDEN);
pub const NOT_FOUND: Self = Self(axum::http::StatusCode::NOT_FOUND);
pub const METHOD_NOT_ALLOWED: Self = Self(axum::http::StatusCode::METHOD_NOT_ALLOWED);
pub const PRECONDITION_FAILED: Self = Self(axum::http::StatusCode::PRECONDITION_FAILED);
pub const CONFLICT: Self = Self(axum::http::StatusCode::CONFLICT);
pub const LOCKED: Self = Self(axum::http::StatusCode::LOCKED);
pub const UNPROCESSABLE_ENTITY: Self = Self(axum::http::StatusCode::UNPROCESSABLE_ENTITY);
pub const REQUEST_TIMEOUT: Self = Self(axum::http::StatusCode::REQUEST_TIMEOUT);
pub const PAYLOAD_TOO_LARGE: Self = Self(axum::http::StatusCode::PAYLOAD_TOO_LARGE);
pub const TOO_MANY_REQUESTS: Self = Self(axum::http::StatusCode::TOO_MANY_REQUESTS);
pub const INTERNAL_SERVER_ERROR: Self = Self(axum::http::StatusCode::INTERNAL_SERVER_ERROR);
pub const NOT_IMPLEMENTED: Self = Self(axum::http::StatusCode::NOT_IMPLEMENTED);
pub const BAD_GATEWAY: Self = Self(axum::http::StatusCode::BAD_GATEWAY);
pub const SERVICE_UNAVAILABLE: Self = Self(axum::http::StatusCode::SERVICE_UNAVAILABLE);
pub fn from_u16(src: u16) -> Result<Self, ParseError> {
axum::http::StatusCode::from_u16(src)
.map(Self)
.map_err(|_| ParseError::invalid_status_code(src))
}
pub fn as_u16(&self) -> u16 {
self.0.as_u16()
}
pub fn is_informational(&self) -> bool {
self.0.is_informational()
}
pub fn is_success(&self) -> bool {
self.0.is_success()
}
pub fn is_redirection(&self) -> bool {
self.0.is_redirection()
}
pub fn is_client_error(&self) -> bool {
self.0.is_client_error()
}
pub fn is_server_error(&self) -> bool {
self.0.is_server_error()
}
pub(crate) fn to_axum(&self) -> axum::http::StatusCode {
self.0
}
pub(crate) fn from_axum(status: axum::http::StatusCode) -> Self {
Self(status)
}
}
impl From<u16> for ElifStatusCode {
fn from(src: u16) -> Self {
Self::from_u16(src).expect("Invalid status code")
}
}
impl From<ElifStatusCode> for u16 {
fn from(status: ElifStatusCode) -> u16 {
status.as_u16()
}
}
impl fmt::Display for ElifStatusCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}