miasht/
method.rs

1use std::fmt;
2
3/// HTTP Method.
4///
5/// See [IANA's HTTP Method Registry]
6/// (https://www.iana.org/assignments/http-methods/http-methods.xhtml)
7/// for more details about each method.
8///
9/// # Examples
10///
11/// ```
12/// use miasht::Method;
13///
14/// assert_eq!(Method::try_from_str("GET"), Some(Method::Get));
15/// assert_eq!(Method::try_from_str("get"), None); // case senstive
16///
17/// let method = Method::try_from_str("GET").unwrap();
18/// assert_eq!(method.as_str(), "GET");
19/// assert_eq!(method.to_string(), "GET");
20/// ```
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum Method {
23    Acl,
24    BaselineControl,
25    Bind,
26    Checkin,
27    Checkout,
28    Connect,
29    Copy,
30    Delete,
31    Get,
32    Head,
33    Label,
34    Link,
35    Lock,
36    Merge,
37    Mkactivity,
38    Mkcalendar,
39    Mkcol,
40    Mkredirectref,
41    Mkworkspace,
42    Move,
43    Options,
44    Orderpatch,
45    Patch,
46    Post,
47    Pri,
48    Propfind,
49    Proppatch,
50    Put,
51    Rebind,
52    Report,
53    Search,
54    Trace,
55    Unbind,
56    Uncheckout,
57    Unlink,
58    Unlock,
59    Update,
60    Updateredirectref,
61    VersionControl,
62}
63impl Method {
64    pub fn try_from_str(method: &str) -> Option<Self> {
65        Some(match method {
66            "ACL" => Method::Acl,
67            "BASELINE-CONTROL" => Method::BaselineControl,
68            "BIND" => Method::Bind,
69            "CHECKIN" => Method::Checkin,
70            "CHECKOUT" => Method::Checkout,
71            "CONNECT" => Method::Connect,
72            "COPY" => Method::Copy,
73            "DELETE" => Method::Delete,
74            "GET" => Method::Get,
75            "HEAD" => Method::Head,
76            "LABEL" => Method::Label,
77            "LINK" => Method::Link,
78            "LOCK" => Method::Lock,
79            "MERGE" => Method::Merge,
80            "MKACTIVITY" => Method::Mkactivity,
81            "MKCALENDAR" => Method::Mkcalendar,
82            "MKCOL" => Method::Mkcol,
83            "MKREDIRECTREF" => Method::Mkredirectref,
84            "MKWORKSPACE" => Method::Mkworkspace,
85            "MOVE" => Method::Move,
86            "OPTIONS" => Method::Options,
87            "ORDERPATCH" => Method::Orderpatch,
88            "PATCH" => Method::Patch,
89            "POST" => Method::Post,
90            "PRI" => Method::Pri,
91            "PROPFIND" => Method::Propfind,
92            "PROPPATCH" => Method::Proppatch,
93            "PUT" => Method::Put,
94            "REBIND" => Method::Rebind,
95            "REPORT" => Method::Report,
96            "SEARCH" => Method::Search,
97            "TRACE" => Method::Trace,
98            "UNBIND" => Method::Unbind,
99            "UNCHECKOUT" => Method::Uncheckout,
100            "UNLINK" => Method::Unlink,
101            "UNLOCK" => Method::Unlock,
102            "UPDATE" => Method::Update,
103            "UPDATEREDIRECTREF" => Method::Updateredirectref,
104            "VERSION-CONTROL" => Method::VersionControl,
105            _ => return None,
106        })
107    }
108    pub fn as_str(&self) -> &str {
109        match *self {
110            Method::Acl => "ACL",
111            Method::BaselineControl => "BASELINE-CONTROL",
112            Method::Bind => "BIND",
113            Method::Checkin => "CHECKIN",
114            Method::Checkout => "CHECKOUT",
115            Method::Connect => "CONNECT",
116            Method::Copy => "COPY",
117            Method::Delete => "DELETE",
118            Method::Get => "GET",
119            Method::Head => "HEAD",
120            Method::Label => "LABEL",
121            Method::Link => "LINK",
122            Method::Lock => "LOCK",
123            Method::Merge => "MERGE",
124            Method::Mkactivity => "MKACTIVITY",
125            Method::Mkcalendar => "MKCALENDAR",
126            Method::Mkcol => "MKCOL",
127            Method::Mkredirectref => "MKREDIRECTREF",
128            Method::Mkworkspace => "MKWORKSPACE",
129            Method::Move => "MOVE",
130            Method::Options => "OPTIONS",
131            Method::Orderpatch => "ORDERPATCH",
132            Method::Patch => "PATCH",
133            Method::Post => "POST",
134            Method::Pri => "PRI",
135            Method::Propfind => "PROPFIND",
136            Method::Proppatch => "PROPPATCH",
137            Method::Put => "PUT",
138            Method::Rebind => "REBIND",
139            Method::Report => "REPORT",
140            Method::Search => "SEARCH",
141            Method::Trace => "TRACE",
142            Method::Unbind => "UNBIND",
143            Method::Uncheckout => "UNCHECKOUT",
144            Method::Unlink => "UNLINK",
145            Method::Unlock => "UNLOCK",
146            Method::Update => "UPDATE",
147            Method::Updateredirectref => "UPDATEREDIRECTREF",
148            Method::VersionControl => "VERSION-CONTROL",
149        }
150    }
151}
152impl fmt::Display for Method {
153    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154        write!(f, "{}", self.as_str())
155    }
156}