diff options
author | Ryan McGrath <ryan@rymc.io> | 2018-07-21 18:27:47 +0000 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-22 14:47:29 +0100 |
commit | c860bee5d5934fba6d57cbf815894a5cc8599ad9 (patch) | |
tree | 4dfc5efe28480e61a245db261daf0835e028602f /askama_derive | |
parent | b231accdedf1c867f22c74d7588055c4e3576789 (diff) | |
download | askama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.tar.gz askama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.tar.bz2 askama-c860bee5d5934fba6d57cbf815894a5cc8599ad9.zip |
Add optional implementation for actix_web's Responder trait
Diffstat (limited to 'askama_derive')
-rw-r--r-- | askama_derive/Cargo.toml | 1 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 23 |
2 files changed, 24 insertions, 0 deletions
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 1834852..774fb51 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -15,6 +15,7 @@ proc-macro = true default = [] iron = ["askama_shared/iron"] rocket = ["askama_shared/rocket"] +actix-web = ["askama_shared/actix-web"] [dependencies] askama_shared = { version = "0.7.0", path = "../askama_shared" } 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>>) { |