aboutsummaryrefslogtreecommitdiff
path: root/src/http_types.rs
blob: a652e6cb7ad8d8b421d6e9a707e1ee04f713626e (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
use itertools::Itertools;
use std::collections::HashMap;

pub enum StatusCode {
    // 2xx Success
    Ok,

    // 4xx Client Error
    BadRequest,
    NotFound,
}

type Endpoint = String;
type Target = String;

#[derive(Debug)]
pub enum HTTPMethod {
    Get((Endpoint, Target)),
    Post((Endpoint, Target)),
    Put((Endpoint, Target)),
}

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

impl From<HTTPMethod> for String {
    fn from(val: HTTPMethod) -> Self {
        use HTTPMethod::*;
        match val {
            Get((endpoint, target)) => "GET".to_string() + &endpoint + &target,
            Post((endpoint, target)) => "POST".to_string() + &endpoint + &target,
            Put((endpoint, target)) => "PUT".to_string() + &endpoint + &target,
        }
    }
}

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

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<&str> for HTTPMethod {
    fn from(val: &str) -> Self {
        use HTTPMethod::*;
        let request_line = val.split(' ').collect_vec();
        let (method, info) = (request_line[0], request_line[1]);
        let info = info.chars().skip(1).collect::<String>() + &"/";
        let (endpoint, target) = info.split_once("/").expect("Should be splitable by /");
        match method {
            "GET" => Get((endpoint.to_string(), target.to_string())),
            "POST" => Post((endpoint.to_string(), target.to_string())),
            _ => {
                eprintln!("{method} Not Supported Yet");
                unreachable!()
            }
        }
    }
}