diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-06-21 17:33:39 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-06-21 17:33:39 +0200 |
commit | 06fd5188056fa59ee194ce1f031e4160e550383c (patch) | |
tree | 4c62f639946ac947086ab41f477af2cde2dd2ee8 /askama_derive/src/lib.rs | |
parent | 532e252270697af2737153d27dd13f978ef856df (diff) | |
download | askama-06fd5188056fa59ee194ce1f031e4160e550383c.tar.gz askama-06fd5188056fa59ee194ce1f031e4160e550383c.tar.bz2 askama-06fd5188056fa59ee194ce1f031e4160e550383c.zip |
Flatten inherited blocks for code generation
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/lib.rs | 31 |
1 files changed, 10 insertions, 21 deletions
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<PathBuf, String>, path: PathBuf, source pub(crate) struct Context<'a> { nodes: &'a [Node<'a>], extends: Option<PathBuf>, - 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 } |