[go: up one dir, main page]

worker/
http.rs

1use std::fmt::Display;
2
3#[cfg(feature = "http")]
4pub mod body;
5#[cfg(feature = "http")]
6mod header;
7#[cfg(feature = "http")]
8mod redirect;
9#[cfg(feature = "http")]
10pub mod request;
11#[cfg(feature = "http")]
12pub mod response;
13
14/// A [`Method`](https://developer.mozilla.org/en-US/docs/Web/API/Request/method) representation
15/// used on Request objects.
16#[derive(Default, Debug, Clone, PartialEq, Hash, Eq)]
17pub enum Method {
18    Head = 0,
19    #[default]
20    Get,
21    Post,
22    Put,
23    Patch,
24    Delete,
25    Options,
26    Connect,
27    Trace,
28    Report,
29}
30
31impl Method {
32    pub fn all() -> Vec<Method> {
33        vec![
34            Method::Head,
35            Method::Get,
36            Method::Post,
37            Method::Put,
38            Method::Patch,
39            Method::Delete,
40            Method::Options,
41            Method::Connect,
42            Method::Trace,
43            Method::Report,
44        ]
45    }
46}
47
48impl From<String> for Method {
49    fn from(m: String) -> Self {
50        match m.to_ascii_uppercase().as_str() {
51            "HEAD" => Method::Head,
52            "POST" => Method::Post,
53            "PUT" => Method::Put,
54            "PATCH" => Method::Patch,
55            "DELETE" => Method::Delete,
56            "OPTIONS" => Method::Options,
57            "CONNECT" => Method::Connect,
58            "TRACE" => Method::Trace,
59            "REPORT" => Method::Report,
60            _ => Method::Get,
61        }
62    }
63}
64
65impl From<Method> for String {
66    fn from(val: Method) -> Self {
67        val.as_ref().to_string()
68    }
69}
70
71impl AsRef<str> for Method {
72    fn as_ref(&self) -> &'static str {
73        match self {
74            Method::Head => "HEAD",
75            Method::Post => "POST",
76            Method::Put => "PUT",
77            Method::Patch => "PATCH",
78            Method::Delete => "DELETE",
79            Method::Options => "OPTIONS",
80            Method::Connect => "CONNECT",
81            Method::Trace => "TRACE",
82            Method::Get => "GET",
83            Method::Report => "REPORT",
84        }
85    }
86}
87
88impl Display for Method {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
90        let s: String = (*self).clone().into();
91        write!(f, "{s}")
92    }
93}