rama_http/protocols/html/response.rs
1use super::core::IntoHtml;
2use crate::Response;
3use crate::headers::ContentType;
4use crate::service::web::response::{Headers, IntoResponse};
5
6/// The output of an element macro (`html!`, `div!`, ..., `custom!`).
7///
8/// `HtmlBuf<T>` is a thin newtype around the macro-generated tuple `T`
9/// that implements both [`IntoHtml`] (so it can be nested in larger
10/// templates) and
11/// [`IntoResponse`](crate::service::web::response::IntoResponse) (so a
12/// rendered page can be returned directly from a handler with
13/// `Content-Type: text/html; charset=utf-8`).
14///
15/// Users normally do not name this type — they receive it from the
16/// macros and either compose it further or return it. It is, however,
17/// public so that handler signatures can mention it explicitly when
18/// desired.
19#[derive(Debug, Clone, Copy)]
20pub struct HtmlBuf<T>(pub T);
21
22impl<T: IntoHtml> IntoHtml for HtmlBuf<T> {
23 #[inline]
24 fn into_html(self) -> impl IntoHtml {
25 self.0
26 }
27
28 #[inline]
29 fn escape_and_write(self, buf: &mut String)
30 where
31 Self: Sized,
32 {
33 self.0.escape_and_write(buf);
34 }
35
36 #[inline]
37 fn size_hint(&self) -> usize {
38 self.0.size_hint()
39 }
40}
41
42impl<T: IntoHtml> IntoResponse for HtmlBuf<T> {
43 fn into_response(self) -> Response {
44 let body = self.0.into_string();
45 (Headers::single(ContentType::html_utf8()), body).into_response()
46 }
47}