diff options
-rw-r--r-- | askama/src/generator.rs | 25 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 2 |
2 files changed, 25 insertions, 2 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 4c91d14..22931c7 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -3,6 +3,18 @@ use std::str; use std::collections::HashSet; use syn; +fn path_as_identifier(s: &str) -> String { + let mut res = String::new(); + for c in s.chars() { + if c.is_alphanumeric() { + res.push(c); + } else { + res.push_str(&format!("{:x}", c as u32)); + } + } + res +} + struct Generator { buf: String, indent: u8, @@ -160,6 +172,16 @@ impl Generator { } } + fn path_based_name(&mut self, ast: &syn::DeriveInput, path: &str) { + let encoded = path_as_identifier(path); + let original = ast.ident.as_ref(); + let anno = self.annotations(&ast.generics); + self.writeln("#[allow(dead_code, non_camel_case_types)]"); + let s = format!("type TemplateFrom{}{} = {}{};", + encoded, &anno, original, &anno); + self.writeln(&s); + } + fn annotations(&self, generics: &syn::Generics) -> String { if generics.lifetimes.len() < 1 { return String::new(); @@ -200,8 +222,9 @@ impl Generator { } -pub fn generate(ast: &syn::DeriveInput, nodes: Vec<Node>) -> String { +pub fn generate(ast: &syn::DeriveInput, path: &str, nodes: Vec<Node>) -> String { let mut gen = Generator::new(); + gen.path_based_name(ast, path); gen.template_impl(ast, &nodes); gen.result() } diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 03bfe7d..ba5dec4 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -59,5 +59,5 @@ pub fn derive_template(input: TokenStream) -> TokenStream { let path = get_path_from_attrs(&ast.attrs); let src = get_template_source(&path); let nodes = askama::parser::parse(&src); - askama::generator::generate(&ast, nodes).parse().unwrap() + askama::generator::generate(&ast, &path, nodes).parse().unwrap() } |