aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src
diff options
context:
space:
mode:
authorLibravatar Ryan McGrath <ryan@rymc.io>2018-07-21 18:27:47 +0000
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-07-22 14:47:29 +0100
commitc860bee5d5934fba6d57cbf815894a5cc8599ad9 (patch)
tree4dfc5efe28480e61a245db261daf0835e028602f /askama_derive/src
parentb231accdedf1c867f22c74d7588055c4e3576789 (diff)
downloadaskama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.tar.gz
askama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.tar.bz2
askama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.zip
Add optional implementation for actix_web's Responder trait
Diffstat (limited to '')
-rw-r--r--askama_derive/src/generator.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index f586e1c..5af38c0 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -94,6 +94,9 @@ impl<'a> Generator<'a> {
if cfg!(feature = "rocket") {
self.impl_rocket_responder();
}
+ if cfg!(feature = "actix-web") {
+ self.impl_actix_web_responder();
+ }
self.buf
}
@@ -235,6 +238,26 @@ impl<'a> Generator<'a> {
self.writeln("}");
}
+ // Implement Actix-web's `Responder`.
+ fn impl_actix_web_responder(&mut self) {
+ self.write_header("::askama::actix_web::Responder", None);
+ self.writeln("type Item = ::askama::actix_web::HttpResponse;");
+ self.writeln("type Error = ::askama::actix_web::Error;");
+ self.writeln(
+ "fn respond_to<S>(self, _req: &::askama::actix_web::HttpRequest<S>) \
+ -> Result<Self::Item, Self::Error> {",
+ );
+
+ let ext = match self.input.path.extension() {
+ Some(s) => s.to_str().unwrap(),
+ None => "txt",
+ };
+ self.writeln(&format!("::askama::actix_web::respond(&self, {:?})", ext));
+
+ self.writeln("}");
+ self.writeln("}");
+ }
+
// Writes header for the `impl` for `TraitFromPathName` or `Template`
// for the given context struct.
fn write_header(&mut self, target: &str, params: Option<Vec<syn::GenericParam>>) {