Transition to new literals compiling.

Tests still need work, as does the implementation for json.
This commit is contained in:
Tom Alexander 2020-05-16 22:30:04 -04:00
parent bc25c1ee16
commit c905e705ff
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 21 additions and 126 deletions

View File

@ -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<Ordering> {
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

View File

@ -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;

View File

@ -118,11 +118,16 @@ pub struct Partial<'a> {
pub params: Vec<KVPair<'a>>,
}
#[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)
}

View File

@ -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,23 +35,12 @@ 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)]
@ -196,7 +185,7 @@ impl CompareContextElement for OwnedLiteral {
}
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
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<Filter>) -> Result<String, RenderError> {
// 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::<Self>() {
// None => other.equals(self),
// Some(other_string) => self == other_string,
// }
// }
// fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// 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::<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_string) => self.partial_cmp(other_string),
// }
// }
// }
// impl ContextElement for u64 {}
// impl Renderable for u64 {
// fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// 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::<Self>() {
// None => other.equals(self),
// Some(other_num) => self == other_num,
// }
// }
// fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// 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::<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_num) => self.partial_cmp(other_num),
// }
// }
// }

View File

@ -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)),
},
}
}