You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.4 KiB
Rust

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<Body<'a>>>,
}
impl<'a> InlinePartialTreeElement<'a> {
pub fn new(
parent: Option<&'a InlinePartialTreeElement<'a>>,
blocks: HashMap<&'a str, &'a Option<Body<'a>>>,
) -> InlinePartialTreeElement<'a> {
InlinePartialTreeElement {
parent: parent,
blocks: blocks,
}
}
pub fn get_block(&self, name: &str) -> Option<&'a Option<Body<'a>>> {
match self.blocks.get(name) {
None => match self.parent {
None => None,
Some(parent_tree_element) => parent_tree_element.get_block(name),
},
Some(interior) => Some(interior),
}
}
}
pub fn extract_inline_partials<'a>(
template: &'a Template<'a>,
) -> HashMap<&'a str, &'a Option<Body<'a>>> {
let mut blocks: HashMap<&'a str, &'a Option<Body<'a>>> = 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 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<Body<'a>>>,
tag: &'a DustTag,
) {
match tag {
DustTag::DTComment(..) => (),
DustTag::DTSpecial(..) => (),
DustTag::DTLiteralStringBlock(..) => (),
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.path.keys[0], &named_block.contents);
}
DustTag::DTBlock(..) => (),
DustTag::DTHelperEquals(parameterized_block)
| DustTag::DTHelperNotEquals(parameterized_block)
| DustTag::DTHelperGreaterThan(parameterized_block)
| DustTag::DTHelperLessThan(parameterized_block)
| DustTag::DTHelperGreaterThanOrEquals(parameterized_block)
| DustTag::DTHelperLessThanOrEquals(parameterized_block)
| DustTag::DTHelperSep(parameterized_block)
| DustTag::DTHelperFirst(parameterized_block)
| DustTag::DTHelperLast(parameterized_block)
| DustTag::DTHelperSelect(parameterized_block)
| DustTag::DTHelperAny(parameterized_block)
| DustTag::DTHelperNone(parameterized_block)
| DustTag::DTHelperMath(parameterized_block) => {
match &parameterized_block.contents {
None => (),
Some(body) => extract_inline_partials_from_body(blocks, &body),
};
match &parameterized_block.else_contents {
None => (),
Some(body) => extract_inline_partials_from_body(blocks, &body),
};
}
}
}