diff --git a/src/bin.rs b/src/bin.rs index 1f698a9..4274501 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -220,7 +220,7 @@ impl CompareContextElement for serde_json::Value { serde_json::Value::String(self_string), serde_json::Value::String(other_string), ) => self_string.partial_cmp(other_string), - // TODO: Non-scalar types + (serde_json::Value::Array(self_array), serde_json::Value::Array(other_array)) => convert_vec_to_context_element(self_array).partial_cmp(&convert_vec_to_context_element(other_array)), _ => None, }; } @@ -242,3 +242,17 @@ impl CompareContextElement for serde_json::Value { None } } + +/// Create a new vec by of references to the serde_json::Values as +/// ContextElement trait objects so we can use its implementation of +/// PartialOrd. +/// +/// You cannot implement a trait you do not define for a type you do +/// not define, so I cannot implement PartialOrd for +/// serde_json::value. Instead, I just re-use the PartialOrd +/// implementation for ContextElement which unfortunately has extra +/// overhead of downcasting. This would be a good spot for +/// optimization. +fn convert_vec_to_context_element(array: &Vec) -> Vec<&dyn ContextElement> { + array.iter().map(|v| v as _).collect() +}