summaryrefslogtreecommitdiff
path: root/src/response/builder.rs
blob: b99b8240c6027a4f71c82c5f93d6782298a8a48c (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
use std::collections::HashMap;

use crate::response::{self, Response};

#[derive(Debug, Clone, Copy)]
pub struct Builder;

#[derive(Debug, Clone, Copy)]
pub struct Status {
    status: response::Status,
}

#[derive(Debug, Clone)]
pub struct Header {
    status: response::Status,
    headers: HashMap<String, Vec<u8>>,
}

impl Builder {
    pub fn new() -> Self {
        Self
    }

    pub fn status(self, status: response::Status) -> Status {
        Status { status }
    }
}

impl Status {
    pub fn headers<K: Into<String>, V: Into<Vec<u8>>, T: IntoIterator<Item = (K, V)>>(
        self,
        headers: T,
    ) -> Header {
        Header {
            status: self.status,
            headers: HashMap::from_iter(
                headers
                    .into_iter()
                    .map(|(key, value)| (key.into(), value.into())),
            ),
        }
    }

    pub fn body(self, body: response::Body) -> Response {
        Response {
            status: self.status,
            headers: HashMap::new(),
            body,
        }
    }
}

impl Header {
    pub fn headers<K: Into<String>, V: Into<Vec<u8>>, T: IntoIterator<Item = (K, V)>>(
        self,
        headers: T,
    ) -> Header {
        Header {
            status: self.status,
            headers: HashMap::from_iter(
                headers
                    .into_iter()
                    .map(|(key, value)| (key.into(), value.into())),
            ),
        }
    }

    pub fn body(self, body: response::Body) -> Response {
        Response {
            status: self.status,
            headers: self.headers,
            body,
        }
    }
}