duster/src/renderer/parameters_context.rs

161 lines
5.7 KiB
Rust
Raw Normal View History

use crate::parser::Filter;
use crate::parser::KVPair;
use crate::parser::OwnedLiteral;
use crate::parser::RValue;
use crate::renderer::breadcrumb_tree::BreadcrumbTree;
use crate::renderer::context_element::CompareContextElement;
use crate::renderer::context_element::ContextElement;
2020-05-31 23:47:20 -04:00
use crate::renderer::context_element::IceResult;
use crate::renderer::context_element::IntoContextElement;
use crate::renderer::tree_walking::walk_path;
use crate::renderer::DustRenderer;
use crate::renderer::Loopable;
use crate::renderer::RenderError;
use crate::renderer::Renderable;
use crate::renderer::Truthiness;
use crate::renderer::WalkError;
use crate::renderer::Walkable;
use std::cmp::Ordering;
use std::collections::HashMap;
2020-05-10 18:10:17 -04:00
2020-05-10 21:28:47 -04:00
#[derive(Debug)]
pub struct ParametersContext<'a> {
params: HashMap<&'a str, &'a dyn IntoContextElement>,
2020-05-10 18:19:06 -04:00
}
impl<'a> ParametersContext<'a> {
pub fn new(
renderer: &DustRenderer,
breadcrumbs: Option<&BreadcrumbTree>,
params: &Vec<KVPair>,
) -> Self {
todo!()
}
}
impl<'a> IntoContextElement for RValue<'a> {
fn into_context_element<'b>(
&'b self,
renderer: &DustRenderer,
breadcrumbs: Option<&'b BreadcrumbTree<'b>>,
2020-05-31 23:47:20 -04:00
) -> Option<IceResult<'b>> {
todo!()
// match self {
// RValue::RVLiteral(owned_literal) => Some(owned_literal),
// RValue::RVPath(path) => walk_path(breadcrumbs, &path.keys)
// .map(|ice| ice.into_context_element(renderer, breadcrumbs))
// .ok()
// .flatten(),
// RValue::RVTemplate(template) => {
// // TODO
// // renderer
// // .render_partial_name(template, breadcrumbs)
// // .map(|rendered| OwnedLiteral::LString(rendered))
// // .ok()
// // .as_ref()
// // .map(|l| l as _)
// todo!()
// }
// }
}
}
impl<'a> Walkable for RValue<'a> {
fn walk(&self, segment: &str) -> Result<&dyn IntoContextElement, WalkError> {
Err(WalkError::CantWalk)
}
}
impl ContextElement for OwnedLiteral {}
impl Truthiness for OwnedLiteral {
fn is_truthy(&self) -> bool {
match self {
OwnedLiteral::LString(text) => !text.is_empty(),
OwnedLiteral::LPositiveInteger(num) => true,
}
}
}
impl Renderable for OwnedLiteral {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
match self {
OwnedLiteral::LString(text) => Ok(text.clone()),
OwnedLiteral::LPositiveInteger(num) => Ok(num.to_string()),
}
}
}
impl Loopable for OwnedLiteral {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
Vec::new()
}
}
impl Walkable for OwnedLiteral {
fn walk(&self, segment: &str) -> Result<&dyn IntoContextElement, WalkError> {
Err(WalkError::CantWalk)
}
}
impl CompareContextElement for OwnedLiteral {
fn equals(&self, other: &dyn ContextElement) -> bool {
// println!("equals literal {:?} | {:?}", self, other);
// If its an OwnedLiteral then compare them directly,
// otherwise defer to the other type's implementation of
// CompareContextElement since the end user could add any
// type.
match other.to_any().downcast_ref::<Self>() {
None => other.equals(self),
Some(other_literal) => match (self, other_literal) {
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
self_text == other_text
}
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LString(other_text)) => {
&self_num.to_string() == other_text
}
(OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => {
self_text == &other_num.to_string()
}
(
OwnedLiteral::LPositiveInteger(self_num),
OwnedLiteral::LPositiveInteger(other_num),
) => self_num == other_num,
},
}
}
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// println!("partial_compare literal {:?} | {:?}", self, other);
// If its an OwnedLiteral then compare them directly,
// otherwise defer to the other type's implementation of
// CompareContextElement since the end user could add any
// type.
match other.to_any().downcast_ref::<Self>() {
None => match other.partial_compare(self) {
None => None,
Some(ord) => match ord {
Ordering::Equal => Some(Ordering::Equal),
Ordering::Greater => Some(Ordering::Less),
Ordering::Less => Some(Ordering::Greater),
},
},
Some(other_literal) => match (self, other_literal) {
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
self_text.partial_cmp(other_text)
}
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LString(other_text)) => {
self_num.to_string().partial_cmp(other_text)
}
(OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => {
self_text.partial_cmp(&other_num.to_string())
}
(
OwnedLiteral::LPositiveInteger(self_num),
OwnedLiteral::LPositiveInteger(other_num),
) => self_num.partial_cmp(other_num),
},
}
}
}