aboutsummaryrefslogtreecommitdiffstats
path: root/askama_mendes/src/lib.rs
blob: d1ccb2c5a1f45e120f5485611801ca4b0763fdcc (plain) (blame)
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
#![forbid(unsafe_code)]
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]

use mendes::application::{Application, Responder};
use mendes::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
use mendes::http::request::Parts;
use mendes::http::Response;

pub use askama::*;

pub fn into_response<A, T>(
    app: &A,
    req: &Parts,
    t: &T,
    _ext: Option<&str>,
) -> Response<A::ResponseBody>
where
    A: Application,
    T: Template,
    A::ResponseBody: From<String>,
    A::Error: From<askama::Error>,
{
    let content = match t.render() {
        Ok(content) => content,
        Err(e) => return <A::Error as From<_>>::from(e).into_response(app, req),
    };

    Response::builder()
        .header(CONTENT_LENGTH, content.len())
        .header(CONTENT_TYPE, HeaderValue::from_static(T::MIME_TYPE))
        .body(content.into())
        .unwrap()
}