Skip to main content

beavuck_hermit/
http_method.rs

1// not tested, as this is a relatively trivial enum
2// and tests would mostly involve just testing the Rust language itself
3#[cfg_attr(test, mutants::skip)]
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum HttpMethod {
6    Get,
7    Post,
8    Put,
9    Patch,
10    Delete,
11    Options,
12    Head,
13    Trace,
14}
15
16#[cfg_attr(test, mutants::skip)] // not tested
17impl HttpMethod {
18    pub const ALL: &'static [Self] = &[
19        Self::Get,
20        Self::Post,
21        Self::Put,
22        Self::Patch,
23        Self::Delete,
24        Self::Options,
25        Self::Head,
26        Self::Trace,
27    ];
28
29    pub fn as_str(self) -> &'static str {
30        match self {
31            Self::Get => "get",
32            Self::Post => "post",
33            Self::Put => "put",
34            Self::Patch => "patch",
35            Self::Delete => "delete",
36            Self::Options => "options",
37            Self::Head => "head",
38            Self::Trace => "trace",
39        }
40    }
41
42    pub fn uses_request_body(self) -> bool {
43        matches!(self, Self::Post | Self::Put | Self::Patch)
44    }
45}
46
47#[cfg_attr(test, mutants::skip)] // not tested
48impl TryFrom<&str> for HttpMethod {
49    type Error = ();
50
51    fn try_from(s: &str) -> Result<Self, Self::Error> {
52        match s {
53            "get" => Ok(Self::Get),
54            "post" => Ok(Self::Post),
55            "put" => Ok(Self::Put),
56            "patch" => Ok(Self::Patch),
57            "delete" => Ok(Self::Delete),
58            "options" => Ok(Self::Options),
59            "head" => Ok(Self::Head),
60            "trace" => Ok(Self::Trace),
61            _ => Err(()),
62        }
63    }
64}