From c860bee5d5934fba6d57cbf815894a5cc8599ad9 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Sat, 21 Jul 2018 18:27:47 +0000 Subject: Add optional implementation for actix_web's Responder trait --- askama_derive/src/generator.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'askama_derive/src/generator.rs') 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(self, _req: &::askama::actix_web::HttpRequest) \ + -> Result {", + ); + + 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>) { -- cgit