Skip to main content

hpke_http/
method.rs

1//! Supported HTTP request methods.
2
3use crate::Error;
4
5/// Supported HTTP request methods.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum Method {
8    /// GET.
9    Get,
10    /// POST.
11    Post,
12    /// PUT.
13    Put,
14    /// PATCH.
15    Patch,
16    /// DELETE.
17    Delete,
18    /// HEAD.
19    Head,
20    /// OPTIONS.
21    Options,
22}
23
24impl Method {
25    /// Return the method name used at the HTTP boundary.
26    #[must_use]
27    pub const fn as_str(self) -> &'static str {
28        match self {
29            Self::Get => "GET",
30            Self::Post => "POST",
31            Self::Put => "PUT",
32            Self::Patch => "PATCH",
33            Self::Delete => "DELETE",
34            Self::Head => "HEAD",
35            Self::Options => "OPTIONS",
36        }
37    }
38
39    pub(crate) const fn as_bytes(self) -> &'static [u8] {
40        self.as_str().as_bytes()
41    }
42
43    /// Read a supported HTTP method name.
44    ///
45    /// # Errors
46    ///
47    /// Returns [`Error::UnsupportedMethod`] for any other name.
48    pub fn from_bytes(value: &[u8]) -> Result<Self, Error> {
49        match value {
50            b"GET" => Ok(Self::Get),
51            b"POST" => Ok(Self::Post),
52            b"PUT" => Ok(Self::Put),
53            b"PATCH" => Ok(Self::Patch),
54            b"DELETE" => Ok(Self::Delete),
55            b"HEAD" => Ok(Self::Head),
56            b"OPTIONS" => Ok(Self::Options),
57            _ => Err(Error::UnsupportedMethod),
58        }
59    }
60}