From 31bad55f2c3729a37ecc9d23535fc0c3ce53772d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 4 Feb 2017 16:23:47 +0100 Subject: Add support for lifetime annotations on template structs --- askama/src/generator.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 0fe3ba4..cabe381 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -21,10 +21,28 @@ impl Generator { } } - fn init(&mut self, name: &str) { - self.write("impl askama::Template for "); + fn annotations(&self, generics: &syn::Generics) -> String { + if generics.lifetimes.len() < 1 { + return String::new(); + } + let mut res = String::new(); + res.push('<'); + for lt in &generics.lifetimes { + res.push_str(lt.lifetime.ident.as_ref()); + } + res.push('>'); + res + } + + fn init(&mut self, name: &str, generics: &syn::Generics) { + self.write("impl"); + let anno = self.annotations(generics); + self.write(&anno); + self.write(" askama::Template for "); self.write(name); + self.write(&anno); self.writeln(" {"); + self.indent(); self.writeln("fn render(&self) -> String {"); self.indent(); @@ -173,7 +191,7 @@ impl Generator { pub fn generate(ast: &syn::DeriveInput, tokens: &Vec) -> String { let mut gen = Generator::new(); - gen.init(ast.ident.as_ref()); + gen.init(ast.ident.as_ref(), &ast.generics); gen.handle(tokens); gen.finalize(); gen.result() -- cgit