Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Ranges so that they are easier to use #190

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions ion-schema/src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Constraint {
Constraint::Contains(ContainsConstraint::new(values.into()))
}

/// Creates a [Constraint::ContainerLength] from a [U64Range] specifying a length range.
/// Creates a [Constraint::ContainerLength] from a [UsizeRange] specifying a length range.
pub fn container_length(length: UsizeRange) -> Constraint {
Constraint::ContainerLength(ContainerLengthConstraint::new(length))
}
Expand Down Expand Up @@ -261,17 +261,14 @@ impl Constraint {

/// Creates a [Constraint::ValidValues] using the [Element]s specified inside it
/// Returns an IonSchemaError if any of the Elements have an annotation other than `range`
pub fn valid_values_with_values(
values: Vec<Element>,
pub fn valid_values(
valid_values: Vec<ValidValue>,
isl_version: IslVersion,
) -> IonSchemaResult<Constraint> {
let valid_values: IonSchemaResult<Vec<ValidValue>> = values
.iter()
.map(|e| ValidValue::from_ion_element(e, isl_version))
.collect();
Ok(Constraint::ValidValues(ValidValuesConstraint {
valid_values: valid_values?,
}))
Ok(Constraint::ValidValues(ValidValuesConstraint::new(
valid_values,
isl_version,
)?))
}

/// Creates a [Constraint::Regex] from the expression and flags (case_insensitive, multi_line) and also specify the ISL version
Expand Down Expand Up @@ -469,7 +466,7 @@ impl Constraint {
))
}
IslConstraintImpl::Precision(precision_range) => {
isl_require!(precision_range.lower() != &Limit::Closed(0u64) => "Precision range cannot be 0")?;
isl_require!(precision_range.lower() != &Limit::Inclusive(0) => "precision range must have non-zero values")?;
Ok(Constraint::Precision(PrecisionConstraint::new(
precision_range.to_owned(),
)))
Expand Down
19 changes: 9 additions & 10 deletions ion-schema/src/ion_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ use ion_rs::element::{Element, Value};
use ion_rs::external::bigdecimal::BigDecimal;
use ion_rs::{Decimal, Int};
use num_traits::ToPrimitive;
use std::str::FromStr;

/// Trait for adding extensions to [`Element`] that are useful for implementing Ion Schema.
pub(crate) trait ElementExtensions {
/// Returns the value as an `usize` if it is an Ion Int that can be represented as such.
/// Returns some `usize` if this `Element` is an Ion Int _and_ it can be represented as (fits in) a `usize`.
/// Returns `None` if `self` is not an Ion Int, or self is null.int, or self is out of bounds for `usize`.
fn as_usize(&self) -> Option<usize>;
/// Returns the value as an `u64` if it is an Ion Int that can be represented as such.
/// Returns some `u64` if this `Element` is an Ion Int _and_ it can be represented as (fits in) a `u64`.
/// Returns `None` if `self` is not an Ion Int, or self is null.int, or self is out of bounds for `u64`.
fn as_u64(&self) -> Option<u64>;
/// Returns the value as a [`Decimal`] if it is any numeric Ion value that can be represented as a [`Decimal`].
/// Returns some [`Decimal`] if this `Element` is any Ion number type (`int`, `decimal`, or `float`)
/// _and_ it can be represented as (fits in) a `Decimal`. Returns `None` if `self` is not one
/// of the Ion number types or not a finite value.
fn any_number_as_decimal(&self) -> Option<Decimal>;
}
impl ElementExtensions for Element {
Expand All @@ -30,14 +33,10 @@ impl ElementExtensions for Element {
}
fn any_number_as_decimal(&self) -> Option<Decimal> {
match self.value() {
// TODO: Consolidate Int match arms once https://github.com/amazon-ion/ion-rust/issues/582 is resolved
Value::Int(Int::I64(i)) => Some(Decimal::from(*i)),
Value::Int(Int::BigInt(i)) => Some(Decimal::from(BigDecimal::from(i.clone()))),
popematt marked this conversation as resolved.
Show resolved Hide resolved
Value::Float(f) => BigDecimal::from_str(&format!(
"{f:.PRECISION$e}",
PRECISION = f64::MANTISSA_DIGITS as usize
))
.ok()
.map(|it| it.into()),
Value::Float(f) => (*f).try_into().ok(),
Value::Decimal(d) => Some(d.clone()),
_ => None,
}
Expand Down
2 changes: 1 addition & 1 deletion ion-schema/src/isl/isl_type_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl IslVariablyOccurringTypeRef {
Ok(UsizeRange::new_single_value(1))
})?;

isl_require!(occurs.upper() != &Limit::Closed(0usize) => "Occurs cannot be 0")?;
isl_require!(occurs.upper() != &Limit::Inclusive(0usize) => "Occurs cannot be 0")?;
Ok(IslVariablyOccurringTypeRef { type_ref, occurs })
}

Expand Down
2 changes: 1 addition & 1 deletion ion-schema/src/isl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ mod isl_tests {
use ion_rs::Symbol;
use ion_rs::{IonType, TextWriterBuilder};
use rstest::*;
use std::path::{Path, MAIN_SEPARATOR};
use std::path::Path;
use test_generator::test_resources;

// helper function to create NamedIslType for isl tests using ISL 1.0
Expand Down
Loading