Skip to main content

base_http/
status.rs

1// Released under the MIT License.
2// Copyright, 2019-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 UNPROCESSABLE_CONTENT: Self = Self(422);
47    pub const TOO_MANY_REQUESTS: Self = Self(429);
48    pub const INTERNAL_SERVER_ERROR: Self = Self(500);
49    pub const NOT_IMPLEMENTED: Self = Self(501);
50    pub const BAD_GATEWAY: Self = Self(502);
51    pub const SERVICE_UNAVAILABLE: Self = Self(503);
52    pub const GATEWAY_TIMEOUT: Self = Self(504);
53
54    pub const fn new(code: u16) -> Result<Self, InvalidStatus> {
55        if code >= 100 && code <= 599 {
56            Ok(Self(code))
57        } else {
58            Err(InvalidStatus)
59        }
60    }
61
62    pub const fn as_u16(self) -> u16 {
63        self.0
64    }
65    pub const fn is_informational(self) -> bool {
66        self.0 >= 100 && self.0 < 200
67    }
68    /// Switching protocols ends an HTTP/1 exchange despite its 1xx class.
69    pub const fn is_final(self) -> bool {
70        self.0 == 101 || self.0 >= 200
71    }
72    pub const fn is_continue(self) -> bool {
73        self.0 == 100
74    }
75    pub const fn is_ok(self) -> bool {
76        self.0 == 200
77    }
78    pub const fn is_success(self) -> bool {
79        self.0 >= 200 && self.0 < 300
80    }
81    pub const fn is_partial(self) -> bool {
82        self.0 == 206
83    }
84    pub const fn is_redirection(self) -> bool {
85        self.0 >= 300 && self.0 < 400
86    }
87    pub const fn is_not_modified(self) -> bool {
88        self.0 == 304
89    }
90    pub const fn preserves_method(self) -> bool {
91        self.0 == 307 || self.0 == 308
92    }
93    pub const fn is_failure(self) -> bool {
94        self.0 >= 400
95    }
96    pub const fn is_client_error(self) -> bool {
97        self.0 >= 400 && self.0 < 500
98    }
99    pub const fn is_server_error(self) -> bool {
100        self.0 >= 500
101    }
102
103    /// A conventional description, independent of the wire version/reason phrase.
104    pub const fn description(self) -> Option<&'static str> {
105        Some(match self.0 {
106            100 => "Continue",
107            101 => "Switching Protocols",
108            102 => "Processing",
109            103 => "Early Hints",
110            200 => "OK",
111            201 => "Created",
112            202 => "Accepted",
113            203 => "Non-Authoritative Information",
114            204 => "No Content",
115            205 => "Reset Content",
116            206 => "Partial Content",
117            207 => "Multi-Status",
118            208 => "Already Reported",
119            226 => "IM Used",
120            300 => "Multiple Choices",
121            301 => "Moved Permanently",
122            302 => "Found",
123            303 => "See Other",
124            304 => "Not Modified",
125            305 => "Use Proxy",
126            306 => "Switch Proxy",
127            307 => "Temporary Redirect",
128            308 => "Permanent Redirect",
129            400 => "Bad Request",
130            401 => "Unauthorized",
131            402 => "Payment Required",
132            403 => "Forbidden",
133            404 => "Not Found",
134            405 => "Method Not Allowed",
135            406 => "Not Acceptable",
136            407 => "Proxy Authentication Required",
137            408 => "Request Timeout",
138            409 => "Conflict",
139            410 => "Gone",
140            411 => "Length Required",
141            412 => "Precondition Failed",
142            413 => "Content Too Large",
143            414 => "URI Too Long",
144            415 => "Unsupported Media Type",
145            416 => "Range Not Satisfiable",
146            417 => "Expectation Failed",
147            418 => "I'm a Teapot",
148            421 => "Misdirected Request",
149            422 => "Unprocessable Content",
150            423 => "Locked",
151            424 => "Failed Dependency",
152            425 => "Too Early",
153            426 => "Upgrade Required",
154            428 => "Precondition Required",
155            429 => "Too Many Requests",
156            431 => "Request Header Fields Too Large",
157            451 => "Unavailable For Legal Reasons",
158            500 => "Internal Server Error",
159            501 => "Not Implemented",
160            502 => "Bad Gateway",
161            503 => "Service Unavailable",
162            504 => "Gateway Timeout",
163            505 => "HTTP Version Not Supported",
164            506 => "Variant Also Negotiates",
165            507 => "Insufficient Storage",
166            508 => "Loop Detected",
167            510 => "Not Extended",
168            511 => "Network Authentication Required",
169            _ => return None,
170        })
171    }
172}
173
174impl TryFrom<u16> for Status {
175    type Error = InvalidStatus;
176    fn try_from(value: u16) -> Result<Self, Self::Error> {
177        Self::new(value)
178    }
179}
180impl From<Status> for u16 {
181    fn from(value: Status) -> Self {
182        value.0
183    }
184}
185impl fmt::Display for Status {
186    fn fmt(&self, output: &mut fmt::Formatter<'_>) -> fmt::Result {
187        self.0.fmt(output)
188    }
189}