From 06fd5188056fa59ee194ce1f031e4160e550383c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 21 Jun 2018 17:33:39 +0200 Subject: Flatten inherited blocks for code generation --- askama_derive/src/lib.rs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) (limited to 'askama_derive/src/lib.rs') diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 07ba0a6..ad1c123 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -17,7 +17,7 @@ use proc_macro::TokenStream; use shared::path; use std::collections::HashMap; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { @@ -92,10 +92,9 @@ fn find_used_templates(map: &mut HashMap, path: PathBuf, source pub(crate) struct Context<'a> { nodes: &'a [Node<'a>], extends: Option, - blocks: Vec<&'a Node<'a>>, + blocks: HashMap<&'a str, &'a Node<'a>>, macros: HashMap<&'a str, &'a Macro<'a>>, imports: HashMap<&'a str, PathBuf>, - trait_name: String, } impl<'a> Context<'a> { @@ -143,10 +142,14 @@ impl<'a> Context<'a> { check_nested += 1; } - let trait_name = match extends { - Some(ref path) => trait_name_for_path(path), - None => trait_name_for_path(&input.path), - }; + let blocks: HashMap<_, _> = blocks + .iter() + .map(|def| if let Node::BlockDef(_, name, _, _) = def { + (*name, *def) + } else { + unreachable!() + }) + .collect(); Context { nodes, @@ -154,20 +157,6 @@ impl<'a> Context<'a> { blocks, macros, imports, - trait_name, - } - } -} - -fn trait_name_for_path(path: &Path) -> String { - let mut res = String::new(); - res.push_str("TraitFrom"); - for c in path.to_string_lossy().chars() { - if c.is_alphanumeric() { - res.push(c); - } else { - res.push_str(&format!("{:x}", c as u32)); } } - res } -- cgit