1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Provides functionality for handling HTTP methods.
use super::request::RequestError;
use std::fmt::Display;
/// Represents an HTTP method.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Method {
/// The `GET` method.
Get,
/// The `POST` method.
Post,
/// The `PUT` method.
Put,
/// The `DELETE` method.
Delete,
/// The `OPTIONS` method.
Options,
}
impl Method {
/// Attempts to convert from the HTTP verb into an enum variant.
///
/// ## Example
/// ```
/// let method = Method::from_name("GET");
/// assert_eq!(method, Method::Get);
/// ```
pub fn from_name(name: &str) -> Result<Self, RequestError> {
match name {
"GET" => Ok(Self::Get),
"POST" => Ok(Self::Post),
"PUT" => Ok(Self::Put),
"DELETE" => Ok(Self::Delete),
"OPTIONS" => Ok(Self::Options),
_ => Err(RequestError::Request),
}
}
}
impl Display for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Method::Get => "GET",
Method::Post => "POST",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Options => "OPTIONS",
}
)
}
}