diff options
Diffstat (limited to '')
-rw-r--r-- | askama_derive/Cargo.toml | 4 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 12 |
2 files changed, 16 insertions, 0 deletions
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 3bec13a..40f1ad5 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -11,6 +11,10 @@ workspace = ".." [lib] proc-macro = true +[features] +default = [] +iron = [] + [dependencies] nom = "3" syn = "0.11" diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 6eb16e8..57d54b1 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -53,6 +53,9 @@ pub fn generate(ast: &syn::DeriveInput, path: &Path, mut nodes: Vec<Node>) -> St gen.impl_template(ast, &content); } gen.impl_display(ast); + if cfg!(feature = "iron") { + gen.impl_modifier_response(ast); + } gen.result() } @@ -618,6 +621,15 @@ impl<'a> Generator<'a> { self.writeln("}"); } + // Implement iron's Modifier<Response> if enabled + fn impl_modifier_response(&mut self, ast: &syn::DeriveInput) { + self.write_header(ast, "::askama::iron::Modifier<::askama::iron::Response>"); + self.writeln("fn modify(self, res: &mut ::askama::iron::Response) {"); + self.writeln("res.body = Some(Box::new(self.render().unwrap().into_bytes()));"); + self.writeln("}"); + self.writeln("}"); + } + fn result(self) -> String { self.buf } |