Skip to main content

g_server/http/
status.rs

1use std::fmt;
2
3/// An HTTP response status code.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct Status(u16);
6
7impl Status {
8    // 1xx Informational
9    pub const CONTINUE: Self = Self(100);
10    pub const SWITCHING_PROTOCOLS: Self = Self(101);
11    pub const PROCESSING: Self = Self(102);
12    pub const EARLY_HINTS: Self = Self(103);
13
14    // 2xx Success
15    pub const OK: Self = Self(200);
16    pub const CREATED: Self = Self(201);
17    pub const ACCEPTED: Self = Self(202);
18    pub const NON_AUTHORITATIVE_INFORMATION: Self = Self(203);
19    pub const NO_CONTENT: Self = Self(204);
20    pub const RESET_CONTENT: Self = Self(205);
21    pub const PARTIAL_CONTENT: Self = Self(206);
22    pub const MULTI_STATUS: Self = Self(207);
23    pub const ALREADY_REPORTED: Self = Self(208);
24    pub const IM_USED: Self = Self(226);
25
26    // 3xx Redirection
27    pub const MULTIPLE_CHOICES: Self = Self(300);
28    pub const MOVED_PERMANENTLY: Self = Self(301);
29    pub const FOUND: Self = Self(302);
30    pub const SEE_OTHER: Self = Self(303);
31    pub const NOT_MODIFIED: Self = Self(304);
32    pub const TEMPORARY_REDIRECT: Self = Self(307);
33    pub const PERMANENT_REDIRECT: Self = Self(308);
34
35    // 4xx Client Error
36    pub const BAD_REQUEST: Self = Self(400);
37    pub const UNAUTHORIZED: Self = Self(401);
38    pub const PAYMENT_REQUIRED: Self = Self(402);
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 NOT_ACCEPTABLE: Self = Self(406);
43    pub const PROXY_AUTHENTICATION_REQUIRED: Self = Self(407);
44    pub const REQUEST_TIMEOUT: Self = Self(408);
45    pub const CONFLICT: Self = Self(409);
46    pub const GONE: Self = Self(410);
47    pub const LENGTH_REQUIRED: Self = Self(411);
48    pub const PRECONDITION_FAILED: Self = Self(412);
49    pub const PAYLOAD_TOO_LARGE: Self = Self(413);
50    pub const URI_TOO_LONG: Self = Self(414);
51    pub const UNSUPPORTED_MEDIA_TYPE: Self = Self(415);
52    pub const RANGE_NOT_SATISFIABLE: Self = Self(416);
53    pub const EXPECTATION_FAILED: Self = Self(417);
54    pub const IM_A_TEAPOT: Self = Self(418);
55    pub const UNPROCESSABLE_CONTENT: Self = Self(422);
56    pub const TOO_EARLY: Self = Self(425);
57    pub const UPGRADE_REQUIRED: Self = Self(426);
58    pub const TOO_MANY_REQUESTS: Self = Self(429);
59    pub const REQUEST_HEADER_FIELDS_TOO_LARGE: Self = Self(431);
60
61    // 5xx Server Error
62    pub const INTERNAL_SERVER_ERROR: Self = Self(500);
63    pub const NOT_IMPLEMENTED: Self = Self(501);
64    pub const BAD_GATEWAY: Self = Self(502);
65    pub const SERVICE_UNAVAILABLE: Self = Self(503);
66    pub const GATEWAY_TIMEOUT: Self = Self(504);
67    pub const HTTP_VERSION_NOT_SUPPORTED: Self = Self(505);
68    pub const VARIANT_ALSO_NEGOTIATES: Self = Self(506);
69    pub const INSUFFICIENT_STORAGE: Self = Self(507);
70    pub const LOOP_DETECTED: Self = Self(508);
71    pub const NOT_EXTENDED: Self = Self(510);
72    pub const NETWORK_AUTHENTICATION_REQUIRED: Self = Self(511);
73
74    /// Creates a status from a numeric status code.
75    pub const fn new(code: u16) -> Self {
76        Self(code)
77    }
78
79    /// Returns the numeric status code.
80    pub const fn as_u16(self) -> u16 {
81        self.0
82    }
83
84    /// Returns whether this is a `1xx` status.
85    pub const fn is_informational(self) -> bool {
86        self.0 >= 100 && self.0 < 200
87    }
88
89    /// Returns whether this is a `2xx` status.
90    pub const fn is_success(self) -> bool {
91        self.0 >= 200 && self.0 < 300
92    }
93
94    /// Returns whether this is a `3xx` status.
95    pub const fn is_redirection(self) -> bool {
96        self.0 >= 300 && self.0 < 400
97    }
98
99    /// Returns whether this is a `4xx` status.
100    pub const fn is_client_error(self) -> bool {
101        self.0 >= 400 && self.0 < 500
102    }
103
104    /// Returns whether this is a `5xx` status.
105    pub const fn is_server_error(self) -> bool {
106        self.0 >= 500 && self.0 < 600
107    }
108}
109
110impl From<u16> for Status {
111    fn from(code: u16) -> Self {
112        Self::new(code)
113    }
114}
115
116impl fmt::Display for Status {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        self.0.fmt(f)
119    }
120}