2020-05-24 16:01:56 -04:00
|
|
|
use crate::renderer::context_element::CompareContextElement;
|
|
|
|
use crate::renderer::context_element::ContextElement;
|
|
|
|
use crate::renderer::Loopable;
|
|
|
|
use crate::renderer::RenderError;
|
|
|
|
use crate::renderer::Renderable;
|
2020-05-24 16:16:43 -04:00
|
|
|
use crate::renderer::Truthiness;
|
2020-05-24 16:01:56 -04:00
|
|
|
use crate::renderer::WalkError;
|
|
|
|
use crate::{parser::Filter, parser::OwnedLiteral, renderer::Walkable};
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
/// An injected context for $idx and $len
|
|
|
|
///
|
|
|
|
/// Functions the same as the injected parameters contexts for
|
|
|
|
/// helpers/partials with parameters but this has no need for storing
|
|
|
|
/// breadcrumbs since its simply storing two integers.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct IterationContext {
|
|
|
|
idx: OwnedLiteral,
|
|
|
|
len: OwnedLiteral,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IterationContext {
|
|
|
|
pub fn new(idx: u64, len: u64) -> Self {
|
|
|
|
IterationContext {
|
|
|
|
idx: OwnedLiteral::LPositiveInteger(idx),
|
|
|
|
len: OwnedLiteral::LPositiveInteger(len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContextElement for IterationContext {}
|
|
|
|
|
2020-05-24 16:16:43 -04:00
|
|
|
impl Truthiness for IterationContext {
|
|
|
|
fn is_truthy(&self) -> bool {
|
|
|
|
// 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 `{.}`.
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 16:01:56 -04:00
|
|
|
impl Renderable for IterationContext {
|
|
|
|
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
|
|
|
// 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 `{.}`.
|
|
|
|
Ok("[object Object]".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Loopable for IterationContext {
|
|
|
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Walkable for IterationContext {
|
|
|
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
|
|
|
match segment {
|
|
|
|
"$idx" => Ok(&self.idx),
|
|
|
|
"$len" => Ok(&self.len),
|
|
|
|
_ => Err(WalkError::CantWalk),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompareContextElement for IterationContext {
|
|
|
|
fn equals(&self, other: &dyn ContextElement) -> bool {
|
|
|
|
// TODO: Does this ever happen? perhaps I should have a panic here.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
|
|
|
// TODO: Does this ever happen? perhaps I should have a panic here.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|