Add a Loopable trait for dust sections.

This commit is contained in:
Tom Alexander
2020-04-28 19:34:52 -04:00
parent c961cf7ab8
commit e5c4ba8c82
4 changed files with 72 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ use crate::parser::Filter;
use crate::renderer::errors::RenderError;
use std::fmt::Debug;
pub trait ContextElement: Walkable + Renderable + Debug {}
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {}
pub trait Walkable {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>;
@@ -11,3 +11,18 @@ pub trait Walkable {
pub trait Renderable {
fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError>;
}
pub trait Loopable {
/// Return the elements for a Dust section
///
/// Sections in dust are accomplished with the {#path} syntax. A
/// section has a truthiness check performed on it. If that
/// truthiness check fails, then it will render the
/// else-block. Otherwise if its a scalar value it will render
/// once with the context being the element at that path. Finally,
/// if its an array-like value then it will render n-times, once
/// for each element of the array.
///
/// TODO: Should this return an iterator instead of a vec?
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError>;
}