From b56c11639f9ea5ef1354a1e91ca98541a16bca9b Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 28 Jan 2020 21:26:13 +0100 Subject: Move Actix-Web integration into separate askama_actix crate --- askama_actix/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 askama_actix/src/lib.rs (limited to 'askama_actix/src/lib.rs') diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs new file mode 100644 index 0000000..7372034 --- /dev/null +++ b/askama_actix/src/lib.rs @@ -0,0 +1,22 @@ +pub use askama::*; +use bytes::BytesMut; + +use actix_web::{error::ErrorInternalServerError, Error, HttpResponse}; + +pub trait TemplateIntoResponse { + fn into_response(&self) -> ::std::result::Result; +} + +impl TemplateIntoResponse for T { + fn into_response(&self) -> ::std::result::Result { + let mut buffer = BytesMut::with_capacity(self.size_hint()); + self.render_into(&mut buffer) + .map_err(|_| ErrorInternalServerError("Template parsing error"))?; + + let ctype = + askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string(); + Ok(HttpResponse::Ok() + .content_type(ctype.as_str()) + .body(buffer.freeze())) + } +} -- cgit