diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-04 16:23:47 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-04 16:23:47 +0100 |
commit | 31bad55f2c3729a37ecc9d23535fc0c3ce53772d (patch) | |
tree | 8a0dd8c734f7ebf4ab4e607d8c6cd7ddf11a97b4 | |
parent | 3478395ca91413e007b06da600f4dba3370d197c (diff) | |
download | askama-31bad55f2c3729a37ecc9d23535fc0c3ce53772d.tar.gz askama-31bad55f2c3729a37ecc9d23535fc0c3ce53772d.tar.bz2 askama-31bad55f2c3729a37ecc9d23535fc0c3ce53772d.zip |
Add support for lifetime annotations on template structs
-rw-r--r-- | askama/src/generator.rs | 24 |
1 files 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<Node>) -> 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() |