aboutsummaryrefslogtreecommitdiffstats
path: root/askama_actix/src/lib.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-01-28 21:26:13 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-01-29 09:25:59 +0100
commitb56c11639f9ea5ef1354a1e91ca98541a16bca9b (patch)
tree11933ea82d131d0b1efe64aed64ce1705f6f7d0a /askama_actix/src/lib.rs
parent9026f616e620426535bf095848fe355e25911d62 (diff)
downloadaskama-b56c11639f9ea5ef1354a1e91ca98541a16bca9b.tar.gz
askama-b56c11639f9ea5ef1354a1e91ca98541a16bca9b.tar.bz2
askama-b56c11639f9ea5ef1354a1e91ca98541a16bca9b.zip
Move Actix-Web integration into separate askama_actix crate
Diffstat (limited to 'askama_actix/src/lib.rs')
-rw-r--r--askama_actix/src/lib.rs22
1 files changed, 22 insertions, 0 deletions
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<HttpResponse, Error>;
+}
+
+impl<T: askama::Template> TemplateIntoResponse for T {
+ fn into_response(&self) -> ::std::result::Result<HttpResponse, Error> {
+ 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()))
+ }
+}