1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::fmt::Display;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Method {
    GET,
    POST,
    PUT,
    DELETE,
    HEAD,
    OPTIONS,
    CONNECT,
    PATCH,
    TRACE,
    UNKNOWN,
}

impl Display for Method {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let method = match self {
            Method::GET => "GET",
            Method::POST => "POST",
            Method::PUT => "PUT",
            Method::DELETE => "DELETE",
            Method::HEAD => "HEAD",
            Method::OPTIONS => "OPTIONS",
            Method::CONNECT => "CONNECT",
            Method::PATCH => "PATCH",
            Method::TRACE => "TRACE",
            Method::UNKNOWN => "UNKNOWN",
        };

        write!(f, "{}", method)
    }
}

impl From<String> for Method {
    fn from(method: String) -> Self {
        match method.as_str() {
            "GET" => Method::GET,
            "POST" => Method::POST,
            "PUT" => Method::PUT,
            "DELETE" => Method::DELETE,
            "HEAD" => Method::HEAD,
            "OPTIONS" => Method::OPTIONS,
            "CONNECT" => Method::CONNECT,
            "PATCH" => Method::PATCH,
            "TRACE" => Method::TRACE,
            _ => Method::UNKNOWN,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HttpVersion {
    V1_0,
    V1_1,
    V2_0,
    V3_0,
    UNKNOWN,
}

impl Display for HttpVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let version = match self {
            HttpVersion::V1_0 => "1.0",
            HttpVersion::V1_1 => "1.1",
            HttpVersion::V2_0 => "2.0",
            HttpVersion::V3_0 => "3.0",
            HttpVersion::UNKNOWN => "1.1",
        };

        write!(f, "HTTP/{}", version)
    }
}

impl From<String> for HttpVersion {
    fn from(version: String) -> Self {
        match version.to_uppercase().as_str() {
            "HTTP/1.0" => HttpVersion::V1_0,
            "HTTP/1.1" => HttpVersion::V1_1,
            "HTTP/2.0" => HttpVersion::V2_0,
            "HTTP/3.0" => HttpVersion::V3_0,
            _ => HttpVersion::UNKNOWN,
        }
    }
}