sark_http/payload/response/status/
mod.rs1mod seal;
2
3pub(crate) use seal::Seal;
4use std::num::NonZero;
5
6use crate::{
7 field,
8 payload::response::{dispatch, headers::Application},
9};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[repr(transparent)]
14pub struct Code(NonZero<u16>);
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct Error {
18 actual: u16,
19}
20
21pub trait Source: Seal {
22 type Value: Copy;
23
24 fn code(value: Self::Value) -> Code;
25
26 fn fields<F: field::Source<Application>>(
27 value: Self::Value,
28 fields: F,
29 ) -> (Code, impl field::Source) {
30 let code = Self::code(value);
31 (code, field::Response::new(code, fields))
32 }
33}
34
35pub trait Constant {
36 const CODE: Code;
37}
38
39impl<S: dispatch::Success> Constant for S {
40 const CODE: Code = S::STATUS;
41}
42
43impl<S: Constant> Source for S {
44 type Value = ();
45
46 fn code(_: ()) -> Code {
47 S::CODE
48 }
49
50 fn fields<F: field::Source<Application>>(_: (), fields: F) -> (Code, impl field::Source) {
51 (S::CODE, field::Response::declared::<S>(fields))
52 }
53}
54
55impl Source for Code {
56 type Value = Code;
57
58 fn code(value: Code) -> Code {
59 value
60 }
61}
62
63impl Code {
64 pub const CONTINUE: Self = Self::exact::<100>();
65 pub const SWITCHING_PROTOCOLS: Self = Self::exact::<101>();
66 pub const PROCESSING: Self = Self::exact::<102>();
67 pub const EARLY_HINTS: Self = Self::exact::<103>();
68 pub const OK: Self = Self::exact::<200>();
69 pub const CREATED: Self = Self::exact::<201>();
70 pub const ACCEPTED: Self = Self::exact::<202>();
71 pub const NON_AUTHORITATIVE_INFORMATION: Self = Self::exact::<203>();
72 pub const NO_CONTENT: Self = Self::exact::<204>();
73 pub const RESET_CONTENT: Self = Self::exact::<205>();
74 pub const PARTIAL_CONTENT: Self = Self::exact::<206>();
75 pub const MULTI_STATUS: Self = Self::exact::<207>();
76 pub const ALREADY_REPORTED: Self = Self::exact::<208>();
77 pub const IM_USED: Self = Self::exact::<226>();
78 pub const MULTIPLE_CHOICES: Self = Self::exact::<300>();
79 pub const MOVED_PERMANENTLY: Self = Self::exact::<301>();
80 pub const FOUND: Self = Self::exact::<302>();
81 pub const SEE_OTHER: Self = Self::exact::<303>();
82 pub const NOT_MODIFIED: Self = Self::exact::<304>();
83 pub const USE_PROXY: Self = Self::exact::<305>();
84 pub const TEMPORARY_REDIRECT: Self = Self::exact::<307>();
85 pub const PERMANENT_REDIRECT: Self = Self::exact::<308>();
86 pub const BAD_REQUEST: Self = Self::exact::<400>();
87 pub const UNAUTHORIZED: Self = Self::exact::<401>();
88 pub const PAYMENT_REQUIRED: Self = Self::exact::<402>();
89 pub const FORBIDDEN: Self = Self::exact::<403>();
90 pub const NOT_FOUND: Self = Self::exact::<404>();
91 pub const METHOD_NOT_ALLOWED: Self = Self::exact::<405>();
92 pub const NOT_ACCEPTABLE: Self = Self::exact::<406>();
93 pub const PROXY_AUTHENTICATION_REQUIRED: Self = Self::exact::<407>();
94 pub const REQUEST_TIMEOUT: Self = Self::exact::<408>();
95 pub const CONFLICT: Self = Self::exact::<409>();
96 pub const GONE: Self = Self::exact::<410>();
97 pub const LENGTH_REQUIRED: Self = Self::exact::<411>();
98 pub const PRECONDITION_FAILED: Self = Self::exact::<412>();
99 pub const PAYLOAD_TOO_LARGE: Self = Self::exact::<413>();
100 pub const URI_TOO_LONG: Self = Self::exact::<414>();
101 pub const UNSUPPORTED_MEDIA_TYPE: Self = Self::exact::<415>();
102 pub const RANGE_NOT_SATISFIABLE: Self = Self::exact::<416>();
103 pub const EXPECTATION_FAILED: Self = Self::exact::<417>();
104 pub const IM_A_TEAPOT: Self = Self::exact::<418>();
105 pub const MISDIRECTED_REQUEST: Self = Self::exact::<421>();
106 pub const UNPROCESSABLE_ENTITY: Self = Self::exact::<422>();
107 pub const LOCKED: Self = Self::exact::<423>();
108 pub const FAILED_DEPENDENCY: Self = Self::exact::<424>();
109 pub const TOO_EARLY: Self = Self::exact::<425>();
110 pub const UPGRADE_REQUIRED: Self = Self::exact::<426>();
111 pub const PRECONDITION_REQUIRED: Self = Self::exact::<428>();
112 pub const TOO_MANY_REQUESTS: Self = Self::exact::<429>();
113 pub const REQUEST_HEADER_FIELDS_TOO_LARGE: Self = Self::exact::<431>();
114 pub const UNAVAILABLE_FOR_LEGAL_REASONS: Self = Self::exact::<451>();
115 pub const INTERNAL_SERVER_ERROR: Self = Self::exact::<500>();
116 pub const NOT_IMPLEMENTED: Self = Self::exact::<501>();
117 pub const BAD_GATEWAY: Self = Self::exact::<502>();
118 pub const SERVICE_UNAVAILABLE: Self = Self::exact::<503>();
119 pub const GATEWAY_TIMEOUT: Self = Self::exact::<504>();
120 pub const HTTP_VERSION_NOT_SUPPORTED: Self = Self::exact::<505>();
121 pub const VARIANT_ALSO_NEGOTIATES: Self = Self::exact::<506>();
122 pub const INSUFFICIENT_STORAGE: Self = Self::exact::<507>();
123 pub const LOOP_DETECTED: Self = Self::exact::<508>();
124 pub const NOT_EXTENDED: Self = Self::exact::<510>();
125 pub const NETWORK_AUTHENTICATION_REQUIRED: Self = Self::exact::<511>();
126
127 pub const fn from_u16(actual: u16) -> Result<Self, Error> {
129 if actual < 100 || actual > 599 {
130 return Err(Error { actual });
131 }
132 Ok(Self(NonZero::<u16>::MIN.saturating_add(actual - 1)))
133 }
134
135 pub const fn as_u16(&self) -> u16 {
136 self.0.get()
137 }
138
139 pub const fn as_bytes(&self) -> [u8; 3] {
140 let value = self.as_u16();
141 [
142 b'0' + (value / 100) as u8,
143 b'0' + ((value / 10) % 10) as u8,
144 b'0' + (value % 10) as u8,
145 ]
146 }
147
148 pub const fn is_info(&self) -> bool {
149 self.as_u16() / 100 == 1
150 }
151
152 pub const fn is_success(&self) -> bool {
153 self.as_u16() / 100 == 2
154 }
155
156 pub const fn reason(&self) -> Option<&str> {
157 match self.as_u16() {
158 100 => Some("Continue"),
159 101 => Some("Switching Protocols"),
160 102 => Some("Processing"),
161 103 => Some("Early Hints"),
162 200 => Some("OK"),
163 201 => Some("Created"),
164 202 => Some("Accepted"),
165 203 => Some("Non Authoritative Information"),
166 204 => Some("No Content"),
167 205 => Some("Reset Content"),
168 206 => Some("Partial Content"),
169 207 => Some("Multi-Status"),
170 208 => Some("Already Reported"),
171 226 => Some("IM Used"),
172 300 => Some("Multiple Choices"),
173 301 => Some("Moved Permanently"),
174 302 => Some("Found"),
175 303 => Some("See Other"),
176 304 => Some("Not Modified"),
177 305 => Some("Use Proxy"),
178 307 => Some("Temporary Redirect"),
179 308 => Some("Permanent Redirect"),
180 400 => Some("Bad Request"),
181 401 => Some("Unauthorized"),
182 402 => Some("Payment Required"),
183 403 => Some("Forbidden"),
184 404 => Some("Not Found"),
185 405 => Some("Method Not Allowed"),
186 406 => Some("Not Acceptable"),
187 407 => Some("Proxy Authentication Required"),
188 408 => Some("Request Timeout"),
189 409 => Some("Conflict"),
190 410 => Some("Gone"),
191 411 => Some("Length Required"),
192 412 => Some("Precondition Failed"),
193 413 => Some("Payload Too Large"),
194 414 => Some("URI Too Long"),
195 415 => Some("Unsupported Media Type"),
196 416 => Some("Range Not Satisfiable"),
197 417 => Some("Expectation Failed"),
198 418 => Some("I'm a teapot"),
199 421 => Some("Misdirected Request"),
200 422 => Some("Unprocessable Entity"),
201 423 => Some("Locked"),
202 424 => Some("Failed Dependency"),
203 425 => Some("Too Early"),
204 426 => Some("Upgrade Required"),
205 428 => Some("Precondition Required"),
206 429 => Some("Too Many Requests"),
207 431 => Some("Request Header Fields Too Large"),
208 451 => Some("Unavailable For Legal Reasons"),
209 500 => Some("Internal Server Error"),
210 501 => Some("Not Implemented"),
211 502 => Some("Bad Gateway"),
212 503 => Some("Service Unavailable"),
213 504 => Some("Gateway Timeout"),
214 505 => Some("HTTP Version Not Supported"),
215 506 => Some("Variant Also Negotiates"),
216 507 => Some("Insufficient Storage"),
217 508 => Some("Loop Detected"),
218 510 => Some("Not Extended"),
219 511 => Some("Network Authentication Required"),
220 _ => None,
221 }
222 }
223
224 const fn exact<const VALUE: u16>() -> Self {
225 const { assert!(VALUE >= 100 && VALUE <= 599) };
226 Self(NonZero::<u16>::MIN.saturating_add(VALUE - 1))
227 }
228}
229
230impl Default for Code {
231 fn default() -> Self {
232 Self::OK
233 }
234}
235
236impl From<Code> for u16 {
237 fn from(code: Code) -> Self {
238 code.as_u16()
239 }
240}
241
242impl Error {
243 pub const fn actual(self) -> u16 {
244 self.actual
245 }
246}
247
248const _: () = {
249 assert!(size_of::<Code>() == size_of::<u16>());
250 assert!(size_of::<Option<Code>>() == size_of::<Code>());
251 assert!(size_of::<Error>() == size_of::<u16>());
252 assert!(!std::mem::needs_drop::<Code>());
253 assert!(!std::mem::needs_drop::<Error>());
254};