Move ComparisonNumber into the library and add an OwnedLiteral for booleans.

This commit is contained in:
Tom Alexander
2020-06-14 17:08:01 -04:00
parent 8ab315abfe
commit 83c86ff9b3
6 changed files with 148 additions and 169 deletions

View File

@@ -3,6 +3,8 @@ use crate::parser::KVPair;
use crate::parser::OwnedLiteral;
use crate::parser::RValue;
use crate::renderer::breadcrumb_tree::BreadcrumbTreeElement;
use crate::renderer::comparison_number::compare_json_numbers;
use crate::renderer::comparison_number::ComparisonNumber;
use crate::renderer::context_element::CompareContextElement;
use crate::renderer::context_element::ContextElement;
use crate::renderer::context_element::IceResult;
@@ -160,6 +162,7 @@ impl ContextElement for OwnedLiteral {}
impl Truthiness for OwnedLiteral {
fn is_truthy(&self) -> bool {
match self {
OwnedLiteral::LBoolean(boolean) => *boolean,
OwnedLiteral::LString(text) => !text.is_empty(),
OwnedLiteral::LPositiveInteger(_num) => true,
OwnedLiteral::LNegativeInteger(_num) => true,
@@ -171,6 +174,7 @@ impl Truthiness for OwnedLiteral {
impl Renderable for OwnedLiteral {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
match self {
OwnedLiteral::LBoolean(boolean) => Ok(boolean.to_string()),
OwnedLiteral::LString(text) => Ok(text.clone()),
OwnedLiteral::LPositiveInteger(num) => Ok(num.to_string()),
OwnedLiteral::LNegativeInteger(num) => Ok(num.to_string()),
@@ -194,6 +198,9 @@ impl Walkable for OwnedLiteral {
impl Sizable for OwnedLiteral {
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
match self {
OwnedLiteral::LBoolean(_) => {
Some(IceResult::from_owned(OwnedLiteral::LPositiveInteger(0)))
}
OwnedLiteral::LFloat(_) => Some(IceResult::from_borrowed(self)),
OwnedLiteral::LPositiveInteger(_) => Some(IceResult::from_borrowed(self)),
OwnedLiteral::LNegativeInteger(_) => Some(IceResult::from_borrowed(self)),
@@ -258,9 +265,11 @@ impl CompareContextElement for OwnedLiteral {
OwnedLiteral::LNegativeInteger(self_num),
OwnedLiteral::LNegativeInteger(other_num),
) => self_num == other_num,
(OwnedLiteral::LString(_self_text), _) | (_, OwnedLiteral::LString(_self_text)) => {
false
(OwnedLiteral::LBoolean(self_bool), OwnedLiteral::LBoolean(other_bool)) => {
self_bool == other_bool
}
(OwnedLiteral::LString(_), _) | (_, OwnedLiteral::LString(_)) => false,
(OwnedLiteral::LBoolean(_), _) | (_, OwnedLiteral::LBoolean(_)) => false,
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
self_num == other_num
}
@@ -315,70 +324,12 @@ impl CompareContextElement for OwnedLiteral {
},
},
Some(other_literal) => match (self, other_literal) {
// If they're both strings, compare them directly
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
self_text.partial_cmp(other_text)
}
(
OwnedLiteral::LPositiveInteger(self_num),
OwnedLiteral::LPositiveInteger(other_num),
) => self_num.partial_cmp(other_num),
(
OwnedLiteral::LNegativeInteger(self_num),
OwnedLiteral::LNegativeInteger(other_num),
) => self_num.partial_cmp(other_num),
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
self_num.partial_cmp(other_num)
}
(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::LNegativeInteger(self_num), OwnedLiteral::LString(other_text)) => {
self_num.to_string().partial_cmp(other_text)
}
(OwnedLiteral::LString(self_text), OwnedLiteral::LNegativeInteger(other_num)) => {
self_text.partial_cmp(&other_num.to_string())
}
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LString(other_text)) => {
self_num.to_string().partial_cmp(other_text)
}
(OwnedLiteral::LString(self_text), OwnedLiteral::LFloat(other_num)) => {
self_text.partial_cmp(&other_num.to_string())
}
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
(*self_num as f64).partial_cmp(other_num)
}
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LPositiveInteger(other_num)) => {
self_num.partial_cmp(&(*other_num as f64))
}
(OwnedLiteral::LNegativeInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
(*self_num as f64).partial_cmp(other_num)
}
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LNegativeInteger(other_num)) => {
self_num.partial_cmp(&(*other_num as f64))
}
(
OwnedLiteral::LPositiveInteger(self_num),
OwnedLiteral::LNegativeInteger(other_num),
) => {
if *self_num < std::i64::MAX as u64 {
(*self_num as i64).partial_cmp(other_num)
} else {
Some(Ordering::Greater)
}
}
(
OwnedLiteral::LNegativeInteger(self_num),
OwnedLiteral::LPositiveInteger(other_num),
) => {
if *other_num < std::i64::MAX as u64 {
self_num.partial_cmp(&(*other_num as i64))
} else {
Some(Ordering::Less)
}
}
// Otherwise, convert to numbers and compare them that way
(_, _) => return compare_json_numbers(self, other_literal),
},
}
}
@@ -481,3 +432,27 @@ impl CompareContextElement for OwnedLiteral {
.map(IceResult::from_owned)
}
}
impl From<&OwnedLiteral> for ComparisonNumber {
fn from(original: &OwnedLiteral) -> Self {
match original {
OwnedLiteral::LBoolean(boolean) => {
if *boolean {
ComparisonNumber::UnsignedInteger(1)
} else {
ComparisonNumber::UnsignedInteger(0)
}
}
OwnedLiteral::LPositiveInteger(num) => ComparisonNumber::UnsignedInteger(*num),
OwnedLiteral::LNegativeInteger(num) => ComparisonNumber::SignedInteger(*num),
OwnedLiteral::LString(text) => text.into(),
OwnedLiteral::LFloat(num) => {
if num.is_nan() {
ComparisonNumber::Failure
} else {
ComparisonNumber::Decimal(*num)
}
}
}
}
}