Mostly implemented the new comparison logic for json with new literals.

This commit is contained in:
Tom Alexander 2020-05-16 23:07:05 -04:00
parent 189dfb1755
commit 596611c03a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 54 additions and 15 deletions

View File

@ -235,18 +235,24 @@ impl CompareContextElement for serde_json::Value {
};
}
}
// Handle string literals
match other.to_any().downcast_ref::<String>() {
// Handle literals
match other.to_any().downcast_ref::<OwnedLiteral>() {
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::<u64>() {
None => (),
Some(other_num) => return self.as_u64().map_or(None, |n| n.partial_cmp(other_num)),
}
None
}
}