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
use crate::core::res_body::{full, ResBody};
use bytes::Bytes;
use hyper::Response as HyperResponse;
use std::ops::{Deref, DerefMut};

#[derive(Debug)]
pub struct Response {
    pub res: HyperResponse<ResBody>,
}

impl Response {
    pub fn empty() -> Self {
        Response::from(Bytes::new())
    }
    #[allow(dead_code)]
    pub fn set_status(&mut self, status: hyper::StatusCode) {
        *self.res.status_mut() = status;
    }
    #[allow(dead_code)]
    pub fn set_body(mut self, body: ResBody) -> Self {
        *self.res.body_mut() = body;
        self
    }
    #[allow(dead_code)]
    pub fn set_header(
        &mut self,
        key: hyper::header::HeaderName,
        value: hyper::header::HeaderValue,
    ) -> &mut Self {
        self.headers_mut().insert(key, value);
        self
    }
}

impl<T: Into<Bytes>> From<T> for Response {
    fn from(chunk: T) -> Self {
        Self {
            res: HyperResponse::new(full(chunk)),
        }
    }
}

impl Deref for Response {
    type Target = HyperResponse<ResBody>;

    fn deref(&self) -> &Self::Target {
        &self.res
    }
}

impl DerefMut for Response {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.res
    }
}