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

pub use askama::*;
#[doc(hidden)]
pub use hyper;
use hyper::{header, Body, Response, StatusCode};

pub fn try_respond<T: Template>(t: &T) -> Result<Response<Body>> {
    Response::builder()
        .status(StatusCode::OK)
        .header(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static(T::MIME_TYPE),
        )
        .body(t.render()?.into())
        .map_err(|err| Error::Custom(Box::new(err)))
}

pub fn respond<T: Template>(t: &T) -> Response<Body> {
    match try_respond(t) {
        Ok(response) => response,
        Err(_) => Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body(Body::empty())
            .unwrap(),
    }
}