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
use super::Html;
use crate::{http::StatusCode, IntoResponse, Response};

/// Template response using [`askama`](https://crates.io/crates/askama).
pub struct Template<T>(pub T);

impl<T: askama::Template + Send> IntoResponse for Template<T> {
    fn into_response(self) -> Response {
        match self.0.render() {
            Ok(s) => s.into_response(),
            Err(err) => Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(err.to_string()),
        }
    }
}

/// Template response with content-type "text/html" using [`askama`](https://crates.io/crates/askama).
pub struct HtmlTemplate<T>(pub T);

impl<T: askama::Template + Send> IntoResponse for HtmlTemplate<T> {
    fn into_response(self) -> Response {
        match self.0.render() {
            Ok(s) => Html(s).into_response(),
            Err(err) => Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(err.to_string()),
        }
    }
}