problem_spec/
status.rs

1use serde::{Serialize, Deserialize};
2
3/// HttpStatus const
4/// 这个 Status 枚举包含了常用的 HTTP 状态码,并提供了一些有用的方法:
5// is_success(), is_redirection(), is_client_error(), is_server_error(): 用于快速判断状态码的类型。
6// to_u16(): 将状态码转换为 u16 类型。
7// From<u16> trait 实现:允许从 u16 创建 Status 枚举。
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10pub enum Status {
11    // 2xx Success
12    OK = 200,
13    Created = 201,
14    Accepted = 202,
15    NoContent = 204,
16
17    // 3xx Redirection
18    MovedPermanently = 301,
19    Found = 302,
20    SeeOther = 303,
21    NotModified = 304,
22    TemporaryRedirect = 307,
23    PermanentRedirect = 308,
24
25    // 4xx Client Errors
26    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    // 5xx Server Errors
40    InternalServerError = 500,
41    NotImplemented = 501,
42    BadGateway = 502,
43    ServiceUnavailable = 503,
44    GatewayTimeout = 504,
45}
46
47impl Status {
48    ///method to check http status for common cases.
49    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, // 默认值,可以根据需要调整
101        }
102    }
103}