stream_httparse/
method.rs

1/// The different HTTP-Methods as defined by
2/// [RFC 2616 5.1.1](https://tools.ietf.org/html/rfc2616#section-5.1.1)
3#[derive(Debug, PartialEq, Clone)]
4pub enum Method {
5    /// Requests the Communication-Options available
6    /// for a given Ressource
7    OPTIONS,
8    /// Retrieves the specified Ressource from the Server
9    GET,
10    /// Identical to the GET-Method, but the Server is not
11    /// required to return a Response-Body
12    HEAD,
13    /// Used to post Data to the Server
14    POST,
15    /// Tells the Server to store the supplied Body under a
16    /// given Ressource URI
17    PUT,
18    /// Requests that the given Data assosicated with the
19    /// Ressource-URI is deleted
20    DELETE,
21    /// Used to invoke a remote application-layer loopback
22    TRACE,
23    /// Reserved
24    CONNECT,
25}
26
27impl Method {
28    /// Parses the raw Method into one of the known Methods,
29    /// returns None if the Method is unknown
30    pub fn parse(raw_method: &str) -> Option<Method> {
31        match raw_method {
32            "OPTIONS" => Some(Method::OPTIONS),
33            "GET" => Some(Method::GET),
34            "HEAD" => Some(Method::HEAD),
35            "POST" => Some(Method::POST),
36            "PUT" => Some(Method::PUT),
37            "DELETE" => Some(Method::DELETE),
38            "TRACE" => Some(Method::TRACE),
39            "CONNECT" => Some(Method::CONNECT),
40            _ => None,
41        }
42    }
43
44    /// Serializes the Method into a static String
45    /// for that Method
46    pub fn serialize(&self) -> &'static str {
47        match *self {
48            Method::OPTIONS => "OPTIONS",
49            Method::GET => "GET",
50            Method::HEAD => "HEAD",
51            Method::POST => "POST",
52            Method::PUT => "PUT",
53            Method::DELETE => "DELETE",
54            Method::TRACE => "TRACE",
55            Method::CONNECT => "CONNECT",
56        }
57    }
58}
59
60impl std::fmt::Display for Method {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{}", self.serialize())
63    }
64}
65
66#[cfg(feature = "wasm_serialize")]
67impl Method {
68    /// Serializes the header into a WASM friendly form
69    pub fn wasm_serialize(&self) -> i32 {
70        match *self {
71            Self::OPTIONS => 0,
72            Self::GET => 1,
73            Self::HEAD => 2,
74            Self::POST => 3,
75            Self::PUT => 4,
76            Self::DELETE => 5,
77            Self::TRACE => 6,
78            Self::CONNECT => 7,
79        }
80    }
81
82    /// Deserializes the Output from the `wasm_serialize` method
83    /// back into a valid Method
84    pub fn wasm_deserialize(tmp: i32) -> Option<Self> {
85        match tmp {
86            0 => Some(Self::OPTIONS),
87            1 => Some(Self::GET),
88            2 => Some(Self::HEAD),
89            3 => Some(Self::POST),
90            4 => Some(Self::PUT),
91            5 => Some(Self::DELETE),
92            6 => Some(Self::TRACE),
93            7 => Some(Self::CONNECT),
94            _ => None,
95        }
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn parse_method_options() {
105        assert_eq!(Some(Method::OPTIONS), Method::parse("OPTIONS"));
106    }
107    #[test]
108    fn parse_method_get() {
109        assert_eq!(Some(Method::GET), Method::parse("GET"));
110    }
111    #[test]
112    fn parse_method_head() {
113        assert_eq!(Some(Method::HEAD), Method::parse("HEAD"));
114    }
115    #[test]
116    fn parse_method_post() {
117        assert_eq!(Some(Method::POST), Method::parse("POST"));
118    }
119    #[test]
120    fn parse_method_put() {
121        assert_eq!(Some(Method::PUT), Method::parse("PUT"));
122    }
123    #[test]
124    fn parse_method_delete() {
125        assert_eq!(Some(Method::DELETE), Method::parse("DELETE"));
126    }
127    #[test]
128    fn parse_method_trace() {
129        assert_eq!(Some(Method::TRACE), Method::parse("TRACE"));
130    }
131    #[test]
132    fn parse_method_connect() {
133        assert_eq!(Some(Method::CONNECT), Method::parse("CONNECT"));
134    }
135    #[test]
136    fn parse_method_invalid() {
137        assert_eq!(None, Method::parse("DIFFERENT"));
138    }
139}