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    pub(crate) const fn as_bytes(self) -> &'static [u8] {
26        match self {
27            Self::Get => b"GET",
28            Self::Post => b"POST",
29            Self::Put => b"PUT",
30            Self::Patch => b"PATCH",
31            Self::Delete => b"DELETE",
32            Self::Head => b"HEAD",
33            Self::Options => b"OPTIONS",
34        }
35    }
36
37    pub(crate) fn from_bytes(value: &[u8]) -> Result<Self, Error> {
38        match value {
39            b"GET" => Ok(Self::Get),
40            b"POST" => Ok(Self::Post),
41            b"PUT" => Ok(Self::Put),
42            b"PATCH" => Ok(Self::Patch),
43            b"DELETE" => Ok(Self::Delete),
44            b"HEAD" => Ok(Self::Head),
45            b"OPTIONS" => Ok(Self::Options),
46            _ => Err(Error::UnsupportedMethod),
47        }
48    }
49}