Skip to content

Commit

Permalink
adds changes for ranges int_value()
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd committed Sep 26, 2023
1 parent 1ac7224 commit b1c5861
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ion-schema/src/isl/isl_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ impl IslConstraintImpl {
));
}

if value.annotations().contains("range") {
if !value.annotations().is_empty() {
return invalid_schema_error("contains list can not have any annotations");
}

Expand Down
27 changes: 2 additions & 25 deletions ion-schema/src/isl/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,13 @@ impl RangeValidation<Timestamp> for TimestampRange {}
pub type TimestampPrecisionRange = base::Range<TimestampPrecision>;
impl RangeValidation<TimestampPrecision> for TimestampPrecisionRange {
fn is_empty(start: &Limit<TimestampPrecision>, end: &Limit<TimestampPrecision>) -> bool {
use crate::isl::util::TimestampPrecision::*;

match (start, end) {
(Limit::Inclusive(lower), Limit::Inclusive(upper)) => lower > upper,
(Limit::Exclusive(lower), Limit::Inclusive(upper))
| (Limit::Inclusive(lower), Limit::Exclusive(upper)) => lower >= upper,
(Limit::Exclusive(lower), Limit::Exclusive(upper)) => {
let start_value = match lower {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};

let end_value = match upper {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};
let start_value = lower.int_value();
let end_value = upper.int_value();

// Checking for e.g. range::[exclusive::1, exclusive::2] which is empty.
let adjusted_lower = start_value + 1;
Expand Down
41 changes: 17 additions & 24 deletions ion-schema/src/isl/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ impl TimestampPrecision {
TimestampPrecision::OtherFractionalSeconds(i) => format!("fractional second (10e{i})"),
}
}

pub(crate) fn int_value(&self) -> i64 {
use TimestampPrecision::*;
match self {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
}
}
}

impl TryFrom<&str> for TimestampPrecision {
Expand All @@ -145,30 +160,8 @@ impl TryFrom<&str> for TimestampPrecision {

impl PartialOrd for TimestampPrecision {
fn partial_cmp(&self, other: &TimestampPrecision) -> Option<Ordering> {
use TimestampPrecision::*;
let self_value = match self {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};

let other_value = match other {
Year => -4,
Month => -3,
Day => -2,
Minute => -1,
Second => 0,
Millisecond => 3,
Microsecond => 6,
Nanosecond => 9,
OtherFractionalSeconds(scale) => *scale,
};
let self_value = self.int_value();
let other_value = other.int_value();

Some(self_value.cmp(&other_value))
}
Expand Down

0 comments on commit b1c5861

Please sign in to comment.