Skip to main content

rustlavel_http/
method.rs

1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
4pub enum Method {
5    Get,
6    Head,
7    Post,
8    Put,
9    Patch,
10    Delete,
11    Options,
12}
13
14impl Method {
15    pub fn as_str(self) -> &'static str {
16        match self {
17            Method::Get => "GET",
18            Method::Head => "HEAD",
19            Method::Post => "POST",
20            Method::Put => "PUT",
21            Method::Patch => "PATCH",
22            Method::Delete => "DELETE",
23            Method::Options => "OPTIONS",
24        }
25    }
26
27    pub fn parse(value: &str) -> Option<Method> {
28        match value {
29            "GET" => Some(Method::Get),
30            "HEAD" => Some(Method::Head),
31            "POST" => Some(Method::Post),
32            "PUT" => Some(Method::Put),
33            "PATCH" => Some(Method::Patch),
34            "DELETE" => Some(Method::Delete),
35            "OPTIONS" => Some(Method::Options),
36            _ => None,
37        }
38    }
39
40    /// Whether this method is expected to carry a body.
41    pub fn takes_body(self) -> bool {
42        matches!(self, Method::Post | Method::Put | Method::Patch)
43    }
44}
45
46impl fmt::Display for Method {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self.as_str())
49    }
50}