duster/src/renderer/inline_partial_tree.rs

95 lines
2.9 KiB
Rust
Raw Normal View History

use crate::parser::Body;
use crate::parser::DustTag;
use crate::parser::Template;
use crate::parser::TemplateElement;
use std::collections::HashMap;
2020-05-09 22:05:43 -04:00
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 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::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
}
}