Update IterationContext to be an IntoContextElement and finish implementing section.

This commit is contained in:
Tom Alexander
2020-06-06 23:08:21 -04:00
parent 00699b84ba
commit e28ebaf26a
3 changed files with 102 additions and 52 deletions

View File

@@ -15,6 +15,7 @@ use crate::renderer::errors::RenderError;
use crate::renderer::errors::WalkError;
use crate::renderer::inline_partial_tree::extract_inline_partials;
use crate::renderer::inline_partial_tree::InlinePartialTreeElement;
use crate::renderer::iteration_context::IterationContext;
use crate::renderer::parameters_context::ParametersContext;
use crate::renderer::tree_walking::walk_path;
use std::borrow::Borrow;
@@ -170,8 +171,7 @@ impl<'a> DustRenderer<'a> {
let val = walk_path(breadcrumbs, &container.path.keys)
.map(|ice| ice.into_context_element(self, breadcrumbs));
match val {
Err(WalkError::CantWalk) => {
// TODO
Err(WalkError::CantWalk) | Ok(None) => {
let new_breadcrumbs = self.new_breadcrumbs_section(
breadcrumbs,
None,
@@ -179,9 +179,84 @@ impl<'a> DustRenderer<'a> {
&container.explicit_context,
None,
);
return self.render_maybe_body(
&container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks,
);
}
Ok(final_val) => {
// TODO
Ok(Some(final_val)) => {
let context_element = final_val.get_context_element_reference();
return if context_element.is_truthy() {
match &container.contents {
None => Ok("".to_owned()),
Some(body) => {
let loop_elements: Vec<&dyn ContextElement> =
context_element.get_loop_elements();
if loop_elements.is_empty() {
// Scalar value
let new_breadcrumbs = self.new_breadcrumbs_section(
breadcrumbs,
None,
Some(&injected_context),
&container.explicit_context,
Some(context_element),
);
self.render_body(
body,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks,
)
} else {
// Array-like value
let total_length = loop_elements.len();
let rendered_results: Result<Vec<String>, RenderError> =
loop_elements
.into_iter()
.enumerate()
.map(|(i, array_elem)| {
let index_context =
IterationContext::new(i, total_length);
let new_breadcrumbs = self
.new_breadcrumbs_section(
breadcrumbs,
Some(&index_context),
Some(&injected_context),
&container.explicit_context,
Some(array_elem),
);
self.render_body(
&body,
new_breadcrumbs
.as_ref()
.unwrap_or(breadcrumbs),
blocks,
)
})
.collect();
let rendered_slice: &[String] = &rendered_results?;
return Ok(rendered_slice.join(""));
}
}
}
} else {
// Oddly enough if the value is falsey (like
// an empty array or null), Dust uses the
// original context before walking the path as
// the context for rendering the else block
let new_breadcrumbs = self.new_breadcrumbs_section(
breadcrumbs,
None,
Some(&injected_context),
&container.explicit_context,
None,
);
self.render_maybe_body(
&container.else_contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks,
)
};
}
}
}
@@ -247,7 +322,6 @@ impl<'a> DustRenderer<'a> {
injected_context: Option<&'b dyn IntoContextElement>,
explicit_context: &Option<Path<'b>>,
) -> Option<Vec<BreadcrumbTreeElement<'b>>> {
// If none of the additional contexts are present, return None
// to signal that the original breadcrumbs should be used
// rather than incurring a copy here.
@@ -270,8 +344,11 @@ impl<'a> DustRenderer<'a> {
// added after the current context but before the explicit
// context.
match explicit_context {
None => new_stack.insert(Self::get_index_of_first_non_pseudo_element(&new_stack).unwrap_or(0), BreadcrumbTreeElement::from_borrowed(ctx)),
_ => new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx))
None => new_stack.insert(
Self::get_index_of_first_non_pseudo_element(&new_stack).unwrap_or(0),
BreadcrumbTreeElement::from_borrowed(ctx),
),
_ => new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx)),
}
});
@@ -308,11 +385,12 @@ impl<'a> DustRenderer<'a> {
final_filters
}
fn get_index_of_first_non_pseudo_element<'b>(breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>) -> Option<usize>
{
breadcrumbs
.iter()
.rposition(|b| std::borrow::Borrow::<dyn IntoContextElement + 'b>::borrow(b).is_pseudo_element())
fn get_index_of_first_non_pseudo_element<'b>(
breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
) -> Option<usize> {
breadcrumbs.iter().rposition(|b| {
std::borrow::Borrow::<dyn IntoContextElement + 'b>::borrow(b).is_pseudo_element()
})
}
}