67 lines
1.9 KiB
Rust
67 lines
1.9 KiB
Rust
use crate::parser::Filter;
|
|
use crate::renderer::errors::RenderError;
|
|
use crate::renderer::errors::WalkError;
|
|
use std::any::Any;
|
|
use std::fmt::Debug;
|
|
|
|
pub trait ContextElement:
|
|
Debug + Walkable + Renderable + Loopable + CloneIntoBoxedContextElement + CompareContextElement
|
|
{
|
|
}
|
|
|
|
pub trait Walkable {
|
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError>;
|
|
}
|
|
|
|
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.
|
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
|
|
}
|
|
|
|
pub trait CompareContextElement {
|
|
fn to_any(&self) -> &dyn Any;
|
|
|
|
fn equals(&self, other: &dyn ContextElement) -> bool;
|
|
}
|
|
|
|
pub trait CloneIntoBoxedContextElement {
|
|
fn clone_to_box(&self) -> Box<dyn ContextElement>;
|
|
}
|
|
|
|
impl<C: 'static + ContextElement + Clone> CloneIntoBoxedContextElement for C {
|
|
fn clone_to_box(&self) -> Box<dyn ContextElement> {
|
|
Box::new(self.clone())
|
|
}
|
|
}
|
|
|
|
impl<C: 'static + ContextElement + PartialEq> CompareContextElement for C {
|
|
fn to_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
|
|
fn equals(&self, other: &dyn ContextElement) -> bool {
|
|
other
|
|
.to_any()
|
|
.downcast_ref::<Self>()
|
|
.map_or(false, |a| self == a)
|
|
}
|
|
}
|
|
|
|
impl<'a, 'b> PartialEq<&'b dyn ContextElement> for &'a dyn ContextElement {
|
|
fn eq(&self, other: &&'b dyn ContextElement) -> bool {
|
|
self.equals(*other)
|
|
}
|
|
}
|