Rename JsonNumber to ComparisonNumber.

This commit is contained in:
Tom Alexander 2020-06-13 19:15:51 -04:00
parent d6ad7c28f3
commit 241f6c04e4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -419,25 +419,25 @@ impl CompareContextElement for serde_json::Value {
}
#[derive(Debug)]
enum JsonNumber {
enum ComparisonNumber {
UnsignedInteger(u64),
SignedInteger(i64),
Decimal(f64),
Failure,
}
impl From<&serde_json::Value> for JsonNumber {
impl From<&serde_json::Value> for ComparisonNumber {
/// Convert from a JSON value to a number for comparison based on
/// the logic described at
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than
fn from(original: &serde_json::Value) -> Self {
match original {
serde_json::Value::Null => JsonNumber::UnsignedInteger(0),
serde_json::Value::Null => ComparisonNumber::UnsignedInteger(0),
serde_json::Value::Bool(boolean) => {
if *boolean {
JsonNumber::UnsignedInteger(1)
ComparisonNumber::UnsignedInteger(1)
} else {
JsonNumber::UnsignedInteger(0)
ComparisonNumber::UnsignedInteger(0)
}
}
serde_json::Value::Number(num) => num.into(),
@ -452,53 +452,53 @@ impl From<&serde_json::Value> for JsonNumber {
}
}
impl From<&String> for JsonNumber {
impl From<&String> for ComparisonNumber {
fn from(original: &String) -> Self {
match original.parse::<u64>() {
Ok(num) => return JsonNumber::UnsignedInteger(num),
Ok(num) => return ComparisonNumber::UnsignedInteger(num),
Err(_) => (),
};
match original.parse::<i64>() {
Ok(num) => return JsonNumber::SignedInteger(num),
Ok(num) => return ComparisonNumber::SignedInteger(num),
Err(_) => (),
};
match original.parse::<f64>() {
Ok(num) => return JsonNumber::Decimal(num),
Ok(num) => return ComparisonNumber::Decimal(num),
Err(_) => (),
};
JsonNumber::Failure
ComparisonNumber::Failure
}
}
impl From<&serde_json::Number> for JsonNumber {
impl From<&serde_json::Number> for ComparisonNumber {
fn from(original: &serde_json::Number) -> Self {
match original.as_u64() {
Some(num) => return JsonNumber::UnsignedInteger(num),
Some(num) => return ComparisonNumber::UnsignedInteger(num),
None => (),
};
match original.as_i64() {
Some(num) => return JsonNumber::SignedInteger(num),
Some(num) => return ComparisonNumber::SignedInteger(num),
None => (),
};
match original.as_f64() {
Some(num) => return JsonNumber::Decimal(num),
Some(num) => return ComparisonNumber::Decimal(num),
None => (),
};
JsonNumber::Failure
ComparisonNumber::Failure
}
}
impl From<&OwnedLiteral> for JsonNumber {
impl From<&OwnedLiteral> for ComparisonNumber {
fn from(original: &OwnedLiteral) -> Self {
match original {
OwnedLiteral::LPositiveInteger(num) => JsonNumber::UnsignedInteger(*num),
OwnedLiteral::LNegativeInteger(num) => JsonNumber::SignedInteger(*num),
OwnedLiteral::LPositiveInteger(num) => ComparisonNumber::UnsignedInteger(*num),
OwnedLiteral::LNegativeInteger(num) => ComparisonNumber::SignedInteger(*num),
OwnedLiteral::LString(text) => text.into(),
OwnedLiteral::LFloat(num) => {
if num.is_nan() {
JsonNumber::Failure
ComparisonNumber::Failure
} else {
JsonNumber::Decimal(*num)
ComparisonNumber::Decimal(*num)
}
}
}
@ -512,49 +512,56 @@ impl From<&OwnedLiteral> for JsonNumber {
/// strings
fn compare_json_numbers<S, O>(self_input: S, other_input: O) -> Option<Ordering>
where
S: Into<JsonNumber>,
O: Into<JsonNumber>,
S: Into<ComparisonNumber>,
O: Into<ComparisonNumber>,
{
let self_number: JsonNumber = self_input.into();
let other_number: JsonNumber = other_input.into();
let self_number: ComparisonNumber = self_input.into();
let other_number: ComparisonNumber = other_input.into();
match (self_number, other_number) {
(JsonNumber::Failure, _) => return None,
(_, JsonNumber::Failure) => return None,
(JsonNumber::UnsignedInteger(self_num), JsonNumber::UnsignedInteger(other_num)) => {
return self_num.partial_cmp(&other_num)
}
(JsonNumber::UnsignedInteger(self_num), JsonNumber::SignedInteger(other_num)) => {
(ComparisonNumber::Failure, _) => return None,
(_, ComparisonNumber::Failure) => return None,
(
ComparisonNumber::UnsignedInteger(self_num),
ComparisonNumber::UnsignedInteger(other_num),
) => return self_num.partial_cmp(&other_num),
(
ComparisonNumber::UnsignedInteger(self_num),
ComparisonNumber::SignedInteger(other_num),
) => {
if self_num < std::i64::MAX as u64 {
return (self_num as i64).partial_cmp(&other_num);
} else {
return Some(Ordering::Greater);
}
}
(JsonNumber::UnsignedInteger(self_num), JsonNumber::Decimal(other_num)) => {
(ComparisonNumber::UnsignedInteger(self_num), ComparisonNumber::Decimal(other_num)) => {
return (self_num as f64).partial_cmp(&other_num)
}
(JsonNumber::SignedInteger(self_num), JsonNumber::UnsignedInteger(other_num)) => {
(
ComparisonNumber::SignedInteger(self_num),
ComparisonNumber::UnsignedInteger(other_num),
) => {
if other_num < std::i64::MAX as u64 {
return self_num.partial_cmp(&(other_num as i64));
} else {
return Some(Ordering::Less);
}
}
(JsonNumber::SignedInteger(self_num), JsonNumber::SignedInteger(other_num)) => {
(ComparisonNumber::SignedInteger(self_num), ComparisonNumber::SignedInteger(other_num)) => {
return self_num.partial_cmp(&other_num)
}
(JsonNumber::SignedInteger(self_num), JsonNumber::Decimal(other_num)) => {
(ComparisonNumber::SignedInteger(self_num), ComparisonNumber::Decimal(other_num)) => {
return (self_num as f64).partial_cmp(&other_num)
}
(JsonNumber::Decimal(self_num), JsonNumber::UnsignedInteger(other_num)) => {
(ComparisonNumber::Decimal(self_num), ComparisonNumber::UnsignedInteger(other_num)) => {
return self_num.partial_cmp(&(other_num as f64))
}
(JsonNumber::Decimal(self_num), JsonNumber::SignedInteger(other_num)) => {
(ComparisonNumber::Decimal(self_num), ComparisonNumber::SignedInteger(other_num)) => {
return self_num.partial_cmp(&(other_num as f64))
}
(JsonNumber::Decimal(self_num), JsonNumber::Decimal(other_num)) => {
(ComparisonNumber::Decimal(self_num), ComparisonNumber::Decimal(other_num)) => {
return self_num.partial_cmp(&other_num)
}
}