XSD: Adds checked_neg operation

pull/509/head
Tpt 2 years ago committed by Thomas Tanon
parent 5bfbbdbd3f
commit 63945638ea
  1. 5
      lib/oxsdatatypes/src/date_time.rs
  2. 13
      lib/oxsdatatypes/src/decimal.rs
  3. 22
      lib/oxsdatatypes/src/duration.rs
  4. 8
      lib/oxsdatatypes/src/integer.rs
  5. 12
      lib/src/sparql/eval.rs

@ -188,7 +188,10 @@ impl DateTime {
self.checked_sub_day_time_duration(rhs)
} else {
Some(Self {
timestamp: Timestamp::new(&date_time_plus_duration(-rhs, &self.properties())?)
timestamp: Timestamp::new(&date_time_plus_duration(
rhs.checked_neg()?,
&self.properties(),
)?)
.ok()?,
})
}

@ -141,6 +141,14 @@ impl Decimal {
})
}
/// [op:numeric-unary-minus](https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus)
#[inline]
pub fn checked_neg(&self) -> Option<Self> {
Some(Self {
value: self.value.checked_neg()?,
})
}
/// [fn:abs](https://www.w3.org/TR/xpath-functions/#func-abs)
#[inline]
pub const fn abs(&self) -> Self {
@ -698,7 +706,10 @@ mod tests {
fn add() {
assert!(Decimal::MIN.checked_add(Decimal::STEP).is_some());
assert!(Decimal::MAX.checked_add(Decimal::STEP).is_none());
assert_eq!(Decimal::MAX.checked_add(Decimal::MIN), Some(-Decimal::STEP));
assert_eq!(
Decimal::MAX.checked_add(Decimal::MIN),
Decimal::STEP.checked_neg()
);
}
#[test]

@ -107,6 +107,14 @@ impl Duration {
})
}
#[inline]
pub fn checked_neg(&self) -> Option<Self> {
Some(Self {
year_month: self.year_month.checked_neg()?,
day_time: self.day_time.checked_neg()?,
})
}
/// Checks if the two values are [identical](https://www.w3.org/TR/xmlschema11-2/#identity).
#[inline]
pub fn is_identical_with(&self, other: &Self) -> bool {
@ -301,6 +309,13 @@ impl YearMonthDuration {
})
}
#[inline]
pub fn checked_neg(&self) -> Option<Self> {
Some(Self {
months: self.months.checked_neg()?,
})
}
/// Checks if the two values are [identical](https://www.w3.org/TR/xmlschema11-2/#identity).
#[inline]
pub fn is_identical_with(&self, other: &Self) -> bool {
@ -467,6 +482,13 @@ impl DayTimeDuration {
})
}
#[inline]
pub fn checked_neg(&self) -> Option<Self> {
Some(Self {
seconds: self.seconds.checked_neg()?,
})
}
/// Checks if the two values are [identical](https://www.w3.org/TR/xmlschema11-2/#identity).
#[inline]
pub fn is_identical_with(&self, other: &Self) -> bool {

@ -73,6 +73,14 @@ impl Integer {
})
}
/// [op:numeric-unary-minus](https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus)
#[inline]
pub fn checked_neg(&self) -> Option<Self> {
Some(Self {
value: self.value.checked_neg()?,
})
}
/// [fn:abs](https://www.w3.org/TR/xpath-functions/#func-abs)
#[inline]
pub const fn abs(&self) -> Self {

@ -1189,11 +1189,13 @@ impl SimpleEvaluator {
Rc::new(move |tuple| match e(tuple)? {
EncodedTerm::FloatLiteral(value) => Some((-value).into()),
EncodedTerm::DoubleLiteral(value) => Some((-value).into()),
EncodedTerm::IntegerLiteral(value) => Some((-value).into()),
EncodedTerm::DecimalLiteral(value) => Some((-value).into()),
EncodedTerm::DurationLiteral(value) => Some((-value).into()),
EncodedTerm::YearMonthDurationLiteral(value) => Some((-value).into()),
EncodedTerm::DayTimeDurationLiteral(value) => Some((-value).into()),
EncodedTerm::IntegerLiteral(value) => Some(value.checked_neg()?.into()),
EncodedTerm::DecimalLiteral(value) => Some(value.checked_neg()?.into()),
EncodedTerm::DurationLiteral(value) => Some(value.checked_neg()?.into()),
EncodedTerm::YearMonthDurationLiteral(value) => {
Some(value.checked_neg()?.into())
}
EncodedTerm::DayTimeDurationLiteral(value) => Some(value.checked_neg()?.into()),
_ => None,
})
}

Loading…
Cancel
Save