use core::convert::Infallible;
use std::io::{Error as IoError, ErrorKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HttpStatus(u16);
impl HttpStatus {
pub const OK: Self = Self(200);
pub const NO_CONTENT: Self = Self(204);
pub const SEE_OTHER: Self = Self(303);
pub const BAD_REQUEST: Self = Self(400);
pub const FORBIDDEN: Self = Self(403);
pub const NOT_FOUND: Self = Self(404);
pub const METHOD_NOT_ALLOWED: Self = Self(405);
pub const INTERNAL: Self = Self(500);
pub const fn new(code: u16) -> Self {
Self(code)
}
pub const fn code(self) -> u16 {
self.0
}
pub const fn is_success(self) -> bool {
200 <= self.0 && self.0 < 300
}
}
pub trait HttpErrorAlg {
const HTTP_STATUSES: &'static [HttpStatus];
fn http_status(&self) -> HttpStatus;
fn http_message(&self) -> String;
}
impl HttpErrorAlg for Infallible {
const HTTP_STATUSES: &'static [HttpStatus] = &[];
fn http_status(&self) -> HttpStatus {
match *self {}
}
fn http_message(&self) -> String {
match *self {}
}
}
impl HttpErrorAlg for IoError {
const HTTP_STATUSES: &'static [HttpStatus] = &[HttpStatus::NOT_FOUND, HttpStatus::FORBIDDEN, HttpStatus::INTERNAL];
fn http_status(&self) -> HttpStatus {
match self.kind() {
ErrorKind::NotFound => HttpStatus::NOT_FOUND,
ErrorKind::PermissionDenied => HttpStatus::FORBIDDEN,
_ => HttpStatus::INTERNAL,
}
}
fn http_message(&self) -> String {
self.to_string()
}
}