Add tests for explicit context setting that check failure conditions and implement a helper function to generate a new breadcrumb stack.

This commit is contained in:
Tom Alexander
2020-05-25 15:40:42 -04:00
parent 8121c93392
commit 4a21ae5af3
6 changed files with 184 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ use crate::parser::Body;
use crate::parser::DustTag;
use crate::parser::KVPair;
use crate::parser::PartialNameElement;
use crate::parser::Path;
use crate::parser::RValue;
use crate::parser::Special;
use crate::parser::Template;
@@ -551,6 +552,61 @@ impl<'a> DustRenderer<'a> {
}
final_filters
}
/// Generate a new breadcrumbs object
///
/// This function generates a new breadcrumbs object based on the
/// new context information provided.
///
/// explicit_context is for contexts specified with a `:path`
/// inside a dust tag.
///
/// injected_context is for any generated context. This includes
/// both parameters on a tag and also the handling of $idx and
/// $len.
///
/// New context element is the element is an element to append to
/// the end, generally for use in section tags which walk to a new
/// context.
///
/// If explicit_context is not None, then the final breadcrumb stack will be:
///
/// ```
/// breadcrumbs
/// injected_context
/// new_context_element
/// ```
///
/// However, if explicit_context is not None, then the old
/// breadcrumbs are omitted, leading to the new breadcrumb stack
/// as:
///
/// ```
/// injected_context
/// explicit_context
/// new_context_element
/// ```
fn new_breadcrumbs<'b>(
breadcrumbs: &'b Vec<&'b dyn ContextElement>,
injected_context: Option<&'b dyn ContextElement>,
explicit_context: &Option<Path<'b>>,
new_context_element: Option<&'b dyn ContextElement>,
) -> Vec<&'b dyn ContextElement> {
let mut new_stack = match explicit_context {
Some(_) => Vec::with_capacity(3),
None => breadcrumbs.clone(),
};
injected_context.map(|ctx| new_stack.push(ctx));
explicit_context.as_ref().map(|path| {
walk_path(breadcrumbs, &path.keys).map(|val| {
if val.is_truthy() {
new_stack.push(val)
}
});
});
new_context_element.map(|ctx| new_stack.push(ctx));
new_stack
}
}
#[cfg(test)]