myc_core/domain/dtos/
http.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3use std::{
4    fmt::{Display, Formatter, Result as FmtResult},
5    str::FromStr,
6};
7use utoipa::{ToResponse, ToSchema};
8
9#[derive(
10    Debug,
11    Clone,
12    Copy,
13    Deserialize,
14    Serialize,
15    PartialEq,
16    Eq,
17    ToSchema,
18    ToResponse,
19)]
20#[serde(rename_all = "UPPERCASE")]
21pub enum HttpMethod {
22    Get,
23    Head,
24    Patch,
25    Post,
26    Put,
27    Delete,
28    Connect,
29    Options,
30    Trace,
31    All,
32    None,
33}
34
35impl HttpMethod {
36    /// Convert a reqwest::Method to a HttpMethod.
37    pub fn from_reqwest_method(method: Method) -> HttpMethod {
38        match method {
39            Method::GET => HttpMethod::Get,
40            Method::HEAD => HttpMethod::Head,
41            Method::PATCH => HttpMethod::Patch,
42            Method::POST => HttpMethod::Post,
43            Method::PUT => HttpMethod::Put,
44            Method::DELETE => HttpMethod::Delete,
45            Method::CONNECT => HttpMethod::Connect,
46            Method::OPTIONS => HttpMethod::Options,
47            Method::TRACE => HttpMethod::Trace,
48            _ => HttpMethod::None,
49        }
50    }
51}
52
53impl Display for HttpMethod {
54    fn fmt(&self, f: &mut Formatter) -> FmtResult {
55        match self {
56            HttpMethod::Get => write!(f, "GET"),
57            HttpMethod::Head => write!(f, "HEAD"),
58            HttpMethod::Patch => write!(f, "PATCH"),
59            HttpMethod::Post => write!(f, "POST"),
60            HttpMethod::Put => write!(f, "PUT"),
61            HttpMethod::Delete => write!(f, "DELETE"),
62            HttpMethod::Connect => write!(f, "CONNECT"),
63            HttpMethod::Options => write!(f, "OPTIONS"),
64            HttpMethod::Trace => write!(f, "TRACE"),
65            HttpMethod::All => write!(f, "ALL"),
66            HttpMethod::None => write!(f, "NONE"),
67        }
68    }
69}
70
71impl FromStr for HttpMethod {
72    type Err = HttpMethod;
73
74    fn from_str(s: &str) -> Result<HttpMethod, HttpMethod> {
75        match s {
76            "GET" => Ok(HttpMethod::Get),
77            "HEAD" => Ok(HttpMethod::Head),
78            "PATCH" => Ok(HttpMethod::Patch),
79            "POST" => Ok(HttpMethod::Post),
80            "PUT" => Ok(HttpMethod::Put),
81            "DELETE" => Ok(HttpMethod::Delete),
82            "CONNECT" => Ok(HttpMethod::Connect),
83            "OPTIONS" => Ok(HttpMethod::Options),
84            "TRACE" => Ok(HttpMethod::Trace),
85            "ALL" => Ok(HttpMethod::All),
86            "NONE" => Ok(HttpMethod::None),
87            _ => Err(HttpMethod::None),
88        }
89    }
90}
91
92#[derive(
93    Debug,
94    Clone,
95    Copy,
96    Deserialize,
97    Serialize,
98    ToSchema,
99    ToResponse,
100    PartialEq,
101    Eq,
102)]
103#[serde(rename_all = "camelCase")]
104pub enum Protocol {
105    Http,
106    Https,
107    Grpc,
108}
109
110impl Display for Protocol {
111    fn fmt(&self, f: &mut Formatter) -> FmtResult {
112        match self {
113            Protocol::Http => write!(f, "http"),
114            Protocol::Https => write!(f, "https"),
115            Protocol::Grpc => write!(f, "grpc"),
116        }
117    }
118}