pillow_http/
http_methods.rs

1/// Htpp Methods
2///
3/// Like GET, POST, ...
4///
5/// Not supported all methods yet
6#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
7pub enum HttpMethods {
8    GET,
9    POST,
10    PUT,
11    DELETE,
12    HEAD,
13    OPTIONS,
14    CONNECT,
15    PATCH,
16    TRACE,
17}
18
19impl HttpMethods {
20    /// Convert HttpMethods in &str
21    ///
22    /// ```
23    /// let method_str = HttpMethods::GET;
24    /// assert_eq(method_str.as_str(), "GET")
25    /// ```
26    pub fn as_str(&self) -> &'static str {
27        match self {
28            HttpMethods::GET => "GET",
29            HttpMethods::POST => "POST",
30            HttpMethods::PUT => "PUT",
31            HttpMethods::DELETE => "DELETE",
32
33            HttpMethods::HEAD => "HEAD",
34            HttpMethods::OPTIONS => "OPTIONS",
35            HttpMethods::CONNECT => "CONNECT",
36            HttpMethods::PATCH => "PATCH",
37            HttpMethods::TRACE => "TRACE",
38        }
39    }
40}
41
42/// Convert HttpMethods in &str
43///
44/// ```
45/// let method_str = "GET";
46/// assert_eq(from_str_to_http_method(method_str).unwrap(), HttpMethods::GET)
47/// ```
48pub fn from_str_to_http_method(s: &str) -> Result<HttpMethods, ()> {
49    match s {
50        "GET" => Ok(HttpMethods::GET),
51        "POST" => Ok(HttpMethods::POST),
52        "PUT" => Ok(HttpMethods::PUT),
53        "DELETE" => Ok(HttpMethods::DELETE),
54
55        "HEAD" => Ok(HttpMethods::HEAD),
56        "OPTIONS" => Ok(HttpMethods::OPTIONS),
57        "CONNECT" => Ok(HttpMethods::CONNECT),
58        "PATCH" => Ok(HttpMethods::PATCH),
59        "TRACE" => Ok(HttpMethods::TRACE),
60
61        _ => Err(()),
62    }
63}