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:
@@ -9,6 +9,7 @@ pub use parser::Filter;
|
||||
pub use parser::KVPair;
|
||||
pub use parser::OwnedLiteral;
|
||||
pub use parser::PartialNameElement;
|
||||
pub use parser::Path;
|
||||
pub use parser::RValue;
|
||||
pub use parser::Special;
|
||||
pub use parser::Template;
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -42,6 +42,12 @@ where
|
||||
B: Borrow<dyn ContextElement + 'a>,
|
||||
P: Borrow<str>,
|
||||
{
|
||||
if breadcrumbs.is_empty() {
|
||||
// This happens when you use a section with an explicit
|
||||
// context path, where both the path and the explicit context
|
||||
// path fail, leaving you with absolutely no context.
|
||||
return Err(WalkError::CantWalk);
|
||||
}
|
||||
if path.is_empty() {
|
||||
return Ok(breadcrumbs
|
||||
.last()
|
||||
|
||||
Reference in New Issue
Block a user