Skip to main content

a3s_boot/http/
method.rs

1use crate::BootError;
2use std::fmt;
3use std::str::FromStr;
4
5/// HTTP method understood by Boot route definitions.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum HttpMethod {
8    Get,
9    Post,
10    Put,
11    Patch,
12    Delete,
13    Options,
14    Head,
15}
16
17impl HttpMethod {
18    pub fn as_str(self) -> &'static str {
19        match self {
20            Self::Get => "GET",
21            Self::Post => "POST",
22            Self::Put => "PUT",
23            Self::Patch => "PATCH",
24            Self::Delete => "DELETE",
25            Self::Options => "OPTIONS",
26            Self::Head => "HEAD",
27        }
28    }
29}
30
31impl fmt::Display for HttpMethod {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.write_str(self.as_str())
34    }
35}
36
37impl FromStr for HttpMethod {
38    type Err = BootError;
39
40    fn from_str(method: &str) -> std::result::Result<Self, Self::Err> {
41        match method {
42            "GET" => Ok(Self::Get),
43            "POST" => Ok(Self::Post),
44            "PUT" => Ok(Self::Put),
45            "PATCH" => Ok(Self::Patch),
46            "DELETE" => Ok(Self::Delete),
47            "OPTIONS" => Ok(Self::Options),
48            "HEAD" => Ok(Self::Head),
49            method => Err(BootError::MethodNotAllowed(method.to_string())),
50        }
51    }
52}