Switch the get_loop_elements implementation to only return populated arrays when its an array-like object.

master
Tom Alexander 4 years ago
parent 59ee4f508f
commit 966499db76
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -294,30 +294,8 @@ impl Walkable for serde_json::Value {
impl Loopable for serde_json::Value {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
match self {
serde_json::Value::Null => Vec::new(),
serde_json::Value::Bool(boolean) => {
if *boolean {
vec![self]
} else {
Vec::new()
}
}
serde_json::Value::Number(_num) => vec![self],
serde_json::Value::String(string_value) => {
if string_value.is_empty() {
Vec::new()
} else {
vec![self]
}
}
serde_json::Value::Array(array_value) => {
if array_value.is_empty() {
Vec::new()
} else {
array_value.iter().map(|x| x as _).collect()
}
}
serde_json::Value::Object(_obj) => vec![self],
serde_json::Value::Array(array_value) => array_value.iter().map(|x| x as _).collect(),
_ => Vec::new(),
}
}
}

@ -30,13 +30,12 @@ pub trait Renderable {
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.
/// Sections in dust are accomplished with the {#path} syntax. If
/// its an array-like value then it will render n-times, once for
/// each element of the array. If this is a scalar value, then
/// return an empty array. Sections with scalar values will still
/// be rendered (only once) if their truthiness check comes back
/// true.
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
}

@ -54,7 +54,7 @@ impl Loopable for IterationContext {
// TODO: Would this even ever be called? Won't matter, but I'd
// like to know. Since it is injected 1 above the current
// context, we wouldn't be able to access it with `{.}`.
vec![self]
Vec::new()
}
}

@ -91,7 +91,7 @@ impl Loopable for ParametersContext {
// TODO: Would this even ever be called? Won't matter, but I'd
// like to know. Since it is injected 1 above the current
// context, we wouldn't be able to access it with `{.}`.
vec![self]
Vec::new()
}
}
@ -158,16 +158,7 @@ impl Renderable for OwnedLiteral {
impl Loopable for OwnedLiteral {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
match self {
OwnedLiteral::LString(text) => {
if text.is_empty() {
Vec::new()
} else {
vec![self]
}
}
OwnedLiteral::LPositiveInteger(num) => vec![self],
}
Vec::new()
}
}

@ -545,11 +545,7 @@ mod tests {
impl Loopable for String {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
if self.is_empty() {
Vec::new()
} else {
vec![self]
}
Vec::new()
}
}
@ -590,7 +586,7 @@ mod tests {
impl Loopable for u64 {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
vec![self]
Vec::new()
}
}
@ -640,7 +636,7 @@ mod tests {
impl<I: 'static + ContextElement + Clone> Loopable for HashMap<String, I> {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
vec![self]
Vec::new()
}
}

Loading…
Cancel
Save