Add support for negative integers.
This commit is contained in:
parent
02bcefb75c
commit
d99aa44d8e
@ -335,6 +335,11 @@ impl CompareContextElement for serde_json::Value {
|
||||
return self
|
||||
.equals(&serde_json::Value::Number(other_json_num) as &dyn ContextElement);
|
||||
}
|
||||
Some(OwnedLiteral::LNegativeInteger(other_num)) => {
|
||||
let other_json_num: serde_json::Number = std::convert::From::from(*other_num);
|
||||
return self
|
||||
.equals(&serde_json::Value::Number(other_json_num) as &dyn ContextElement);
|
||||
}
|
||||
Some(OwnedLiteral::LFloat(other_num)) => {
|
||||
let other_json_num = serde_json::Number::from_f64(*other_num);
|
||||
match other_json_num {
|
||||
@ -463,6 +468,7 @@ impl From<&OwnedLiteral> for JsonNumber {
|
||||
fn from(original: &OwnedLiteral) -> Self {
|
||||
match original {
|
||||
OwnedLiteral::LPositiveInteger(num) => JsonNumber::UnsignedInteger(*num),
|
||||
OwnedLiteral::LNegativeInteger(num) => JsonNumber::SignedInteger(*num),
|
||||
OwnedLiteral::LString(text) => text.into(),
|
||||
OwnedLiteral::LFloat(num) => {
|
||||
if num.is_nan() {
|
||||
|
@ -119,6 +119,7 @@ pub struct Partial<'a> {
|
||||
pub enum OwnedLiteral {
|
||||
LString(String),
|
||||
LPositiveInteger(u64),
|
||||
LNegativeInteger(i64),
|
||||
LFloat(f64),
|
||||
}
|
||||
|
||||
@ -354,6 +355,34 @@ fn postitive_integer_literal(i: &str) -> IResult<&str, u64> {
|
||||
)(i)
|
||||
}
|
||||
|
||||
/// No decimals, just the sign and digits
|
||||
fn negative_integer_literal(i: &str) -> IResult<&str, i64> {
|
||||
map(
|
||||
verify(
|
||||
map(
|
||||
recognize(tuple((tag("-"), digit1))),
|
||||
|number_string: &str| number_string.parse::<i64>(),
|
||||
),
|
||||
|parse_result| parse_result.is_ok(),
|
||||
),
|
||||
|parsed_number| parsed_number.unwrap(),
|
||||
)(i)
|
||||
}
|
||||
|
||||
/// A non-scientific notation float (sign, digits, decimal, and more digits)
|
||||
fn float_literal(i: &str) -> IResult<&str, f64> {
|
||||
map(
|
||||
verify(
|
||||
map(
|
||||
recognize(tuple((opt(one_of("+-")), digit1, tag("."), digit1))),
|
||||
|number_string: &str| number_string.parse::<f64>(),
|
||||
),
|
||||
|parse_result| parse_result.is_ok(),
|
||||
),
|
||||
|parsed_number| parsed_number.unwrap(),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn template_string_rvalue(i: &str) -> IResult<&str, Vec<PartialNameElement>> {
|
||||
let (i, template_string) = verify(quoted_string, |s: &String| {
|
||||
partial_quoted_tag(s.as_str()).is_ok()
|
||||
@ -376,6 +405,12 @@ fn rvalue(i: &str) -> IResult<&str, RValue> {
|
||||
map(postitive_integer_literal, |num| {
|
||||
RValue::RVLiteral(OwnedLiteral::LPositiveInteger(num))
|
||||
}),
|
||||
map(negative_integer_literal, |num| {
|
||||
RValue::RVLiteral(OwnedLiteral::LNegativeInteger(num))
|
||||
}),
|
||||
map(float_literal, |num| {
|
||||
RValue::RVLiteral(OwnedLiteral::LFloat(num))
|
||||
}),
|
||||
))(i)
|
||||
}
|
||||
|
||||
|
@ -137,6 +137,7 @@ impl Truthiness for OwnedLiteral {
|
||||
match self {
|
||||
OwnedLiteral::LString(text) => !text.is_empty(),
|
||||
OwnedLiteral::LPositiveInteger(_num) => true,
|
||||
OwnedLiteral::LNegativeInteger(_num) => true,
|
||||
OwnedLiteral::LFloat(_num) => true,
|
||||
}
|
||||
}
|
||||
@ -147,6 +148,7 @@ impl Renderable for OwnedLiteral {
|
||||
match self {
|
||||
OwnedLiteral::LString(text) => Ok(text.clone()),
|
||||
OwnedLiteral::LPositiveInteger(num) => Ok(num.to_string()),
|
||||
OwnedLiteral::LNegativeInteger(num) => Ok(num.to_string()),
|
||||
OwnedLiteral::LFloat(num) => Ok(num.to_string()),
|
||||
}
|
||||
}
|
||||
@ -176,31 +178,67 @@ impl CompareContextElement for OwnedLiteral {
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
|
||||
self_text == other_text
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => self_num == other_num,
|
||||
(
|
||||
OwnedLiteral::LNegativeInteger(self_num),
|
||||
OwnedLiteral::LNegativeInteger(other_num),
|
||||
) => self_num == other_num,
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_num == other_num
|
||||
}
|
||||
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LString(other_text)) => {
|
||||
&self_num.to_string() == other_text
|
||||
}
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => {
|
||||
self_text == &other_num.to_string()
|
||||
}
|
||||
(OwnedLiteral::LNegativeInteger(self_num), OwnedLiteral::LString(other_text)) => {
|
||||
&self_num.to_string() == other_text
|
||||
}
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LNegativeInteger(other_num)) => {
|
||||
self_text == &other_num.to_string()
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LString(other_text)) => {
|
||||
&self_num.to_string() == other_text
|
||||
}
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_text == &other_num.to_string()
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => self_num == other_num,
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_num == other_num
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LPositiveInteger(other_num)) => {
|
||||
*self_num == (*other_num as f64)
|
||||
}
|
||||
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
(*self_num as f64) == *other_num
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LNegativeInteger(other_num)) => {
|
||||
*self_num == (*other_num as f64)
|
||||
}
|
||||
(OwnedLiteral::LNegativeInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
(*self_num as f64) == *other_num
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LNegativeInteger(other_num),
|
||||
) => {
|
||||
if *self_num < std::i64::MAX as u64 {
|
||||
(*self_num as i64) == *other_num
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LNegativeInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => {
|
||||
if *other_num < std::i64::MAX as u64 {
|
||||
*self_num == (*other_num as i64)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -223,31 +261,67 @@ impl CompareContextElement for OwnedLiteral {
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LString(other_text)) => {
|
||||
self_text.partial_cmp(other_text)
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => self_num.partial_cmp(other_num),
|
||||
(
|
||||
OwnedLiteral::LNegativeInteger(self_num),
|
||||
OwnedLiteral::LNegativeInteger(other_num),
|
||||
) => self_num.partial_cmp(other_num),
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_num.partial_cmp(other_num)
|
||||
}
|
||||
(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::LNegativeInteger(self_num), OwnedLiteral::LString(other_text)) => {
|
||||
self_num.to_string().partial_cmp(other_text)
|
||||
}
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LNegativeInteger(other_num)) => {
|
||||
self_text.partial_cmp(&other_num.to_string())
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LString(other_text)) => {
|
||||
self_num.to_string().partial_cmp(other_text)
|
||||
}
|
||||
(OwnedLiteral::LString(self_text), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_text.partial_cmp(&other_num.to_string())
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => self_num.partial_cmp(other_num),
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
self_num.partial_cmp(other_num)
|
||||
}
|
||||
(OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
(*self_num as f64).partial_cmp(other_num)
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LPositiveInteger(other_num)) => {
|
||||
self_num.partial_cmp(&(*other_num as f64))
|
||||
}
|
||||
(OwnedLiteral::LNegativeInteger(self_num), OwnedLiteral::LFloat(other_num)) => {
|
||||
(*self_num as f64).partial_cmp(other_num)
|
||||
}
|
||||
(OwnedLiteral::LFloat(self_num), OwnedLiteral::LNegativeInteger(other_num)) => {
|
||||
self_num.partial_cmp(&(*other_num as f64))
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LPositiveInteger(self_num),
|
||||
OwnedLiteral::LNegativeInteger(other_num),
|
||||
) => {
|
||||
if *self_num < std::i64::MAX as u64 {
|
||||
(*self_num as i64).partial_cmp(other_num)
|
||||
} else {
|
||||
Some(Ordering::Greater)
|
||||
}
|
||||
}
|
||||
(
|
||||
OwnedLiteral::LNegativeInteger(self_num),
|
||||
OwnedLiteral::LPositiveInteger(other_num),
|
||||
) => {
|
||||
if *other_num < std::i64::MAX as u64 {
|
||||
self_num.partial_cmp(&(*other_num as i64))
|
||||
} else {
|
||||
Some(Ordering::Less)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user