Skip to main content

gen_html/
web.rs

1#[cfg(feature = "axum")]
2#[cfg_attr(docsrs, doc(cfg(feature = "axum")))]
3mod axum {
4    use crate::{Escaped, Raw, Render, RenderFn};
5    use axum::response::{Html, IntoResponse, Response};
6    use std::fmt;
7
8    impl<T: fmt::Display> IntoResponse for Escaped<T> {
9        fn into_response(self) -> Response {
10            self.render().into_response()
11        }
12    }
13
14    impl<T: fmt::Display> IntoResponse for Raw<T> {
15        fn into_response(self) -> Response {
16            Html(self.0.to_string()).into_response()
17        }
18    }
19
20    impl<F> IntoResponse for RenderFn<F>
21    where
22        F: Fn(&mut fmt::Formatter) -> fmt::Result,
23    {
24        fn into_response(self) -> Response {
25            self.render().into_response()
26        }
27    }
28}
29
30#[cfg(feature = "actix-web")]
31#[cfg_attr(docsrs, doc(cfg(feature = "actix-web")))]
32mod actix_web {
33    use crate::{Escaped, Raw, Render, RenderFn};
34    use actix_web::{HttpRequest, HttpResponse, Responder, web::Html};
35    use std::fmt;
36
37    impl<T: fmt::Display> Responder for Escaped<T> {
38        type Body = String;
39
40        fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
41            self.render().respond_to(req)
42        }
43    }
44
45    impl<T: fmt::Display> Responder for Raw<T> {
46        type Body = String;
47
48        fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
49            Html::new(self.0.to_string()).respond_to(req)
50        }
51    }
52
53    impl<F> Responder for RenderFn<F>
54    where
55        F: Fn(&mut fmt::Formatter) -> fmt::Result,
56    {
57        type Body = String;
58
59        fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
60            self.render().respond_to(req)
61        }
62    }
63}