From 7979d5c1dd790d1821c9c4c77c848f47ac49c769 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 6 Oct 2023 11:36:59 +0200 Subject: Attach find_used_templates() to TemplateInput --- askama_derive/src/generator.rs | 43 +-------------------------------------- askama_derive/src/input.rs | 46 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 44 deletions(-) (limited to 'askama_derive/src') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 3374161..21b8d25 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -31,7 +31,7 @@ pub(crate) fn build_template(ast: &syn::DeriveInput) -> Result Result, - map: &mut HashMap, - source: String, -) -> Result<(), CompileError> { - let mut dependency_graph = Vec::new(); - let mut check = vec![(input.path.clone(), source)]; - while let Some((path, source)) = check.pop() { - let parsed = Parsed::new(source, input.syntax)?; - for n in parsed.nodes() { - match n { - Node::Extends(extends) => { - let extends = input.config.find_template(extends.path, Some(&path))?; - let dependency_path = (path.clone(), extends.clone()); - if dependency_graph.contains(&dependency_path) { - return Err(format!( - "cyclic dependency in graph {:#?}", - dependency_graph - .iter() - .map(|e| format!("{:#?} --> {:#?}", e.0, e.1)) - .collect::>() - ) - .into()); - } - dependency_graph.push(dependency_path); - let source = get_template_source(&extends)?; - check.push((extends, source)); - } - Node::Import(import) => { - let import = input.config.find_template(import.path, Some(&path))?; - let source = get_template_source(&import)?; - check.push((import, source)); - } - _ => {} - } - } - map.insert(path, parsed); - } - Ok(()) -} - struct Generator<'a> { // The template input state: original struct AST and attributes input: &'a TemplateInput<'a>, diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index 5977058..03817f1 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -1,3 +1,4 @@ +use std::collections::hash_map::HashMap; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -5,9 +6,9 @@ use mime::Mime; use quote::ToTokens; use syn::punctuated::Punctuated; -use crate::config::Config; +use crate::config::{get_template_source, Config}; use crate::CompileError; -use parser::Syntax; +use parser::{Node, Parsed, Syntax}; pub(crate) struct TemplateInput<'a> { pub(crate) ast: &'a syn::DeriveInput, @@ -100,6 +101,47 @@ impl TemplateInput<'_> { }) } + pub(crate) fn find_used_templates( + &self, + map: &mut HashMap, + source: String, + ) -> Result<(), CompileError> { + let mut dependency_graph = Vec::new(); + let mut check = vec![(self.path.clone(), source)]; + while let Some((path, source)) = check.pop() { + let parsed = Parsed::new(source, self.syntax)?; + for n in parsed.nodes() { + match n { + Node::Extends(extends) => { + let extends = self.config.find_template(extends.path, Some(&path))?; + let dependency_path = (path.clone(), extends.clone()); + if dependency_graph.contains(&dependency_path) { + return Err(format!( + "cyclic dependency in graph {:#?}", + dependency_graph + .iter() + .map(|e| format!("{:#?} --> {:#?}", e.0, e.1)) + .collect::>() + ) + .into()); + } + dependency_graph.push(dependency_path); + let source = get_template_source(&extends)?; + check.push((extends, source)); + } + Node::Import(import) => { + let import = self.config.find_template(import.path, Some(&path))?; + let source = get_template_source(&import)?; + check.push((import, source)); + } + _ => {} + } + } + map.insert(path, parsed); + } + Ok(()) + } + #[inline] pub(crate) fn extension(&self) -> Option<&str> { ext_default_to_path(self.ext.as_deref(), &self.path) -- cgit