aboutsummaryrefslogtreecommitdiff
path: root/src/http_types.rs
blob: 44a9039b367ce74d41cf35faff4e56d7665ee3e2 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use itertools::Itertools;
use std::collections::HashMap;

pub enum StatusCode {
    // 2xx Success
    Ok,
    Created,

    // 4xx Client Error
    BadRequest,
    NotFound,
}

type Route = String;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Method {
    GET,
    POST,
    PUT,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Endpoint {
    pub method: Method,
    pub route: Route,
}

impl Endpoint {
    fn get(route: &str) -> Self {
        Self {
            method: Method::GET,
            route: route.to_string(),
        }
    }
    fn post(route: &str) -> Self {
        Self {
            method: Method::POST,
            route: route.to_string(),
        }
    }
    fn put(route: &str) -> Self {
        Self {
            method: Method::PUT,
            route: route.to_string(),
        }
    }
}

pub fn get(route: &str) -> Endpoint {
    Endpoint::get(route)
}

pub fn post(route: &str) -> Endpoint {
    Endpoint::post(route)
}

pub fn put(route: &str) -> Endpoint {
    Endpoint::put(route)
}

impl From<StatusCode> for String {
    fn from(val: StatusCode) -> Self {
        use StatusCode::*;
        match val {
            Ok => "200 OK".to_string(),
            Created => "201 Created".to_string(),
            BadRequest => "400 Bad Request".to_string(),
            NotFound => "404 Not Found".to_string(),
        }
    }
}

impl From<Endpoint> for String {
    fn from(val: Endpoint) -> Self {
        use Method as M;
        match (val.method, val.route) {
            (M::GET, route) => "GET ".to_string() + &route,
            (M::POST, route) => "GET ".to_string() + &route,
            (M::PUT, route) => "GET ".to_string() + &route,
        }
    }
}
impl From<&Endpoint> for String {
    fn from(val: &Endpoint) -> Self {
        use Method as M;
        match (&val.method, &val.route) {
            (M::GET, route) => "GET ".to_string() + &route,
            (M::POST, route) => "GET ".to_string() + &route,
            (M::PUT, route) => "GET ".to_string() + &route,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Headers(pub HashMap<String, String>);

impl From<Vec<String>> for Headers {
    fn from(value: Vec<String>) -> Self {
        let mut header_map = HashMap::new();
        for header in value.iter().filter(|val| !val.is_empty()) {
            let (key, val) = header
                .split_once(": ")
                .expect("Should be splitable by :<space>");
            header_map.insert(key.to_string(), val.to_string());
        }
        Headers(header_map)
    }
}

impl From<&[&str]> for Headers {
    fn from(value: &[&str]) -> Self {
        let mut header_map = HashMap::new();
        for header in value.iter().filter(|val| !val.is_empty()) {
            let (key, val) = header
                .split_once(": ")
                .expect("Should be splitable by :<space>");
            header_map.insert(key.to_string(), val.to_string());
        }
        Headers(header_map)
    }
}

impl From<String> for Endpoint {
    fn from(val: String) -> Self {
        let request_line = val.split(' ').collect_vec();
        let (method, route) = (request_line[0], request_line[1]);
        match method {
            "GET" => Endpoint::get(route),
            "POST" => Endpoint::post(route),
            _ => {
                eprintln!("{method} Not Supported Yet");
                unreachable!()
            }
        }
    }
}
impl From<&str> for Endpoint {
    fn from(val: &str) -> Self {
        let request_line = val.split(' ').collect_vec();
        let (method, route) = (request_line[0], request_line[1]);
        match method {
            "GET" => Endpoint::get(route),
            "POST" => Endpoint::post(route),
            _ => {
                eprintln!("{method} Not Supported Yet");
                unreachable!()
            }
        }
    }
}