azure_functions/http/
status.rs

1/// Represents a HTTP status code.
2#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq)]
3pub struct Status(u16);
4
5macro_rules! statuses {
6    ($($name:ident => $code:expr => $code_str:expr),+) => {
7        $(
8            #[doc="[Status](struct.Status.html) with code <b>"]
9            #[doc=$code_str]
10            #[doc="</b>."]
11            #[allow(non_upper_case_globals)]
12            pub const $name: Status = Status($code);
13         )+
14
15        /// Returns a `Status` given a status code `code`.
16        ///
17        /// # Examples
18        ///
19        /// Create a `Status` from a status code `code`:
20        ///
21        /// ```rust
22        /// use azure_functions::http::Status;
23        ///
24        /// assert_eq!(Status::from_code(404), Status::NotFound);
25        /// ```
26        pub fn from_code(code: u16) -> Self {
27            match code {
28                $($code => Status::$name,)+
29                _ => Status(code)
30            }
31        }
32    };
33}
34
35impl Status {
36    statuses! {
37        Continue => 100 => "100",
38        SwitchingProtocols => 101 => "101",
39        Processing => 102 => "102",
40        Ok => 200 => "200",
41        Created => 201 => "201",
42        Accepted => 202 => "202",
43        NonAuthoritativeInformation => 203 => "203",
44        NoContent => 204 => "204",
45        ResetContent => 205 => "205",
46        PartialContent => 206 => "206",
47        MultiStatus => 207 => "207",
48        AlreadyReported => 208 => "208",
49        ImUsed => 226 => "226",
50        MultipleChoices => 300 => "300",
51        MovedPermanently => 301 => "301",
52        Found => 302 => "302",
53        SeeOther => 303 => "303",
54        NotModified => 304 => "304",
55        UseProxy => 305 => "305",
56        TemporaryRedirect => 307 => "307",
57        PermanentRedirect => 308 => "308",
58        BadRequest => 400 => "400",
59        Unauthorized => 401 => "401",
60        PaymentRequired => 402 => "402",
61        Forbidden => 403 => "403",
62        NotFound => 404 => "404",
63        MethodNotAllowed => 405 => "405",
64        NotAcceptable => 406 => "406",
65        ProxyAuthenticationRequired => 407 => "407",
66        RequestTimeout => 408 => "408",
67        Conflict => 409 => "409",
68        Gone => 410 => "410",
69        LengthRequired => 411 => "411",
70        PreconditionFailed => 412 => "412",
71        PayloadTooLarge => 413 => "413",
72        UriTooLong => 414 => "414",
73        UnsupportedMediaType => 415 => "415",
74        RangeNotSatisfiable => 416 => "416",
75        ExpectationFailed => 417 => "417",
76        ImATeapot => 418 => "418",
77        MisdirectedRequest => 421 => "421",
78        UnprocessableEntity => 422 => "422",
79        Locked => 423 => "423",
80        FailedDependency => 424 => "424",
81        UpgradeRequired => 426 => "426",
82        PreconditionRequired => 428 => "428",
83        TooManyRequests => 429 => "429",
84        RequestHeaderFieldsTooLarge => 431 => "431",
85        UnavailableForLegalReasons => 451 => "451",
86        InternalServerError => 500 => "500",
87        NotImplemented => 501 => "501",
88        BadGateway => 502 => "502",
89        ServiceUnavailable => 503 => "503",
90        GatewayTimeout => 504 => "504",
91        HttpVersionNotSupported => 505 => "505",
92        VariantAlsoNegotiates => 506 => "506",
93        InsufficientStorage => 507 => "507",
94        LoopDetected => 508 => "508",
95        NotExtended => 510 => "510",
96        NetworkAuthenticationRequired => 511 => "511"
97    }
98}
99
100impl ToString for Status {
101    fn to_string(&self) -> String {
102        self.0.to_string()
103    }
104}
105
106impl From<u16> for Status {
107    fn from(code: u16) -> Self {
108        Status::from_code(code)
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn it_converts_to_string() {
118        assert_eq!(Status::Ok.to_string(), "200");
119        assert_eq!(Status::NotFound.to_string(), "404");
120    }
121
122    #[test]
123    fn it_converts_from_code() {
124        let status: Status = 200.into();
125        assert_eq!(status, Status::Ok);
126        assert_eq!(Status::from_code(404), Status::NotFound);
127    }
128}