From c905e705ff5f5b8e9bc73b3321755f7455b3d8ea Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 22:30:04 -0400 Subject: [PATCH] Transition to new literals compiling. Tests still need work, as does the implementation for json. --- src/bin.rs | 5 +- src/parser/mod.rs | 1 + src/parser/parser.rs | 17 +++- src/renderer/parameters_context.rs | 121 +---------------------------- src/renderer/renderer.rs | 3 +- 5 files changed, 21 insertions(+), 126 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 9e3218c..720907f 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -2,6 +2,7 @@ extern crate nom; use crate::renderer::CompareContextElement; use parser::Filter; +use parser::OwnedLiteral; use renderer::compile_template; use renderer::CompiledTemplate; use renderer::ContextElement; @@ -169,13 +170,13 @@ impl CompareContextElement for serde_json::Value { } fn partial_compare(&self, other: &dyn ContextElement) -> Option { - println!("partial_compare json {:?} | {:?}", self, other); + // println!("partial_compare json {:?} | {:?}", self, other); // Handle type coerced objects // When doing a greater than or less than comparison, // javascript coerces objects into "[object Object]". if let serde_json::Value::Object(_) = self { - return "[object Object]".to_owned().partial_compare(other); + return OwnedLiteral::LString("[object Object]".to_owned()).partial_compare(other); } // Handle other serde_json::Value diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 202309e..58e38e3 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -7,6 +7,7 @@ pub use parser::Body; pub use parser::DustTag; pub use parser::Filter; pub use parser::KVPair; +pub use parser::OwnedLiteral; pub use parser::RValue; pub use parser::Special; pub use parser::Template; diff --git a/src/parser/parser.rs b/src/parser/parser.rs index a742fa3..613f2df 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -118,11 +118,16 @@ pub struct Partial<'a> { pub params: Vec>, } +#[derive(Clone, Debug, PartialEq)] +pub enum OwnedLiteral { + LString(String), + LPositiveInteger(u64), +} + #[derive(Clone, Debug, PartialEq)] pub enum RValue<'a> { RVPath(Path<'a>), - RVString(String), - RVPositiveInteger(u64), + RVLiteral(OwnedLiteral), } #[derive(Clone, Debug, PartialEq)] @@ -226,8 +231,12 @@ fn postitive_integer_literal(i: &str) -> IResult<&str, u64> { fn rvalue(i: &str) -> IResult<&str, RValue> { alt(( map(path, RValue::RVPath), - map(quoted_string, RValue::RVString), - map(postitive_integer_literal, RValue::RVPositiveInteger), + map(quoted_string, |s| { + RValue::RVLiteral(OwnedLiteral::LString(s)) + }), + map(postitive_integer_literal, |num| { + RValue::RVLiteral(OwnedLiteral::LPositiveInteger(num)) + }), ))(i) } diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index eba7537..052ec4a 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -1,5 +1,5 @@ use crate::parser::KVPair; -use crate::parser::{Filter, RValue}; +use crate::parser::{Filter, OwnedLiteral, RValue}; use crate::renderer::context_element::CompareContextElement; use crate::renderer::context_element::ContextElement; use crate::renderer::walking::owned_walk_path; @@ -35,25 +35,14 @@ pub struct OwnedPath { impl From<&RValue<'_>> for OwnedRValue { fn from(original: &RValue<'_>) -> Self { match original { - RValue::RVString(text) => { - OwnedRValue::RVLiteral(OwnedLiteral::LString(text.to_owned())) - } + RValue::RVLiteral(literal) => OwnedRValue::RVLiteral(literal.clone()), RValue::RVPath(path) => OwnedRValue::RVPath(OwnedPath { keys: path.keys.iter().map(|k| k.to_string()).collect(), }), - RValue::RVPositiveInteger(num) => { - OwnedRValue::RVLiteral(OwnedLiteral::LPositiveInteger(num.clone())) - } } } } -#[derive(Clone, Debug)] -pub enum OwnedLiteral { - LString(String), - LPositiveInteger(u64), -} - #[derive(Debug)] pub struct ParametersContext { params: HashMap, @@ -196,7 +185,7 @@ impl CompareContextElement for OwnedLiteral { } fn partial_compare(&self, other: &dyn ContextElement) -> Option { - println!("partial_compare literal {:?} | {:?}", self, other); + // 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 @@ -228,107 +217,3 @@ impl CompareContextElement for OwnedLiteral { } } } - -// impl ContextElement for String {} - -// impl Renderable for String { -// fn render(&self, _filters: &Vec) -> Result { -// Ok(self.clone()) -// } -// } - -// impl Loopable for String { -// fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { -// if self.is_empty() { -// Vec::new() -// } else { -// vec![self] -// } -// } -// } - -// impl Walkable for String { -// fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> { -// Err(WalkError::CantWalk) -// } -// } - -// impl CompareContextElement for String { -// fn equals(&self, other: &dyn ContextElement) -> bool { -// // If its a String 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::() { -// None => other.equals(self), -// Some(other_string) => self == other_string, -// } -// } - -// fn partial_compare(&self, other: &dyn ContextElement) -> Option { -// println!("partial_compare String {:?} | {:?}", self, other); -// // If its a string 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::() { -// 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_string) => self.partial_cmp(other_string), -// } -// } -// } - -// impl ContextElement for u64 {} - -// impl Renderable for u64 { -// fn render(&self, _filters: &Vec) -> Result { -// Ok(self.to_string()) -// } -// } - -// impl Loopable for u64 { -// fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { -// vec![self] -// } -// } - -// impl Walkable for u64 { -// fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> { -// Err(WalkError::CantWalk) -// } -// } - -// impl CompareContextElement for u64 { -// fn equals(&self, other: &dyn ContextElement) -> bool { -// // If its a u64 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::() { -// None => other.equals(self), -// Some(other_num) => self == other_num, -// } -// } - -// fn partial_compare(&self, other: &dyn ContextElement) -> Option { -// println!("partial_compare u64 {:?} | {:?}", self, other); -// // If its a u64 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::() { -// 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_num) => self.partial_cmp(other_num), -// } -// } -// } diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 5ba55d4..f3862cd 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -477,9 +477,8 @@ impl<'a> DustRenderer<'a> { match param_map.get(key) { None => None, Some(rval) => match rval { - RValue::RVString(text) => Some(Ok(text)), + RValue::RVLiteral(literal) => Some(Ok(literal)), RValue::RVPath(path) => Some(walk_path(breadcrumbs, &path.keys)), - RValue::RVPositiveInteger(num) => Some(Ok(num)), }, } }