rust_integration_services/http/
http_method.rs

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