From 596611c03a3a28b7e4c038f46c1d6d2ac2384d91 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 23:07:05 -0400 Subject: [PATCH] Mostly implemented the new comparison logic for json with new literals. --- src/bin.rs | 69 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 720907f..54c85b2 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -235,18 +235,24 @@ impl CompareContextElement for serde_json::Value { }; } } - // Handle string literals - match other.to_any().downcast_ref::() { + // Handle literals + match other.to_any().downcast_ref::() { None => (), - Some(other_string) => match self { - serde_json::Value::String(self_string) => { + Some(other_literal) => match (self, other_literal) { + (serde_json::Value::String(self_string), OwnedLiteral::LString(other_string)) => { return self_string.partial_cmp(other_string) } - serde_json::Value::Number(self_number) => { - match ( - self_number.as_u64(), - self_number.as_i64(), - self_number.as_f64(), + ( + serde_json::Value::String(self_string), + OwnedLiteral::LPositiveInteger(other_num), + ) => return self_string.partial_cmp(&other_num.to_string()), + ( + serde_json::Value::Number(self_num), + OwnedLiteral::LString(other_string), + ) => match ( + self_num.as_u64(), + self_num.as_i64(), + self_num.as_f64(), ) { (Some(self_uint), _, _) => { return self_uint.to_string().partial_cmp(other_string) @@ -259,15 +265,48 @@ impl CompareContextElement for serde_json::Value { } (None, None, None) => panic!("This should be impossible since u64 and i64 can both be converted to floats") } + ( + serde_json::Value::Number(self_num), + OwnedLiteral::LPositiveInteger(other_num), + ) => match ( + self_num.as_u64(), + self_num.as_i64(), + self_num.as_f64(), + ) { + (Some(self_uint), _, _) => { + return self_uint.partial_cmp(other_num) + } + (_, Some(self_int), _) => { + return if other_num <= &(i64::max_value() as u64) { + self_int.partial_cmp(&(*other_num as i64)) + } else { + Some(Ordering::Less) + } + } + (_, _, Some(self_float)) => { + // TODO: How does javascript compare ints and floats? I'm just going to assume a cast to a string for now. + return self_float.to_string().partial_cmp(&other_num.to_string()) + } + (None, None, None) => panic!("This should be impossible since u64 and i64 can both be converted to floats") + } + (serde_json::Value::Array(_), _) => { + // TODO + todo!() + } + (serde_json::Value::Object(_), _) => { + // TODO + todo!() + } + (serde_json::Value::Bool(_), _) => { + // TODO + todo!() + } + (serde_json::Value::Null, _) => { + // TODO + todo!() } - _ => (), }, } - // Handle numeric literals - match other.to_any().downcast_ref::() { - None => (), - Some(other_num) => return self.as_u64().map_or(None, |n| n.partial_cmp(other_num)), - } None } }