diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-03-08 19:55:19 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-03-08 19:55:27 +0100 |
commit | c927d51fe10a2050e0afd143e300615facd781b7 (patch) | |
tree | 9a18b87e29af325e255a4b06825f722131dbbbbe /askama_derive/src/generator.rs | |
parent | f229c795173db98c4552b0af11eb0a1b8c762ecc (diff) | |
download | askama-c927d51fe10a2050e0afd143e300615facd781b7.tar.gz askama-c927d51fe10a2050e0afd143e300615facd781b7.tar.bz2 askama-c927d51fe10a2050e0afd143e300615facd781b7.zip |
Copy type parameters and where clauses (fixes #11)
Diffstat (limited to 'askama_derive/src/generator.rs')
-rw-r--r-- | askama_derive/src/generator.rs | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index af07470..2eed03f 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -2,6 +2,7 @@ use parser::{Cond, Expr, Node, Target, WS}; use std::str; use std::collections::HashSet; use syn; +use quote::{Tokens, ToTokens}; fn path_as_identifier(s: &str) -> String { let mut res = String::new(); @@ -308,22 +309,51 @@ impl<'a> Generator<'a> { fn write_header(&mut self, ast: &syn::DeriveInput, trait_suffix: Option<&str>) { - let mut anno = String::new(); - if ast.generics.lifetimes.len() > 0 { - anno.push('<'); - for lt in &ast.generics.lifetimes { - anno.push_str(lt.lifetime.ident.as_ref()); + let mut full_anno = Tokens::new(); + let mut orig_anno = Tokens::new(); + let need_anno = ast.generics.lifetimes.len() > 0 || + ast.generics.ty_params.len() > 0; + if need_anno { + full_anno.append("<"); + orig_anno.append("<"); + } + + let mut sep = false; + for lt in &ast.generics.lifetimes { + if sep { + full_anno.append(","); + orig_anno.append(","); } - anno.push('>'); - }; + lt.to_tokens(&mut full_anno); + lt.to_tokens(&mut orig_anno); + sep = true; + } + + for param in &ast.generics.ty_params { + if sep { + full_anno.append(","); + orig_anno.append(","); + } + param.to_tokens(&mut full_anno); + param.ident.to_tokens(&mut orig_anno); + sep = true; + } + + if need_anno { + full_anno.append(">"); + orig_anno.append(">"); + } + let mut where_clause = Tokens::new(); + ast.generics.where_clause.to_tokens(&mut where_clause); let name = if trait_suffix.is_some() { format!("TraitFrom{}", trait_suffix.unwrap()) } else { "askama::Template".to_string() }; - self.writeln(&format!("impl{} {} for {}{} {{", - anno, &name, ast.ident.as_ref(), anno)); + self.writeln(&format!("impl{} {} for {}{}{} {{", + full_anno.as_str(), &name, ast.ident.as_ref(), + orig_anno.as_str(), where_clause.as_str())); } fn impl_template(&mut self, ast: &syn::DeriveInput, nodes: &'a [Node]) { |