From bf51e7264fb4273672430f74ce8c2b16a74d1c38 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 23 Aug 2017 16:32:07 +0200 Subject: Add optional support for Rocket --- askama/Cargo.toml | 2 ++ askama/src/lib.rs | 8 ++++++++ askama_derive/Cargo.toml | 1 + askama_derive/src/generator.rs | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/askama/Cargo.toml b/askama/Cargo.toml index f96b939..62d4fec 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -18,6 +18,7 @@ travis-ci = { repository = "djc/askama" } [dependencies] askama_derive = { path = "../askama_derive", version = "0.3.4" } error-chain = "0.10" +rocket = { version = "0.3", optional = true } serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } iron = { version = "0.5", optional = true } @@ -26,6 +27,7 @@ iron = { version = "0.5", optional = true } serde-json = ["serde", "serde_json"] default = [] with-iron = ["iron", "askama_derive/iron"] +with-rocket = ["rocket", "askama_derive/rocket"] [package.metadata.docs.rs] features = [ "serde-json" ] diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 4d9b2fb..70202f5 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -250,6 +250,14 @@ pub mod iron { pub use self::iron::response::Response; } +#[cfg(feature = "with-rocket")] +pub mod rocket { + extern crate rocket; + pub use self::rocket::http::{ContentType, Status}; + pub use self::rocket::request::Request; + pub use self::rocket::response::{Responder, Response}; +} + // Duplicates askama_derive::path::template_dir() fn template_dir() -> PathBuf { let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 40f1ad5..8016b37 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -14,6 +14,7 @@ proc-macro = true [features] default = [] iron = [] +rocket = [] [dependencies] nom = "3" diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 3595a84..1d8e8f2 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -56,6 +56,9 @@ pub fn generate(ast: &syn::DeriveInput, path: &Path, mut nodes: Vec) -> St if cfg!(feature = "iron") { gen.impl_modifier_response(ast); } + if cfg!(feature = "rocket") { + gen.impl_responder(ast, path); + } gen.result() } @@ -645,6 +648,27 @@ impl<'a> Generator<'a> { self.writeln("}"); } + // Implement Rocket's `Responder`. + fn impl_responder(&mut self, ast: &syn::DeriveInput, path: &Path) { + self.write_header(ast, "::askama::rocket::Responder<'r>", &vec!["'r"]); + self.writeln("fn respond_to(self, _: &::askama::rocket::Request) \ + -> ::std::result::Result<\ + ::askama::rocket::Response<'r>, ::askama::rocket::Status> {"); + let ext = match path.extension() { + Some(s) => s.to_str().unwrap(), + None => "txt", + }; + self.writeln("::askama::rocket::Response::build()"); + self.indent(); + self.writeln(&format!(".header(::askama::rocket::ContentType::from_extension({:?})\ + .unwrap())", ext)); + self.writeln(".sized_body(::std::io::Cursor::new(self.render().unwrap()))"); + self.writeln(".ok()"); + self.dedent(); + self.writeln("}"); + self.writeln("}"); + } + fn result(self) -> String { self.buf } -- cgit