Skip to main content

base_http/
status.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use std::{error::Error, fmt};
5
6/// An HTTP status code in the range 100..=599, including unknown codes.
7#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
8pub struct Status(u16);
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub struct InvalidStatus;
12
13impl fmt::Display for InvalidStatus {
14    fn fmt(&self, output: &mut fmt::Formatter<'_>) -> fmt::Result {
15        output.write_str("HTTP status must be between 100 and 599")
16    }
17}
18impl Error for InvalidStatus {}
19
20impl Status {
21    pub const CONTINUE: Self = Self(100);
22    pub const SWITCHING_PROTOCOLS: Self = Self(101);
23    pub const PROCESSING: Self = Self(102);
24    pub const EARLY_HINTS: Self = Self(103);
25    pub const OK: Self = Self(200);
26    pub const CREATED: Self = Self(201);
27    pub const ACCEPTED: Self = Self(202);
28    pub const NO_CONTENT: Self = Self(204);
29    pub const RESET_CONTENT: Self = Self(205);
30    pub const PARTIAL_CONTENT: Self = Self(206);
31    pub const MOVED_PERMANENTLY: Self = Self(301);
32    pub const FOUND: Self = Self(302);
33    pub const SEE_OTHER: Self = Self(303);
34    pub const NOT_MODIFIED: Self = Self(304);
35    pub const TEMPORARY_REDIRECT: Self = Self(307);
36    pub const PERMANENT_REDIRECT: Self = Self(308);
37    pub const BAD_REQUEST: Self = Self(400);
38    pub const UNAUTHORIZED: Self = Self(401);
39    pub const FORBIDDEN: Self = Self(403);
40    pub const NOT_FOUND: Self = Self(404);
41    pub const METHOD_NOT_ALLOWED: Self = Self(405);
42    pub const REQUEST_TIMEOUT: Self = Self(408);
43    pub const CONFLICT: Self = Self(409);
44    pub const CONTENT_TOO_LARGE: Self = Self(413);
45    pub const EXPECTATION_FAILED: Self = Self(417);
46    pub const TOO_MANY_REQUESTS: Self = Self(429);
47    pub const INTERNAL_SERVER_ERROR: Self = Self(500);
48    pub const NOT_IMPLEMENTED: Self = Self(501);
49    pub const BAD_GATEWAY: Self = Self(502);
50    pub const SERVICE_UNAVAILABLE: Self = Self(503);
51    pub const GATEWAY_TIMEOUT: Self = Self(504);
52
53    pub const fn new(code: u16) -> Result<Self, InvalidStatus> {
54        if code >= 100 && code <= 599 {
55            Ok(Self(code))
56        } else {
57            Err(InvalidStatus)
58        }
59    }
60
61    pub const fn as_u16(self) -> u16 {
62        self.0
63    }
64    pub const fn is_informational(self) -> bool {
65        self.0 >= 100 && self.0 < 200
66    }
67    /// Switching protocols ends an HTTP/1 exchange despite its 1xx class.
68    pub const fn is_final(self) -> bool {
69        self.0 == 101 || self.0 >= 200
70    }
71    pub const fn is_continue(self) -> bool {
72        self.0 == 100
73    }
74    pub const fn is_ok(self) -> bool {
75        self.0 == 200
76    }
77    pub const fn is_success(self) -> bool {
78        self.0 >= 200 && self.0 < 300
79    }
80    pub const fn is_partial(self) -> bool {
81        self.0 == 206
82    }
83    pub const fn is_redirection(self) -> bool {
84        self.0 >= 300 && self.0 < 400
85    }
86    pub const fn is_not_modified(self) -> bool {
87        self.0 == 304
88    }
89    pub const fn preserves_method(self) -> bool {
90        self.0 == 307 || self.0 == 308
91    }
92    pub const fn is_failure(self) -> bool {
93        self.0 >= 400
94    }
95    pub const fn is_client_error(self) -> bool {
96        self.0 >= 400 && self.0 < 500
97    }
98    pub const fn is_server_error(self) -> bool {
99        self.0 >= 500
100    }
101
102    /// A conventional description, independent of the wire version/reason phrase.
103    pub const fn description(self) -> Option<&'static str> {
104        Some(match self.0 {
105            100 => "Continue",
106            101 => "Switching Protocols",
107            102 => "Processing",
108            103 => "Early Hints",
109            200 => "OK",
110            201 => "Created",
111            202 => "Accepted",
112            203 => "Non-Authoritative Information",
113            204 => "No Content",
114            205 => "Reset Content",
115            206 => "Partial Content",
116            207 => "Multi-Status",
117            208 => "Already Reported",
118            226 => "IM Used",
119            300 => "Multiple Choices",
120            301 => "Moved Permanently",
121            302 => "Found",
122            303 => "See Other",
123            304 => "Not Modified",
124            305 => "Use Proxy",
125            306 => "Switch Proxy",
126            307 => "Temporary Redirect",
127            308 => "Permanent Redirect",
128            400 => "Bad Request",
129            401 => "Unauthorized",
130            402 => "Payment Required",
131            403 => "Forbidden",
132            404 => "Not Found",
133            405 => "Method Not Allowed",
134            406 => "Not Acceptable",
135            407 => "Proxy Authentication Required",
136            408 => "Request Timeout",
137            409 => "Conflict",
138            410 => "Gone",
139            411 => "Length Required",
140            412 => "Precondition Failed",
141            413 => "Content Too Large",
142            414 => "URI Too Long",
143            415 => "Unsupported Media Type",
144            416 => "Range Not Satisfiable",
145            417 => "Expectation Failed",
146            418 => "I'm a Teapot",
147            421 => "Misdirected Request",
148            422 => "Unprocessable Content",
149            423 => "Locked",
150            424 => "Failed Dependency",
151            425 => "Too Early",
152            426 => "Upgrade Required",
153            428 => "Precondition Required",
154            429 => "Too Many Requests",
155            431 => "Request Header Fields Too Large",
156            451 => "Unavailable For Legal Reasons",
157            500 => "Internal Server Error",
158            501 => "Not Implemented",
159            502 => "Bad Gateway",
160            503 => "Service Unavailable",
161            504 => "Gateway Timeout",
162            505 => "HTTP Version Not Supported",
163            506 => "Variant Also Negotiates",
164            507 => "Insufficient Storage",
165            508 => "Loop Detected",
166            510 => "Not Extended",
167            511 => "Network Authentication Required",
168            _ => return None,
169        })
170    }
171}
172
173impl TryFrom<u16> for Status {
174    type Error = InvalidStatus;
175    fn try_from(value: u16) -> Result<Self, Self::Error> {
176        Self::new(value)
177    }
178}
179impl From<Status> for u16 {
180    fn from(value: Status) -> Self {
181        value.0
182    }
183}
184impl fmt::Display for Status {
185    fn fmt(&self, output: &mut fmt::Formatter<'_>) -> fmt::Result {
186        self.0.fmt(output)
187    }
188}