Switch the get_loop_elements implementation to only return populated arrays when its an array-like object.
This commit is contained in:
parent
59ee4f508f
commit
966499db76
26
src/bin.rs
26
src/bin.rs
@ -294,30 +294,8 @@ impl Walkable for serde_json::Value {
|
|||||||
impl Loopable for serde_json::Value {
|
impl Loopable for serde_json::Value {
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
match self {
|
match self {
|
||||||
serde_json::Value::Null => Vec::new(),
|
serde_json::Value::Array(array_value) => array_value.iter().map(|x| x as _).collect(),
|
||||||
serde_json::Value::Bool(boolean) => {
|
_ => Vec::new(),
|
||||||
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],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,13 +30,12 @@ pub trait Renderable {
|
|||||||
pub trait Loopable {
|
pub trait Loopable {
|
||||||
/// Return the elements for a Dust section
|
/// Return the elements for a Dust section
|
||||||
///
|
///
|
||||||
/// Sections in dust are accomplished with the {#path} syntax. A
|
/// Sections in dust are accomplished with the {#path} syntax. If
|
||||||
/// section has a truthiness check performed on it. If that
|
/// its an array-like value then it will render n-times, once for
|
||||||
/// truthiness check fails, then it will render the
|
/// each element of the array. If this is a scalar value, then
|
||||||
/// else-block. Otherwise if its a scalar value it will render
|
/// return an empty array. Sections with scalar values will still
|
||||||
/// once with the context being the element at that path. Finally,
|
/// be rendered (only once) if their truthiness check comes back
|
||||||
/// if its an array-like value then it will render n-times, once
|
/// true.
|
||||||
/// for each element of the array.
|
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
|
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
|
// TODO: Would this even ever be called? Won't matter, but I'd
|
||||||
// like to know. Since it is injected 1 above the current
|
// like to know. Since it is injected 1 above the current
|
||||||
// context, we wouldn't be able to access it with `{.}`.
|
// 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
|
// TODO: Would this even ever be called? Won't matter, but I'd
|
||||||
// like to know. Since it is injected 1 above the current
|
// like to know. Since it is injected 1 above the current
|
||||||
// context, we wouldn't be able to access it with `{.}`.
|
// 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 {
|
impl Loopable for OwnedLiteral {
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
match self {
|
Vec::new()
|
||||||
OwnedLiteral::LString(text) => {
|
|
||||||
if text.is_empty() {
|
|
||||||
Vec::new()
|
|
||||||
} else {
|
|
||||||
vec![self]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
OwnedLiteral::LPositiveInteger(num) => vec![self],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -545,11 +545,7 @@ mod tests {
|
|||||||
|
|
||||||
impl Loopable for String {
|
impl Loopable for String {
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
if self.is_empty() {
|
Vec::new()
|
||||||
Vec::new()
|
|
||||||
} else {
|
|
||||||
vec![self]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -590,7 +586,7 @@ mod tests {
|
|||||||
|
|
||||||
impl Loopable for u64 {
|
impl Loopable for u64 {
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
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> {
|
impl<I: 'static + ContextElement + Clone> Loopable for HashMap<String, I> {
|
||||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
vec![self]
|
Vec::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user