Add a new trait to ContextElement for Truthiness.
Before I was relying on Loopable to both determine truthiness and get a list of elements to loop over. This will no longer work since I need to only set $idx and $len when iterating over actual arrays, as opposed to all truthy values, so I've finally made truthiness explicit.
This commit is contained in:
@@ -524,12 +524,19 @@ mod tests {
|
||||
use crate::parser::Filter;
|
||||
use crate::renderer::context_element::Loopable;
|
||||
use crate::renderer::context_element::Renderable;
|
||||
use crate::renderer::context_element::Truthiness;
|
||||
use crate::renderer::context_element::Walkable;
|
||||
use crate::renderer::CompareContextElement;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
impl ContextElement for String {}
|
||||
|
||||
impl Truthiness for String {
|
||||
fn is_truthy(&self) -> bool {
|
||||
!self.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for String {
|
||||
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
||||
Ok(self.clone())
|
||||
@@ -569,6 +576,12 @@ mod tests {
|
||||
}
|
||||
impl ContextElement for u64 {}
|
||||
|
||||
impl Truthiness for u64 {
|
||||
fn is_truthy(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for u64 {
|
||||
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
||||
Ok(self.to_string())
|
||||
@@ -605,6 +618,12 @@ mod tests {
|
||||
|
||||
impl<I: 'static + ContextElement + Clone> ContextElement for HashMap<String, I> {}
|
||||
|
||||
impl<I: ContextElement> Truthiness for HashMap<String, I> {
|
||||
fn is_truthy(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: ContextElement> Renderable for HashMap<String, I> {
|
||||
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
||||
// TODO: handle the filters
|
||||
|
||||
Reference in New Issue
Block a user