duster/src/renderer/parameters_context.rs

220 lines
7.9 KiB
Rust
Raw Normal View History

use crate::parser::KVPair;
use crate::parser::{Filter, OwnedLiteral, RValue};
2020-05-10 19:16:55 -04:00
use crate::renderer::context_element::CompareContextElement;
use crate::renderer::context_element::ContextElement;
use crate::renderer::walking::owned_walk_path;
2020-05-08 22:34:58 -04:00
use crate::renderer::Loopable;
use crate::renderer::RenderError;
use crate::renderer::Renderable;
2020-05-09 14:10:38 -04:00
use crate::renderer::WalkError;
2020-05-08 22:34:58 -04:00
use crate::renderer::Walkable;
use std::{cmp::Ordering, collections::HashMap};
2020-05-10 18:10:17 -04:00
/// Copy the data from an RValue to an Owned struct
///
/// In order to get comparisons to work for our `ContextElement` trait
/// objects, we need to be able to use `std::any::Any`. Unfortunately,
/// `Any` requires that the structs do not have a lifetime (so they
/// will have a `'static` lifetime. This means that we cannot have a
/// `<'a>` appended to the struct type, so the struct cannot contain
/// any borrows. Rather than impose the copy cost in the parser, we
/// are imposing the cost of copying the data in the renderer because
/// the parser has no reason to not be able to reference data from the
/// input string.
2020-05-16 22:19:51 -04:00
#[derive(Clone, Debug)]
2020-05-10 18:10:17 -04:00
pub enum OwnedRValue {
RVPath(OwnedPath),
2020-05-16 22:19:51 -04:00
RVLiteral(OwnedLiteral),
2020-05-10 18:10:17 -04:00
}
2020-05-10 21:28:47 -04:00
#[derive(Clone, Debug, PartialEq)]
2020-05-10 18:10:17 -04:00
pub struct OwnedPath {
pub keys: Vec<String>,
}
2020-05-10 18:19:06 -04:00
impl From<&RValue<'_>> for OwnedRValue {
fn from(original: &RValue<'_>) -> Self {
2020-05-10 18:10:17 -04:00
match original {
RValue::RVLiteral(literal) => OwnedRValue::RVLiteral(literal.clone()),
2020-05-10 18:10:17 -04:00
RValue::RVPath(path) => OwnedRValue::RVPath(OwnedPath {
keys: path.keys.iter().map(|k| k.to_string()).collect(),
}),
}
}
}
2020-05-10 21:28:47 -04:00
#[derive(Debug)]
pub struct ParametersContext {
2020-05-10 18:10:17 -04:00
params: HashMap<String, OwnedRValue>,
2020-05-10 18:19:06 -04:00
breadcrumbs: Vec<Box<dyn ContextElement>>,
}
impl ParametersContext {
pub fn new(breadcrumbs: &Vec<&dyn ContextElement>, params: &Vec<KVPair>) -> ParametersContext {
2020-05-10 18:19:06 -04:00
let owned_params: HashMap<String, OwnedRValue> = params
.iter()
.map(|kvpair| (kvpair.key.to_string(), OwnedRValue::from(&kvpair.value)))
.collect();
let owned_breadcrumbs: Vec<Box<dyn ContextElement>> =
breadcrumbs.iter().map(|ce| ce.clone_to_box()).collect();
2020-05-10 18:35:24 -04:00
ParametersContext {
2020-05-10 18:19:06 -04:00
params: owned_params,
breadcrumbs: owned_breadcrumbs,
2020-05-10 18:19:06 -04:00
}
}
2020-05-10 18:10:17 -04:00
}
impl ContextElement for ParametersContext {}
2020-05-10 21:28:47 -04:00
impl Renderable for ParametersContext {
2020-05-10 21:28:47 -04:00
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 ParametersContext {
2020-05-10 21:28:47 -04:00
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 ParametersContext {
2020-05-10 21:28:47 -04:00
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
let rval = self.params.get(segment).ok_or(WalkError::CantWalk)?;
match rval {
OwnedRValue::RVPath(path) => owned_walk_path(&self.breadcrumbs, &path.keys),
2020-05-16 22:19:51 -04:00
OwnedRValue::RVLiteral(literal) => Ok(literal),
2020-05-10 21:28:47 -04:00
}
}
}
impl Clone for ParametersContext {
2020-05-10 21:28:47 -04:00
fn clone(&self) -> Self {
let new_params: HashMap<String, OwnedRValue> = self
.params
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
let new_breadcrumbs: Vec<Box<dyn ContextElement>> = self
.breadcrumbs
.iter()
.map(|bread| bread.clone_to_box())
.collect();
ParametersContext {
params: new_params,
breadcrumbs: new_breadcrumbs,
}
2020-05-10 21:28:47 -04:00
}
}
impl CompareContextElement for ParametersContext {
2020-05-10 21:28:47 -04:00
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
}
2020-05-10 21:28:47 -04:00
}
impl ContextElement for OwnedLiteral {}
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> {
match self {
OwnedLiteral::LString(text) => {
if text.is_empty() {
Vec::new()
} else {
2020-05-16 22:19:51 -04:00
vec![self]
}
}
2020-05-16 22:19:51 -04:00
OwnedLiteral::LPositiveInteger(num) => vec![self],
}
}
}
impl Walkable for OwnedLiteral {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
Err(WalkError::CantWalk)
}
}
impl CompareContextElement for OwnedLiteral {
fn equals(&self, other: &dyn ContextElement) -> bool {
2020-05-16 22:02:58 -04:00
// 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),
},
}
}
}