Skip to main content

pipa/http/
method.rs

1use core::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum HttpMethod {
5    GET,
6    POST,
7    PUT,
8    DELETE,
9    PATCH,
10    HEAD,
11    OPTIONS,
12}
13
14impl HttpMethod {
15    pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
16        match bytes {
17            b"GET" => Some(HttpMethod::GET),
18            b"POST" => Some(HttpMethod::POST),
19            b"PUT" => Some(HttpMethod::PUT),
20            b"DELETE" => Some(HttpMethod::DELETE),
21            b"PATCH" => Some(HttpMethod::PATCH),
22            b"HEAD" => Some(HttpMethod::HEAD),
23            b"OPTIONS" => Some(HttpMethod::OPTIONS),
24            _ => None,
25        }
26    }
27
28    pub fn as_str(&self) -> &'static str {
29        match self {
30            HttpMethod::GET => "GET",
31            HttpMethod::POST => "POST",
32            HttpMethod::PUT => "PUT",
33            HttpMethod::DELETE => "DELETE",
34            HttpMethod::PATCH => "PATCH",
35            HttpMethod::HEAD => "HEAD",
36            HttpMethod::OPTIONS => "OPTIONS",
37        }
38    }
39}
40
41impl fmt::Display for HttpMethod {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.write_str(self.as_str())
44    }
45}