Ferrit Explore
中文·繁體·EN·日本語 Sign in Register
cielxl / veld / src / http / status.rs
use std::fmt;

/// HTTP status code (100-599).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HttpStatusCode(u16);

impl HttpStatusCode {
    // --- 1xx Informational ---
    pub const CONTINUE: Self = Self(100);
    pub const SWITCHING_PROTOCOLS: Self = Self(101);
    pub const PROCESSING: Self = Self(102);
    pub const EARLY_HINTS: Self = Self(103);

    // --- 2xx Success ---
    pub const OK: Self = Self(200);
    pub const CREATED: Self = Self(201);
    pub const ACCEPTED: Self = Self(202);
    pub const NON_AUTHORITATIVE_INFORMATION: Self = Self(203);
    pub const NO_CONTENT: Self = Self(204);
    pub const RESET_CONTENT: Self = Self(205);
    pub const PARTIAL_CONTENT: Self = Self(206);
    pub const MULTI_STATUS: Self = Self(207);
    pub const ALREADY_REPORTED: Self = Self(208);
    pub const IM_USED: Self = Self(226);

    // --- 3xx Redirection ---
    pub const MULTIPLE_CHOICES: Self = Self(300);
    pub const MOVED_PERMANENTLY: Self = Self(301);
    pub const FOUND: Self = Self(302);
    pub const SEE_OTHER: Self = Self(303);
    pub const NOT_MODIFIED: Self = Self(304);
    pub const USE_PROXY: Self = Self(305);
    pub const TEMPORARY_REDIRECT: Self = Self(307);
    pub const PERMANENT_REDIRECT: Self = Self(308);

    // --- 4xx Client Error ---
    pub const BAD_REQUEST: Self = Self(400);
    pub const UNAUTHORIZED: Self = Self(401);
    pub const PAYMENT_REQUIRED: Self = Self(402);
    pub const FORBIDDEN: Self = Self(403);
    pub const NOT_FOUND: Self = Self(404);
    pub const METHOD_NOT_ALLOWED: Self = Self(405);
    pub const NOT_ACCEPTABLE: Self = Self(406);
    pub const PROXY_AUTHENTICATION_REQUIRED: Self = Self(407);
    pub const REQUEST_TIMEOUT: Self = Self(408);
    pub const CONFLICT: Self = Self(409);
    pub const GONE: Self = Self(410);
    pub const LENGTH_REQUIRED: Self = Self(411);
    pub const PRECONDITION_FAILED: Self = Self(412);
    pub const CONTENT_TOO_LARGE: Self = Self(413);
    pub const URI_TOO_LONG: Self = Self(414);
    pub const UNSUPPORTED_MEDIA_TYPE: Self = Self(415);
    pub const RANGE_NOT_SATISFIABLE: Self = Self(416);
    pub const EXPECTATION_FAILED: Self = Self(417);
    pub const IM_A_TEAPOT: Self = Self(418);
    pub const MISDIRECTED_REQUEST: Self = Self(421);
    pub const UNPROCESSABLE_CONTENT: Self = Self(422);
    pub const LOCKED: Self = Self(423);
    pub const FAILED_DEPENDENCY: Self = Self(424);
    pub const TOO_EARLY: Self = Self(425);
    pub const UPGRADE_REQUIRED: Self = Self(426);
    pub const PRECONDITION_REQUIRED: Self = Self(428);
    pub const TOO_MANY_REQUESTS: Self = Self(429);
    pub const REQUEST_HEADER_FIELDS_TOO_LARGE: Self = Self(431);
    pub const UNAVAILABLE_FOR_LEGAL_REASONS: Self = Self(451);

    // --- 5xx Server Error ---
    pub const INTERNAL_SERVER_ERROR: Self = Self(500);
    pub const NOT_IMPLEMENTED: Self = Self(501);
    pub const BAD_GATEWAY: Self = Self(502);
    pub const SERVICE_UNAVAILABLE: Self = Self(503);
    pub const GATEWAY_TIMEOUT: Self = Self(504);
    pub const HTTP_VERSION_NOT_SUPPORTED: Self = Self(505);
    pub const VARIANT_ALSO_NEGOTIATES: Self = Self(506);
    pub const INSUFFICIENT_STORAGE: Self = Self(507);
    pub const LOOP_DETECTED: Self = Self(508);
    pub const NOT_EXTENDED: Self = Self(510);
    pub const NETWORK_AUTHENTICATION_REQUIRED: Self = Self(511);

    /// Create an `HttpStatusCode` from a raw `u16`.
    ///
    /// Returns `None` if the code is outside the valid range 100..=599.
    #[inline]
    pub const fn from_u16(code: u16) -> Option<Self> {
        if code >= 100 && code <= 599 {
            Some(Self(code))
        } else {
            None
        }
    }

    /// Create an `HttpStatusCode` without range checking.
    ///
    /// # Safety
    /// Caller must ensure `code` is in 100..=599.
    #[inline]
    pub const fn from_u16_unchecked(code: u16) -> Self {
        Self(code)
    }

    /// Return the numeric value of this status code.
    #[inline]
    pub const fn as_u16(self) -> u16 {
        self.0
    }

    /// Return the standard reason phrase for this status code.
    pub fn reason_phrase(self) -> &'static str {
        match self.0 {
            100 => "Continue",
            101 => "Switching Protocols",
            102 => "Processing",
            103 => "Early Hints",

            200 => "OK",
            201 => "Created",
            202 => "Accepted",
            203 => "Non-Authoritative Information",
            204 => "No Content",
            205 => "Reset Content",
            206 => "Partial Content",
            207 => "Multi-Status",
            208 => "Already Reported",
            226 => "IM Used",

            300 => "Multiple Choices",
            301 => "Moved Permanently",
            302 => "Found",
            303 => "See Other",
            304 => "Not Modified",
            305 => "Use Proxy",
            307 => "Temporary Redirect",
            308 => "Permanent Redirect",

            400 => "Bad Request",
            401 => "Unauthorized",
            402 => "Payment Required",
            403 => "Forbidden",
            404 => "Not Found",
            405 => "Method Not Allowed",
            406 => "Not Acceptable",
            407 => "Proxy Authentication Required",
            408 => "Request Timeout",
            409 => "Conflict",
            410 => "Gone",
            411 => "Length Required",
            412 => "Precondition Failed",
            413 => "Content Too Large",
            414 => "URI Too Long",
            415 => "Unsupported Media Type",
            416 => "Range Not Satisfiable",
            417 => "Expectation Failed",
            418 => "I'm a Teapot",
            421 => "Misdirected Request",
            422 => "Unprocessable Content",
            423 => "Locked",
            424 => "Failed Dependency",
            425 => "Too Early",
            426 => "Upgrade Required",
            428 => "Precondition Required",
            429 => "Too Many Requests",
            431 => "Request Header Fields Too Large",
            451 => "Unavailable For Legal Reasons",

            500 => "Internal Server Error",
            501 => "Not Implemented",
            502 => "Bad Gateway",
            503 => "Service Unavailable",
            504 => "Gateway Timeout",
            505 => "HTTP Version Not Supported",
            506 => "Variant Also Negotiates",
            507 => "Insufficient Storage",
            508 => "Loop Detected",
            510 => "Not Extended",
            511 => "Network Authentication Required",

            _ => "Unknown",
        }
    }

    /// Return `true` if this is a 1xx informational status.
    #[inline]
    pub const fn is_informational(self) -> bool {
        self.0 >= 100 && self.0 < 200
    }

    /// Return `true` if this is a 2xx success status.
    #[inline]
    pub const fn is_success(self) -> bool {
        self.0 >= 200 && self.0 < 300
    }

    /// Return `true` if this is a 3xx redirection status.
    #[inline]
    pub const fn is_redirection(self) -> bool {
        self.0 >= 300 && self.0 < 400
    }

    /// Return `true` if this is a 4xx client error status.
    #[inline]
    pub const fn is_client_error(self) -> bool {
        self.0 >= 400 && self.0 < 500
    }

    /// Return `true` if this is a 5xx server error status.
    #[inline]
    pub const fn is_server_error(self) -> bool {
        self.0 >= 500 && self.0 < 600
    }

    /// Return `true` if this status code indicates an error (4xx or 5xx).
    #[inline]
    pub const fn is_error(self) -> bool {
        self.0 >= 400
    }
}

impl fmt::Display for HttpStatusCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.0, self.reason_phrase())
    }
}

impl From<u16> for HttpStatusCode {
    /// Convert a `u16` to an `HttpStatusCode`.
    ///
    /// Values outside 100..=599 are clamped to the nearest boundary
    /// (100 for values below, 599 for values above).
    fn from(code: u16) -> Self {
        Self(code.clamp(100, 599))
    }
}

impl From<HttpStatusCode> for u16 {
    #[inline]
    fn from(status: HttpStatusCode) -> Self {
        status.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display_format() {
        assert_eq!(HttpStatusCode::OK.to_string(), "200 OK");
        assert_eq!(HttpStatusCode::NOT_FOUND.to_string(), "404 Not Found");
        assert_eq!(
            HttpStatusCode::INTERNAL_SERVER_ERROR.to_string(),
            "500 Internal Server Error"
        );
    }

    #[test]
    fn reason_phrase() {
        assert_eq!(HttpStatusCode::OK.reason_phrase(), "OK");
        assert_eq!(
            HttpStatusCode::MOVED_PERMANENTLY.reason_phrase(),
            "Moved Permanently"
        );
        assert_eq!(
            HttpStatusCode::SERVICE_UNAVAILABLE.reason_phrase(),
            "Service Unavailable"
        );
    }

    #[test]
    fn from_u16_valid() {
        assert_eq!(HttpStatusCode::from_u16(200), Some(HttpStatusCode::OK));
        assert_eq!(
            HttpStatusCode::from_u16(404),
            Some(HttpStatusCode::NOT_FOUND)
        );
        assert_eq!(
            HttpStatusCode::from_u16(100),
            Some(HttpStatusCode::CONTINUE)
        );
        assert_eq!(HttpStatusCode::from_u16(599), Some(HttpStatusCode(599)));
    }

    #[test]
    fn from_u16_invalid() {
        assert_eq!(HttpStatusCode::from_u16(0), None);
        assert_eq!(HttpStatusCode::from_u16(99), None);
        assert_eq!(HttpStatusCode::from_u16(600), None);
        assert_eq!(HttpStatusCode::from_u16(u16::MAX), None);
    }

    #[test]
    fn from_u16_clamping() {
        let s: HttpStatusCode = 50u16.into();
        assert_eq!(s.as_u16(), 100);
        let s: HttpStatusCode = 999u16.into();
        assert_eq!(s.as_u16(), 599);
    }

    #[test]
    fn category_checks() {
        assert!(HttpStatusCode::CONTINUE.is_informational());
        assert!(!HttpStatusCode::CONTINUE.is_success());

        assert!(HttpStatusCode::OK.is_success());
        assert!(!HttpStatusCode::OK.is_error());

        assert!(HttpStatusCode::MOVED_PERMANENTLY.is_redirection());

        assert!(HttpStatusCode::NOT_FOUND.is_client_error());
        assert!(HttpStatusCode::NOT_FOUND.is_error());

        assert!(HttpStatusCode::INTERNAL_SERVER_ERROR.is_server_error());
        assert!(HttpStatusCode::INTERNAL_SERVER_ERROR.is_error());
    }

    #[test]
    fn roundtrip_u16() {
        let original = HttpStatusCode::OK;
        let raw: u16 = original.into();
        assert_eq!(raw, 200u16);
    }
}