Implement partial_compare for OwnedLiteral.

This commit is contained in:
Tom Alexander 2020-05-16 22:08:03 -04:00
parent bf35fd0ae4
commit 16d8891452
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -204,8 +204,36 @@ impl CompareContextElement for OwnedLiteral {
} }
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> { fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// TODO println!("partial_compare literal {:?} | {:?}", self, other);
None // If its an OwnedLiteral 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_literal) => match (self, other_literal) {
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
self_text.partial_cmp(other_text)
}
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LString(other_text)) => {
self_num.to_string().partial_cmp(other_text)
}
(OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => {
self_text.partial_cmp(&other_num.to_string())
}
(
OwnedLiteral::LPositiveInteger(self_num),
OwnedLiteral::LPositiveInteger(other_num),
) => self_num.partial_cmp(other_num),
},
}
} }
} }