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

pub use askama::*;
use bytes::BytesMut;

use actix_web::{error::ErrorInternalServerError, HttpResponse};

pub trait TemplateToResponse {
    fn to_response(&self) -> HttpResponse;
}

impl<T: askama::Template> TemplateToResponse for T {
    fn to_response(&self) -> HttpResponse {
        let mut buffer = BytesMut::with_capacity(self.size_hint());
        if self.render_into(&mut buffer).is_err() {
            return ErrorInternalServerError("Template parsing error").error_response();
        }

        let ctype =
            askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string();
        HttpResponse::Ok()
            .content_type(ctype.as_str())
            .body(buffer.freeze())
    }
}

// Re-exported for use by generated code
#[doc(hidden)]
pub mod futures {
    pub use futures_util::future::ready;
    pub use futures_util::future::Ready;
}