First stab at fixing the error, not a full success.
This commit is contained in:
50
src/bin.rs
50
src/bin.rs
@@ -169,6 +169,14 @@ impl CompareContextElement for serde_json::Value {
|
||||
}
|
||||
|
||||
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Handle other serde_json::Value
|
||||
match other.to_any().downcast_ref::<Self>() {
|
||||
None => (),
|
||||
@@ -228,11 +236,30 @@ impl CompareContextElement for serde_json::Value {
|
||||
// Handle string literals
|
||||
match other.to_any().downcast_ref::<String>() {
|
||||
None => (),
|
||||
Some(other_string) => {
|
||||
return self
|
||||
.as_str()
|
||||
.map_or(None, |s| s.partial_cmp(other_string.as_str()))
|
||||
}
|
||||
Some(other_string) => match self {
|
||||
serde_json::Value::String(self_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(),
|
||||
) {
|
||||
(Some(self_uint), _, _) => {
|
||||
return self_uint.to_string().partial_cmp(other_string)
|
||||
}
|
||||
(_, Some(self_int), _) => {
|
||||
return self_int.to_string().partial_cmp(other_string)
|
||||
}
|
||||
(_, _, Some(self_float)) => {
|
||||
return self_float.to_string().partial_cmp(other_string)
|
||||
}
|
||||
(None, None, None) => panic!("This should be impossible since u64 and i64 can both be converted to floats")
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
}
|
||||
// Handle numeric literals
|
||||
match other.to_any().downcast_ref::<u64>() {
|
||||
@@ -256,3 +283,16 @@ impl CompareContextElement for serde_json::Value {
|
||||
fn convert_vec_to_context_element(array: &Vec<serde_json::Value>) -> Vec<&dyn ContextElement> {
|
||||
array.iter().map(|v| v as _).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_type_coercion_object_int() {
|
||||
let x: serde_json::Value =
|
||||
serde_json::from_str(r#"{"name": "cat"}"#).expect("Failed to parse json");
|
||||
let y: u64 = 7;
|
||||
assert_eq!(x.partial_compare(&y), Some(Ordering::Greater));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,6 +520,11 @@ mod tests {
|
||||
fn equals(&self, other: &dyn ContextElement) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
||||
// TODO: Implement
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user