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
//! response generator for Html

use core::{convert::Infallible, fmt};

use xitca_http::body::ResponseBody;

use crate::{
    context::WebContext,
    handler::Responder,
    http::{const_header_value::TEXT_HTML_UTF8, header::CONTENT_TYPE, WebResponse},
};

pub struct Html<T>(pub T);

impl<T> fmt::Debug for Html<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Html").field("value", &self.0).finish()
    }
}

impl<'r, C, T> Responder<WebContext<'r, C>> for Html<T>
where
    T: Into<ResponseBody>,
{
    type Response = WebResponse;
    type Error = Infallible;

    #[inline]
    async fn respond(self, ctx: WebContext<'r, C>) -> Result<Self::Response, Self::Error> {
        let mut res = ctx.into_response(self.0);
        res.headers_mut().insert(CONTENT_TYPE, TEXT_HTML_UTF8);
        Ok(res)
    }

    fn map(self, mut res: Self::Response) -> Result<Self::Response, Self::Error> {
        res.headers_mut().insert(CONTENT_TYPE, TEXT_HTML_UTF8);
        Ok(res.map(|_| self.0.into()))
    }
}