aboutsummaryrefslogtreecommitdiffstats
path: root/askama_axum/src/lib.rs
blob: 0c21464ab0dfa273ff47011c33975cbb27a17f33 (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
#![deny(elided_lifetimes_in_paths)]

pub use askama::*;
use axum::{
    self,
    body::{Bytes, Full},
    http::{Response, StatusCode},
};

pub fn into_response<T: Template>(t: &T, ext: &str) -> axum::http::Response<Full<Bytes>> {
    match t.render() {
        Ok(body) => Response::builder()
            .status(StatusCode::OK)
            .header(
                "content-type",
                askama::mime::extension_to_mime_type(ext).to_string(),
            )
            .body(body.into())
            .unwrap(),
        Err(_) => Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body(vec![].into())
            .unwrap(),
    }
}