1use std::{
2 fmt::{Display, Error, Formatter},
3 str::FromStr,
4};
5
6use hyper::Method;
7
8#[derive(Eq, PartialEq, Hash, Clone, Debug)]
9pub enum HttpMethod {
10 Get,
11 Post,
12 Put,
13 Delete,
14 Head,
15 Options,
16 Connect,
17 Patch,
18 Trace,
19}
20
21impl Display for HttpMethod {
22 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
23 write!(
24 f,
25 "{}",
26 match self {
27 HttpMethod::Get => "GET",
28 HttpMethod::Head => "HEAD",
29 HttpMethod::Options => "OPTIONS",
30 HttpMethod::Connect => "CONNECT",
31 HttpMethod::Delete => "DELETE",
32 HttpMethod::Patch => "PATCH",
33 HttpMethod::Post => "POST",
34 HttpMethod::Put => "PUT",
35 HttpMethod::Trace => "TRACE",
36 }
37 )
38 }
39}
40
41impl FromStr for HttpMethod {
42 type Err = String;
43
44 fn from_str(method: &str) -> Result<Self, String> {
45 match method.to_lowercase().as_ref() {
46 "get" => Ok(HttpMethod::Get),
47 "post" => Ok(HttpMethod::Post),
48 "put" => Ok(HttpMethod::Put),
49 "delete" => Ok(HttpMethod::Delete),
50 "head" => Ok(HttpMethod::Head),
51 "options" => Ok(HttpMethod::Options),
52 "connect" => Ok(HttpMethod::Connect),
53 "patch" => Ok(HttpMethod::Patch),
54 "trace" => Ok(HttpMethod::Trace),
55 _ => Err(format!(
56 "Could not parse a suitable HTTP Method for string: '{}'",
57 method
58 )),
59 }
60 }
61}
62
63impl From<Method> for HttpMethod {
64 fn from(method: Method) -> Self {
65 match method {
66 Method::GET => HttpMethod::Get,
67 Method::POST => HttpMethod::Post,
68 Method::PUT => HttpMethod::Put,
69 Method::DELETE => HttpMethod::Delete,
70 Method::HEAD => HttpMethod::Head,
71 Method::OPTIONS => HttpMethod::Options,
72 Method::CONNECT => HttpMethod::Connect,
73 Method::PATCH => HttpMethod::Patch,
74 Method::TRACE => HttpMethod::Trace,
75 method => panic!(
76 "Could not recognise suitable HTTP method for {}",
77 method
78 ),
79 }
80 }
81}