rusty_web/status/
mod.rs

1/// Refer to this url for more information: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
2#[derive(Debug)]
3pub enum Status {
4    // Information responses
5    Continue = 100,
6    SwitchingProtocols = 101,
7    Processing = 102,
8    EarlyHints = 103,
9
10    // Successful responses
11    Ok = 200,
12    Created = 201,
13    Accepted = 202,
14    NonAuthoritativeInformation = 203,
15    NoContent = 204,
16    ResetContent = 205,
17    PartialContent = 206,
18    MultiStatus = 207,
19    AlreadyReported = 208,
20    ImUsed = 226,
21
22    // Redirection Messages
23    MultipleChoices = 300,
24    MovedPermanently = 301,
25    Found = 302,
26    SeeOther = 303,
27    NotModified = 304,
28    /// Depreciated
29    UseProxy = 305,
30    /// Depreciated
31    UnUsed = 306,
32    /// No longer used, just reserved
33    TemporaryRedirect = 307,
34    PermanentRedirect = 308,
35
36    // Client error responses
37    BadRequest = 400,
38    UnAuthorized = 401,
39    /// Experimental. Expect behaviour to change in the future.
40    PaymentRequired = 402,
41    Forbidden = 403,
42    NotFound = 404,
43    MethodNotAllowed = 405,
44    NotAcceptable = 406,
45    ProxyAuthenticationRequired = 407,
46    RequestTimeout = 408,
47    Conflict = 409,
48    Gone = 410,
49    LengthRequired = 411,
50    PreconditionFailed = 412,
51    PayloadTooLarge = 413,
52    UriTooLong = 414,
53    UnsupportedMediaType = 415,
54    RangeNotSatisfiable = 416,
55    ExpectationFailed = 417,
56    ImaTeaPot = 418,
57    MisRedirectRequest = 421,
58    UnprocessableContent = 422,
59    Locked = 423,
60    FailedDependency = 424,
61    TooEarly = 425,
62    UpgradeRequired = 426,
63    PreconditionRequired = 428,
64    TooManyRequests = 429,
65    RequestHeaderFieldsTooLarge = 431,
66    UnavailableForLegalReasons = 451,
67
68    // Server error responses
69    InternalServerError = 500,
70    NotImplemented = 501,
71    BadGateway = 502,
72    ServiceUnavailable = 503,
73    GatewayTimeout = 504,
74    HttpVersionNotSupported = 505,
75    VariantAlsoNegotiates = 506,
76    InsufficientStorage = 507,
77    LoopDetected = 508,
78    NotExtended = 510,
79    NetworkAuthenticationRequired = 511,
80}
81
82pub trait StatusMethods {
83    fn status_code(&self) -> Option<usize>;
84    fn status_text(status_code: usize) -> Option<String>;
85}
86
87impl StatusMethods for Status {
88    fn status_code(&self) -> Option<usize> {
89        match self {
90            // Information responses
91            Status::Continue => Some(100),
92            Status::SwitchingProtocols => Some(101),
93            Status::Processing => Some(102),
94            Status::EarlyHints => Some(103),
95
96            // Successful responses
97            Status::Ok => Some(200),
98            Status::Created => Some(201),
99            Status::Accepted => Some(202),
100            Status::NonAuthoritativeInformation => Some(203),
101            Status::NoContent => Some(204),
102            Status::ResetContent => Some(205),
103            Status::PartialContent => Some(206),
104            Status::MultiStatus => Some(207),
105            Status::AlreadyReported => Some(208),
106            Status::ImUsed => Some(226),
107
108            // Redirection Messages
109            Status::MultipleChoices => Some(300),
110            Status::MovedPermanently => Some(301),
111            Status::Found => Some(302),
112            Status::SeeOther => Some(303),
113            Status::NotModified => Some(304),
114            Status::UseProxy => Some(305), // Depreciated
115            Status::UnUsed => Some(306), // Depreciated
116            Status::TemporaryRedirect => Some(307), // No longer used, just reserved
117            Status::PermanentRedirect => Some(308),
118
119            // Client error responses
120            Status::BadRequest => Some(400),
121            Status::UnAuthorized => Some(401), // Experimental. Expect behaviour to change in the future.
122            Status::PaymentRequired => Some(402),
123            Status::Forbidden => Some(403),
124            Status::NotFound => Some(404),
125            Status::MethodNotAllowed => Some(405),
126            Status::NotAcceptable => Some(406),
127            Status::ProxyAuthenticationRequired => Some(407),
128            Status::RequestTimeout => Some(408),
129            Status::Conflict => Some(409),
130            Status::Gone => Some(410),
131            Status::LengthRequired => Some(411),
132            Status::PreconditionFailed => Some(412),
133            Status::PayloadTooLarge => Some(413),
134            Status::UriTooLong => Some(414),
135            Status::UnsupportedMediaType => Some(415),
136            Status::RangeNotSatisfiable => Some(416),
137            Status::ExpectationFailed => Some(417),
138            Status::ImaTeaPot => Some(418),
139            Status::MisRedirectRequest => Some(421),
140            Status::UnprocessableContent => Some(422),
141            Status::Locked => Some(423),
142            Status::FailedDependency => Some(424),
143            Status::TooEarly => Some(425), // Experimental. Expect behaviour to change in the future.
144            Status::UpgradeRequired => Some(426),
145            Status::PreconditionRequired => Some(428),
146            Status::TooManyRequests => Some(429),
147            Status::RequestHeaderFieldsTooLarge => Some(431),
148            Status::UnavailableForLegalReasons => Some(451),
149
150            // Server error responses
151            Status::InternalServerError => Some(500),
152            Status::NotImplemented => Some(501),
153            Status::BadGateway => Some(502),
154            Status::ServiceUnavailable => Some(503),
155            Status::GatewayTimeout => Some(504),
156            Status::HttpVersionNotSupported => Some(505),
157            Status::VariantAlsoNegotiates => Some(506),
158            Status::InsufficientStorage => Some(507),
159            Status::LoopDetected => Some(508),
160            Status::NotExtended => Some(510),
161            Status::NetworkAuthenticationRequired => Some(511)
162        }
163    }
164
165    fn status_text(status_code: usize) -> Option<String> {
166        return match status_code {
167            // Information responses
168            100 => Some("Continue".to_string()),
169            101 => Some("Switching Protocols".to_string()),
170            102 => Some("Processing".to_string()),
171            103 => Some("Early Hints".to_string()),
172
173            // Successful responses
174            200 => Some("OK".to_string()),
175            201 => Some("Created".to_string()),
176            202 => Some("Accepted".to_string()),
177            203 => Some("Non-Authoritative Information".to_string()),
178            204 => Some("No Content".to_string()),
179            205 => Some("Reset Content".to_string()),
180            206 => Some("Partial Content".to_string()),
181            207 => Some("Multi_Status".to_string()),
182            208 => Some("Already Reported".to_string()),
183            226 => Some("IM Used".to_string()),
184
185            // Redirection Messages
186            300 => Some("Multiple Choices".to_string()),
187            301 => Some("Moved permanently".to_string()),
188            302 => Some("Found".to_string()),
189            303 => Some("See Other".to_string()),
190            304 => Some("Not Modified".to_string()),
191            305 => Some("Use Proxy".to_string()),  // Depreciated
192            306 => Some("unused".to_string()), // Depreciated
193            307 => Some("Temporary Redirect".to_string()), // No longer used, just reserved
194            308 => Some("Permanent Redirect".to_string()),
195
196            // Client error responses
197            400 => Some("Bad Request".to_string()),
198            401 => Some("Unauthorized".to_string()), // Experimental. Expect behaviour to change in the future.
199            402 => Some("Payment Required".to_string()),
200            403 => Some("Forbidden".to_string()),
201            404 => Some("Not Found".to_string()),
202            405 => Some("Method Not Allowed".to_string()),
203            406 => Some("Not Acceptable".to_string()),
204            407 => Some("Proxy Authentication Required".to_string()),
205            408 => Some("Request Timeout".to_string()),
206            409 => Some("Conflict".to_string()),
207            410 => Some("Gone".to_string()),
208            411 => Some("Length Required".to_string()),
209            412 => Some("Precondition Failed".to_string()),
210            413 => Some("Payload Too Large".to_string()),
211            414 => Some("URI Too Long".to_string()),
212            415 => Some("Unsupported Media Type".to_string()),
213            416 => Some("Range Not Satisfiable".to_string()),
214            417 => Some("Expectation Failed".to_string()),
215            418 => Some("I',m a teapot".to_string()),
216            421 => Some("Misdirected Request".to_string()),
217            422 => Some("Unprocessable Content".to_string()),
218            423 => Some("Locked".to_string()),
219            424 => Some("Failed Dependency".to_string()), // Experimental. Expect behaviour to change in the future
220            425 => Some("Too Early".to_string()),
221            426 => Some("Upgrade Required".to_string()),
222            428 => Some("Precondition Required".to_string()),
223            429 => Some("Too Many Requests".to_string()),
224            431 => Some("Request Header Fields Too Large".to_string()),
225            451 => Some("Unavailable For Legal Reasons".to_string()),
226
227            // Server error responses
228            500 => Some("Internal Server Error".to_string()),
229            501 => Some("Not Implemented".to_string()),
230            502 => Some("Bad Gateway".to_string()),
231            503 => Some("Service Unavailable".to_string()),
232            504 => Some("Gateway Timeout".to_string()),
233            505 => Some("HTTP Version Not Supported".to_string()),
234            506 => Some("Variant Also Negotiates".to_string()),
235            507 => Some("Insufficient Storage".to_string()),
236            508 => Some("Loop Detected".to_string()),
237            510 => Some("Not Extended".to_string()),
238            511 => Some("Network Authentication Required".to_string()),
239            _ => None
240        };
241    }
242}
243
244
245pub trait StatusCode {
246    fn to_usize(&self) -> usize;
247}
248
249impl StatusCode for Status {
250    fn to_usize(&self) -> usize {
251        let status_code = StatusMethods::status_code(self);
252        return status_code.unwrap();
253    }
254}
255
256impl StatusCode for usize {
257    fn to_usize(&self) -> usize {
258        *self
259    }
260}