http1/
method.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::str::FromStr;

/// Represents a request method.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Method {
    GET,
    POST,
    PUT,
    DELETE,
    PATCH,
    OPTIONS,
    HEAD,
    CONNECT,
    TRACE,
    ExtensionMethod(String),
}

impl Method {
    /// Returns this method as str.
    pub fn as_str(&self) -> &str {
        match self {
            Method::GET => "GET",
            Method::POST => "POST",
            Method::PUT => "PUT",
            Method::DELETE => "DELETE",
            Method::PATCH => "PATCH",
            Method::OPTIONS => "OPTIONS",
            Method::HEAD => "HEAD",
            Method::CONNECT => "CONNECT",
            Method::TRACE => "TRACE",
            Method::ExtensionMethod(ext) => ext.as_str(),
        }
    }
}

#[derive(Debug)]
pub struct InvalidMethod {
    _priv: (),
}

impl<'a> From<&'a str> for Method {
    fn from(value: &'a str) -> Self {
        match value {
            v if v.eq_ignore_ascii_case("GET") => Method::GET,
            v if v.eq_ignore_ascii_case("POST") => Method::POST,
            v if v.eq_ignore_ascii_case("PUT") => Method::PUT,
            v if v.eq_ignore_ascii_case("DELETE") => Method::DELETE,
            v if v.eq_ignore_ascii_case("PATCH") => Method::PATCH,
            v if v.eq_ignore_ascii_case("OPTIONS") => Method::OPTIONS,
            v if v.eq_ignore_ascii_case("HEAD") => Method::HEAD,
            v if v.eq_ignore_ascii_case("CONNECT") => Method::CONNECT,
            v if v.eq_ignore_ascii_case("TRACE") => Method::TRACE,
            _ => Method::ExtensionMethod(value.to_string()),
        }
    }
}

impl<'a> From<&'a String> for Method {
    fn from(value: &'a String) -> Self {
        Method::from(value.as_str())
    }
}

impl From<String> for Method {
    fn from(value: String) -> Self {
        Method::from(value.as_str())
    }
}

impl FromStr for Method {
    type Err = InvalidMethod;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            v if v.eq_ignore_ascii_case("GET") => Ok(Method::GET),
            v if v.eq_ignore_ascii_case("POST") => Ok(Method::POST),
            v if v.eq_ignore_ascii_case("PUT") => Ok(Method::PUT),
            v if v.eq_ignore_ascii_case("DELETE") => Ok(Method::DELETE),
            v if v.eq_ignore_ascii_case("PATCH") => Ok(Method::PATCH),
            v if v.eq_ignore_ascii_case("OPTIONS") => Ok(Method::OPTIONS),
            v if v.eq_ignore_ascii_case("HEAD") => Ok(Method::HEAD),
            v if v.eq_ignore_ascii_case("CONNECT") => Ok(Method::CONNECT),
            v if v.eq_ignore_ascii_case("TRACE") => Ok(Method::TRACE),
            _ => Err(InvalidMethod { _priv: () }),
        }
    }
}

// Implement the Display trait for the Method enum
impl std::fmt::Display for Method {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<'a> PartialEq<Method> for &'a Method {
    fn eq(&self, other: &Method) -> bool {
        *self == other
    }
}