#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HttpStatus {
pub code: u16,
pub reason: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StatusClass {
Informational,
Success,
Redirection,
ClientError,
ServerError,
}
#[must_use]
pub const fn reason_phrase(code: u16) -> Option<&'static str> {
match code {
100 => Some("Continue"),
101 => Some("Switching Protocols"),
102 => Some("Processing"),
103 => Some("Early Hints"),
200 => Some("OK"),
201 => Some("Created"),
202 => Some("Accepted"),
204 => Some("No Content"),
206 => Some("Partial Content"),
300 => Some("Multiple Choices"),
301 => Some("Moved Permanently"),
302 => Some("Found"),
303 => Some("See Other"),
304 => Some("Not Modified"),
307 => Some("Temporary Redirect"),
308 => Some("Permanent Redirect"),
400 => Some("Bad Request"),
401 => Some("Unauthorized"),
403 => Some("Forbidden"),
404 => Some("Not Found"),
405 => Some("Method Not Allowed"),
409 => Some("Conflict"),
410 => Some("Gone"),
415 => Some("Unsupported Media Type"),
422 => Some("Unprocessable Content"),
429 => Some("Too Many Requests"),
500 => Some("Internal Server Error"),
501 => Some("Not Implemented"),
502 => Some("Bad Gateway"),
503 => Some("Service Unavailable"),
504 => Some("Gateway Timeout"),
_ => None,
}
}
#[must_use]
pub const fn status_class(code: u16) -> Option<StatusClass> {
match code {
100..=199 => Some(StatusClass::Informational),
200..=299 => Some(StatusClass::Success),
300..=399 => Some(StatusClass::Redirection),
400..=499 => Some(StatusClass::ClientError),
500..=599 => Some(StatusClass::ServerError),
_ => None,
}
}
#[must_use]
pub const fn is_informational(code: u16) -> bool {
matches!(status_class(code), Some(StatusClass::Informational))
}
#[must_use]
pub const fn is_success(code: u16) -> bool {
matches!(status_class(code), Some(StatusClass::Success))
}
#[must_use]
pub const fn is_redirect(code: u16) -> bool {
matches!(status_class(code), Some(StatusClass::Redirection))
}
#[must_use]
pub const fn is_client_error(code: u16) -> bool {
matches!(status_class(code), Some(StatusClass::ClientError))
}
#[must_use]
pub const fn is_server_error(code: u16) -> bool {
matches!(status_class(code), Some(StatusClass::ServerError))
}
#[must_use]
pub const fn is_error(code: u16) -> bool {
is_client_error(code) || is_server_error(code)
}
#[must_use]
pub const fn is_cacheable_by_default(code: u16) -> bool {
matches!(
code,
200 | 203 | 204 | 206 | 300 | 301 | 404 | 405 | 410 | 414 | 501
)
}