Implemented partial_compare for scalar json values.

This commit is contained in:
Tom Alexander 2020-05-16 14:08:59 -04:00
parent 71e6da39ee
commit 2b5bec05be
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 46 additions and 1 deletions

View File

@ -166,7 +166,52 @@ impl CompareContextElement for serde_json::Value {
None => (),
Some(other_json_value) => {
return match (self, other_json_value) {
// TODO: Implement
(
serde_json::Value::Bool(self_boolean),
serde_json::Value::Bool(other_boolean),
) => self_boolean.partial_cmp(other_boolean),
(
serde_json::Value::Number(self_number),
serde_json::Value::Number(other_number),
) => match (
self_number.as_f64(),
other_number.as_f64(),
self_number.as_u64(),
other_number.as_u64(),
self_number.as_i64(),
other_number.as_i64(),
) {
(_, _, _, _, Some(self_int), Some(other_int)) => {
self_int.partial_cmp(&other_int)
}
(_, _, Some(self_uint), Some(other_uint), _, _) => {
self_uint.partial_cmp(&other_uint)
}
(_, _, Some(_self_uint), _, _, Some(_other_int)) => {
// If the previous matches did not catch
// it, then other must be negative and
// self must be larger than can be
// represented with an i64, therefore self
// is greater than other.
Some(Ordering::Greater)
}
(_, _, _, Some(_other_uint), Some(_self_int), _) => {
// If the previous matches did not catch
// it, then self must be negative and
// other must be larger than can be
// represented with an i64, therefore
// other is greater than self.
Some(Ordering::Less)
}
(Some(self_float), Some(other_float), _, _, _, _) => {
self_float.partial_cmp(&other_float)
}
_ => panic!("This should be impossible since u64 and i64 can both be converted to floats"),
},
(
serde_json::Value::String(self_string),
serde_json::Value::String(other_string),
) => self_string.partial_cmp(other_string),
_ => None,
};
}