elif_http/response/
status.rs

1//! HTTP status code utilities
2
3use crate::errors::ParseError;
4use std::fmt;
5
6/// Framework-native status code wrapper that hides Axum internals
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct ElifStatusCode(axum::http::StatusCode);
9
10impl ElifStatusCode {
11    // Common status codes as constants
12    pub const OK: Self = Self(axum::http::StatusCode::OK);
13    pub const CREATED: Self = Self(axum::http::StatusCode::CREATED);
14    pub const ACCEPTED: Self = Self(axum::http::StatusCode::ACCEPTED);
15    pub const NO_CONTENT: Self = Self(axum::http::StatusCode::NO_CONTENT);
16    pub const MOVED_PERMANENTLY: Self = Self(axum::http::StatusCode::MOVED_PERMANENTLY);
17    pub const FOUND: Self = Self(axum::http::StatusCode::FOUND);
18    pub const SEE_OTHER: Self = Self(axum::http::StatusCode::SEE_OTHER);
19    pub const NOT_MODIFIED: Self = Self(axum::http::StatusCode::NOT_MODIFIED);
20    pub const BAD_REQUEST: Self = Self(axum::http::StatusCode::BAD_REQUEST);
21    pub const UNAUTHORIZED: Self = Self(axum::http::StatusCode::UNAUTHORIZED);
22    pub const FORBIDDEN: Self = Self(axum::http::StatusCode::FORBIDDEN);
23    pub const NOT_FOUND: Self = Self(axum::http::StatusCode::NOT_FOUND);
24    pub const METHOD_NOT_ALLOWED: Self = Self(axum::http::StatusCode::METHOD_NOT_ALLOWED);
25    pub const PRECONDITION_FAILED: Self = Self(axum::http::StatusCode::PRECONDITION_FAILED);
26    pub const CONFLICT: Self = Self(axum::http::StatusCode::CONFLICT);
27    pub const LOCKED: Self = Self(axum::http::StatusCode::LOCKED);
28    pub const UNPROCESSABLE_ENTITY: Self = Self(axum::http::StatusCode::UNPROCESSABLE_ENTITY);
29    pub const REQUEST_TIMEOUT: Self = Self(axum::http::StatusCode::REQUEST_TIMEOUT);
30    pub const PAYLOAD_TOO_LARGE: Self = Self(axum::http::StatusCode::PAYLOAD_TOO_LARGE);
31    pub const TOO_MANY_REQUESTS: Self = Self(axum::http::StatusCode::TOO_MANY_REQUESTS);
32    pub const INTERNAL_SERVER_ERROR: Self = Self(axum::http::StatusCode::INTERNAL_SERVER_ERROR);
33    pub const NOT_IMPLEMENTED: Self = Self(axum::http::StatusCode::NOT_IMPLEMENTED);
34    pub const BAD_GATEWAY: Self = Self(axum::http::StatusCode::BAD_GATEWAY);
35    pub const SERVICE_UNAVAILABLE: Self = Self(axum::http::StatusCode::SERVICE_UNAVAILABLE);
36
37    /// Create status code from u16
38    pub fn from_u16(src: u16) -> Result<Self, ParseError> {
39        axum::http::StatusCode::from_u16(src)
40            .map(Self)
41            .map_err(|_| ParseError::invalid_status_code(src))
42    }
43
44    /// Get status code as u16
45    pub fn as_u16(&self) -> u16 {
46        self.0.as_u16()
47    }
48
49    /// Check if status code is informational (1xx)
50    pub fn is_informational(&self) -> bool {
51        self.0.is_informational()
52    }
53
54    /// Check if status code is success (2xx)
55    pub fn is_success(&self) -> bool {
56        self.0.is_success()
57    }
58
59    /// Check if status code is redirection (3xx)
60    pub fn is_redirection(&self) -> bool {
61        self.0.is_redirection()
62    }
63
64    /// Check if status code is client error (4xx)
65    pub fn is_client_error(&self) -> bool {
66        self.0.is_client_error()
67    }
68
69    /// Check if status code is server error (5xx)
70    pub fn is_server_error(&self) -> bool {
71        self.0.is_server_error()
72    }
73
74    /// Internal method to convert to axum StatusCode (for framework internals only)
75    pub(crate) fn to_axum(&self) -> axum::http::StatusCode {
76        self.0
77    }
78
79    /// Internal method to create from axum StatusCode (for framework internals only)
80    pub(crate) fn from_axum(status: axum::http::StatusCode) -> Self {
81        Self(status)
82    }
83}
84
85impl From<u16> for ElifStatusCode {
86    fn from(src: u16) -> Self {
87        Self::from_u16(src).expect("Invalid status code")
88    }
89}
90
91impl From<ElifStatusCode> for u16 {
92    fn from(status: ElifStatusCode) -> u16 {
93        status.as_u16()
94    }
95}
96
97impl fmt::Display for ElifStatusCode {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(f, "{}", self.0)
100    }
101}