rust_integration_services/http/
http_method.rs1use crate::{common::result::ResultDyn, utils::error::Error};
2
3
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum HttpMethod {
7 Get,
8 Post,
9 Put,
10 Delete,
11 Patch,
12 Head,
13 Options,
14 Connect,
15 Trace,
16}
17
18impl HttpMethod {
19 pub fn as_str(self) -> &'static str {
20 match self {
21 HttpMethod::Get => "GET",
22 HttpMethod::Post => "POST",
23 HttpMethod::Put => "PUT",
24 HttpMethod::Delete => "DELETE",
25 HttpMethod::Patch => "PATCH",
26 HttpMethod::Head => "HEAD",
27 HttpMethod::Options => "OPTIONS",
28 HttpMethod::Connect => "CONNECT",
29 HttpMethod::Trace => "TRACE",
30 }
31 }
32
33 pub fn from_str<T: AsRef<str>>(method: T) -> ResultDyn<HttpMethod> {
34 match method.as_ref().to_ascii_uppercase().as_str() {
35 "GET" => Ok(HttpMethod::Get),
36 "POST" => Ok(HttpMethod::Post),
37 "PUT" => Ok(HttpMethod::Put),
38 "DELETE" => Ok(HttpMethod::Delete),
39 "PATCH" => Ok(HttpMethod::Patch),
40 "HEAD" => Ok(HttpMethod::Head),
41 "OPTIONS" => Ok(HttpMethod::Options),
42 "CONNECT" => Ok(HttpMethod::Connect),
43 "TRACE" => Ok(HttpMethod::Trace),
44 _ => Err(Box::new(Error::std_io("Invalid HTTP method"))),
45 }
46 }
47}