1use serde::{Serialize, Deserialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10pub enum Status {
11 OK = 200,
13 Created = 201,
14 Accepted = 202,
15 NoContent = 204,
16
17 MovedPermanently = 301,
19 Found = 302,
20 SeeOther = 303,
21 NotModified = 304,
22 TemporaryRedirect = 307,
23 PermanentRedirect = 308,
24
25 BadRequest = 400,
27 Unauthorized = 401,
28 Forbidden = 403,
29 NotFound = 404,
30 MethodNotAllowed = 405,
31 NotAcceptable = 406,
32 Conflict = 409,
33 Gone = 410,
34 PreconditionFailed = 412,
35 UnsupportedMediaType = 415,
36 UnprocessableEntity = 422,
37 TooManyRequests = 429,
38
39 InternalServerError = 500,
41 NotImplemented = 501,
42 BadGateway = 502,
43 ServiceUnavailable = 503,
44 GatewayTimeout = 504,
45}
46
47impl Status {
48 pub fn is_success(&self) -> bool {
50 (200..300).contains(&(*self as u16))
51 }
52
53 pub fn is_redirection(&self) -> bool {
54 (300..400).contains(&(*self as u16))
55 }
56
57 pub fn is_client_error(&self) -> bool {
58 (400..500).contains(&(*self as u16))
59 }
60
61 pub fn is_server_error(&self) -> bool {
62 (500..600).contains(&(*self as u16))
63 }
64
65 pub fn to_u16(&self) -> u16 {
66 *self as u16
67 }
68}
69
70impl From<u16> for Status {
71 fn from(code: u16) -> Self {
72 match code {
73 200 => Status::OK,
74 201 => Status::Created,
75 202 => Status::Accepted,
76 204 => Status::NoContent,
77 301 => Status::MovedPermanently,
78 302 => Status::Found,
79 303 => Status::SeeOther,
80 304 => Status::NotModified,
81 307 => Status::TemporaryRedirect,
82 308 => Status::PermanentRedirect,
83 400 => Status::BadRequest,
84 401 => Status::Unauthorized,
85 403 => Status::Forbidden,
86 404 => Status::NotFound,
87 405 => Status::MethodNotAllowed,
88 406 => Status::NotAcceptable,
89 409 => Status::Conflict,
90 410 => Status::Gone,
91 412 => Status::PreconditionFailed,
92 415 => Status::UnsupportedMediaType,
93 422 => Status::UnprocessableEntity,
94 429 => Status::TooManyRequests,
95 500 => Status::InternalServerError,
96 501 => Status::NotImplemented,
97 502 => Status::BadGateway,
98 503 => Status::ServiceUnavailable,
99 504 => Status::GatewayTimeout,
100 _ => Status::InternalServerError, }
102 }
103}