use crate::parser::Body; use crate::parser::DustTag; use crate::parser::Template; use crate::parser::TemplateElement; use std::collections::HashMap; pub struct InlinePartialTreeElement<'a> { parent: Option<&'a InlinePartialTreeElement<'a>>, blocks: HashMap<&'a str, &'a Option>>, } impl<'a> InlinePartialTreeElement<'a> { pub fn new( parent: Option<&'a InlinePartialTreeElement<'a>>, blocks: HashMap<&'a str, &'a Option>>, ) -> InlinePartialTreeElement<'a> { InlinePartialTreeElement { parent: parent, blocks: blocks, } } } pub fn extract_inline_partials<'a>( template: &'a Template<'a>, ) -> HashMap<&'a str, &'a Option>> { let mut blocks: HashMap<&'a str, &'a Option>> = HashMap::new(); extract_inline_partials_from_body(&mut blocks, &template.contents); blocks } fn extract_inline_partials_from_body<'a, 'b>( blocks: &'b mut HashMap<&'a str, &'a Option>>, body: &'a Body<'a>, ) { for elem in &body.elements { match elem { TemplateElement::TEIgnoredWhitespace(_) => (), TemplateElement::TESpan(span) => (), TemplateElement::TETag(dt) => { extract_inline_partials_from_tag(blocks, dt); } } } } fn extract_inline_partials_from_tag<'a, 'b>( blocks: &'b mut HashMap<&'a str, &'a Option>>, tag: &'a DustTag, ) { match tag { DustTag::DTComment(..) => (), DustTag::DTSpecial(..) => (), DustTag::DTReference(..) => (), DustTag::DTSection(container) => { match &container.contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; match &container.else_contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; } DustTag::DTExists(container) => { match &container.contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; match &container.else_contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; } DustTag::DTNotExists(container) => { match &container.contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; match &container.else_contents { None => (), Some(body) => extract_inline_partials_from_body(blocks, &body), }; } DustTag::DTPartial(..) => (), DustTag::DTInlinePartial(named_block) => { blocks.insert(&named_block.name, &named_block.contents); } DustTag::DTBlock(..) => (), _ => (), // TODO: Implement the rest } }