problem-spec 0.2.0

problem spec(https://www.rfc-editor.org/rfc/rfc7807) lib in rust
Documentation
use serde::{Serialize, Deserialize};

/// HttpStatus const
/// 这个 Status 枚举包含了常用的 HTTP 状态码,并提供了一些有用的方法:
// is_success(), is_redirection(), is_client_error(), is_server_error(): 用于快速判断状态码的类型。
// to_u16(): 将状态码转换为 u16 类型。
// From<u16> trait 实现:允许从 u16 创建 Status 枚举。

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Status {
    // 2xx Success
    OK = 200,
    Created = 201,
    Accepted = 202,
    NoContent = 204,

    // 3xx Redirection
    MovedPermanently = 301,
    Found = 302,
    SeeOther = 303,
    NotModified = 304,
    TemporaryRedirect = 307,
    PermanentRedirect = 308,

    // 4xx Client Errors
    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    MethodNotAllowed = 405,
    NotAcceptable = 406,
    Conflict = 409,
    Gone = 410,
    PreconditionFailed = 412,
    UnsupportedMediaType = 415,
    UnprocessableEntity = 422,
    TooManyRequests = 429,

    // 5xx Server Errors
    InternalServerError = 500,
    NotImplemented = 501,
    BadGateway = 502,
    ServiceUnavailable = 503,
    GatewayTimeout = 504,
}

impl Status {
    ///method to check http status for common cases.
    pub fn is_success(&self) -> bool {
        (200..300).contains(&(*self as u16))
    }

    pub fn is_redirection(&self) -> bool {
        (300..400).contains(&(*self as u16))
    }

    pub fn is_client_error(&self) -> bool {
        (400..500).contains(&(*self as u16))
    }

    pub fn is_server_error(&self) -> bool {
        (500..600).contains(&(*self as u16))
    }

    pub fn to_u16(&self) -> u16 {
        *self as u16
    }
}

impl From<u16> for Status {
    fn from(code: u16) -> Self {
        match code {
            200 => Status::OK,
            201 => Status::Created,
            202 => Status::Accepted,
            204 => Status::NoContent,
            301 => Status::MovedPermanently,
            302 => Status::Found,
            303 => Status::SeeOther,
            304 => Status::NotModified,
            307 => Status::TemporaryRedirect,
            308 => Status::PermanentRedirect,
            400 => Status::BadRequest,
            401 => Status::Unauthorized,
            403 => Status::Forbidden,
            404 => Status::NotFound,
            405 => Status::MethodNotAllowed,
            406 => Status::NotAcceptable,
            409 => Status::Conflict,
            410 => Status::Gone,
            412 => Status::PreconditionFailed,
            415 => Status::UnsupportedMediaType,
            422 => Status::UnprocessableEntity,
            429 => Status::TooManyRequests,
            500 => Status::InternalServerError,
            501 => Status::NotImplemented,
            502 => Status::BadGateway,
            503 => Status::ServiceUnavailable,
            504 => Status::GatewayTimeout,
            _ => Status::InternalServerError, // 默认值,可以根据需要调整
        }
    }
}