I think I have the code for the select tag finished, but I haven't yet implemented any or none.

This commit is contained in:
Tom Alexander 2020-06-07 18:44:27 -04:00
parent 576d94780a
commit f1b868ce33
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 43 additions and 6 deletions

View File

@ -20,6 +20,7 @@ use crate::renderer::inline_partial_tree::extract_inline_partials;
use crate::renderer::inline_partial_tree::InlinePartialTreeElement; use crate::renderer::inline_partial_tree::InlinePartialTreeElement;
use crate::renderer::iteration_context::IterationContext; use crate::renderer::iteration_context::IterationContext;
use crate::renderer::parameters_context::ParametersContext; use crate::renderer::parameters_context::ParametersContext;
use crate::renderer::select_context::SelectContext;
use crate::renderer::walking::walk_path; use crate::renderer::walking::walk_path;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::HashMap;
@ -75,7 +76,12 @@ impl<'a> DustRenderer<'a> {
breadcrumbs: breadcrumbs, breadcrumbs: breadcrumbs,
blocks: &new_blocks, blocks: &new_blocks,
}; };
self.render_body(&main_template.contents, breadcrumbs, &new_block_context) self.render_body(
&main_template.contents,
breadcrumbs,
&new_block_context,
None,
)
} }
fn render_maybe_body( fn render_maybe_body(
@ -83,10 +89,11 @@ impl<'a> DustRenderer<'a> {
body: &'a Option<Body>, body: &'a Option<Body>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>, breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>, blocks: &'a BlockContext<'a>,
select_context: Option<&'a SelectContext<'a>>,
) -> Result<String, RenderError> { ) -> Result<String, RenderError> {
match body { match body {
None => Ok("".to_owned()), None => Ok("".to_owned()),
Some(body) => Ok(self.render_body(body, breadcrumbs, blocks)?), Some(body) => Ok(self.render_body(body, breadcrumbs, blocks, select_context)?),
} }
} }
@ -95,6 +102,7 @@ impl<'a> DustRenderer<'a> {
body: &'a Body, body: &'a Body,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>, breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>, blocks: &'a BlockContext<'a>,
select_context: Option<&'a SelectContext<'a>>,
) -> Result<String, RenderError> { ) -> Result<String, RenderError> {
let mut output = String::new(); let mut output = String::new();
for elem in &body.elements { for elem in &body.elements {
@ -102,7 +110,7 @@ impl<'a> DustRenderer<'a> {
TemplateElement::TEIgnoredWhitespace(_) => {} TemplateElement::TEIgnoredWhitespace(_) => {}
TemplateElement::TESpan(span) => output.push_str(span.contents), TemplateElement::TESpan(span) => output.push_str(span.contents),
TemplateElement::TETag(dt) => { TemplateElement::TETag(dt) => {
output.push_str(&self.render_tag(dt, breadcrumbs, blocks)?); output.push_str(&self.render_tag(dt, breadcrumbs, blocks, select_context)?);
} }
} }
} }
@ -130,6 +138,7 @@ impl<'a> DustRenderer<'a> {
}, },
breadcrumbs, breadcrumbs,
&empty_block_context, &empty_block_context,
None,
) )
} }
@ -138,6 +147,7 @@ impl<'a> DustRenderer<'a> {
tag: &'a DustTag, tag: &'a DustTag,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>, breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>, blocks: &'a BlockContext<'a>,
select_context: Option<&'a SelectContext<'a>>,
) -> Result<String, RenderError> { ) -> Result<String, RenderError> {
match tag { match tag {
DustTag::DTComment(_comment) => (), DustTag::DTComment(_comment) => (),
@ -186,6 +196,7 @@ impl<'a> DustRenderer<'a> {
&container.else_contents, &container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
); );
} }
Ok(Some(final_val)) => { Ok(Some(final_val)) => {
@ -209,6 +220,7 @@ impl<'a> DustRenderer<'a> {
body, body,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
} else { } else {
// Array-like value // Array-like value
@ -234,6 +246,7 @@ impl<'a> DustRenderer<'a> {
.as_ref() .as_ref()
.unwrap_or(breadcrumbs), .unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
}) })
.collect(); .collect();
@ -258,6 +271,7 @@ impl<'a> DustRenderer<'a> {
&container.else_contents, &container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
}; };
} }
@ -278,11 +292,13 @@ impl<'a> DustRenderer<'a> {
&container.contents, &container.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
_ => self.render_maybe_body( _ => self.render_maybe_body(
&container.else_contents, &container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
}; };
} }
@ -301,11 +317,13 @@ impl<'a> DustRenderer<'a> {
&container.else_contents, &container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
_ => self.render_maybe_body( _ => self.render_maybe_body(
&container.contents, &container.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
}; };
} }
@ -357,11 +375,13 @@ impl<'a> DustRenderer<'a> {
&named_block.contents, &named_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
Some(inline_partial) => self.render_maybe_body( Some(inline_partial) => self.render_maybe_body(
inline_partial, inline_partial,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
), ),
}; };
} }
@ -379,19 +399,25 @@ impl<'a> DustRenderer<'a> {
); );
return self return self
.perform_comparison_check(tag, breadcrumbs, None) .perform_comparison_check(
tag,
breadcrumbs,
select_context.map(|sc| sc.select_parameters),
)
.map(|check_result| { .map(|check_result| {
if check_result { if check_result {
self.render_maybe_body( self.render_maybe_body(
&parameterized_block.contents, &parameterized_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
} else { } else {
self.render_maybe_body( self.render_maybe_body(
&parameterized_block.else_contents, &parameterized_block.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
} }
}) })
@ -413,6 +439,7 @@ impl<'a> DustRenderer<'a> {
&parameterized_block.contents, &parameterized_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
) )
} }
(Ok(index_resolved), Ok(len_resolved)) => { (Ok(index_resolved), Ok(len_resolved)) => {
@ -437,6 +464,7 @@ impl<'a> DustRenderer<'a> {
&parameterized_block.contents, &parameterized_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
); );
} }
} }
@ -471,6 +499,7 @@ impl<'a> DustRenderer<'a> {
&parameterized_block.contents, &parameterized_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
); );
} }
} }
@ -513,6 +542,7 @@ impl<'a> DustRenderer<'a> {
&parameterized_block.contents, &parameterized_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
None,
); );
} }
} }
@ -561,6 +591,13 @@ impl<'a> DustRenderer<'a> {
) )
}) })
.any(|check_result| check_result.unwrap_or(false)); .any(|check_result| check_result.unwrap_or(false));
let select_context = SelectContext::new(&param_map, are_any_checks_true);
return self.render_maybe_body(
&parameterized_block.contents,
new_breadcrumbs_ref,
blocks,
Some(&select_context),
);
} }
} }
} }

View File

@ -2,8 +2,8 @@ use crate::renderer::parameters_context::ParametersContext;
#[derive(Debug)] #[derive(Debug)]
pub struct SelectContext<'a> { pub struct SelectContext<'a> {
select_parameters: &'a ParametersContext<'a>, pub select_parameters: &'a ParametersContext<'a>,
were_any_true: bool, pub were_any_true: bool,
} }
impl<'a> SelectContext<'a> { impl<'a> SelectContext<'a> {