Structure for ordering, need to implement for serde_json::Value.
This commit is contained in:
@@ -8,7 +8,7 @@ use crate::renderer::RenderError;
|
||||
use crate::renderer::Renderable;
|
||||
use crate::renderer::WalkError;
|
||||
use crate::renderer::Walkable;
|
||||
use std::collections::HashMap;
|
||||
use std::{cmp::Ordering, collections::HashMap};
|
||||
|
||||
/// Copy the data from an RValue to an Owned struct
|
||||
///
|
||||
@@ -122,6 +122,11 @@ impl CompareContextElement for ParametersContext {
|
||||
// TODO: Does this ever happen? perhaps I should have a panic here.
|
||||
false
|
||||
}
|
||||
|
||||
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
||||
// TODO: Does this ever happen? perhaps I should have a panic here.
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextElement for String {}
|
||||
@@ -158,6 +163,23 @@ impl CompareContextElement for String {
|
||||
Some(other_string) => self == other_string,
|
||||
}
|
||||
}
|
||||
|
||||
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
||||
// If its a string then compare them directly, otherwise defer
|
||||
// to the other type's implementation of CompareContextElement
|
||||
// since the end user could add any type.
|
||||
match other.to_any().downcast_ref::<Self>() {
|
||||
None => match other.partial_compare(self) {
|
||||
None => None,
|
||||
Some(ord) => match ord {
|
||||
Ordering::Equal => Some(Ordering::Equal),
|
||||
Ordering::Greater => Some(Ordering::Less),
|
||||
Ordering::Less => Some(Ordering::Greater),
|
||||
},
|
||||
},
|
||||
Some(other_string) => self.partial_cmp(other_string),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextElement for u64 {}
|
||||
@@ -190,4 +212,21 @@ impl CompareContextElement for u64 {
|
||||
Some(other_num) => self == other_num,
|
||||
}
|
||||
}
|
||||
|
||||
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
|
||||
// If its a u64 then compare them directly, otherwise defer
|
||||
// to the other type's implementation of CompareContextElement
|
||||
// since the end user could add any type.
|
||||
match other.to_any().downcast_ref::<Self>() {
|
||||
None => match other.partial_compare(self) {
|
||||
None => None,
|
||||
Some(ord) => match ord {
|
||||
Ordering::Equal => Some(Ordering::Equal),
|
||||
Ordering::Greater => Some(Ordering::Less),
|
||||
Ordering::Less => Some(Ordering::Greater),
|
||||
},
|
||||
},
|
||||
Some(other_num) => self.partial_cmp(other_num),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user