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