-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
187e539
Refactor Ranges so that they are easier to use
popematt 5758df7
Removes errant TODO
popematt 00c194e
PR feedback changes
popematt b1e2b99
Update ion-schema/src/constraint.rs
popematt a4c5274
Minor cleanup
popematt 87a1f14
Adds rustdoc for Limit variants
popematt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use ion_rs::element::{Element, Value}; | ||
use ion_rs::external::bigdecimal::BigDecimal; | ||
use ion_rs::{Decimal, Int}; | ||
use num_traits::ToPrimitive; | ||
|
||
/// Trait for adding extensions to [`Element`] that are useful for implementing Ion Schema. | ||
pub(crate) trait ElementExtensions { | ||
/// 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 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 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 { | ||
fn as_usize(&self) -> Option<usize> { | ||
match self.value() { | ||
Value::Int(Int::I64(i)) => i.to_usize(), | ||
Value::Int(Int::BigInt(i)) => i.to_usize(), | ||
_ => None, | ||
} | ||
} | ||
fn as_u64(&self) -> Option<u64> { | ||
match self.value() { | ||
Value::Int(Int::I64(i)) => i.to_u64(), | ||
Value::Int(Int::BigInt(i)) => i.to_u64(), | ||
_ => None, | ||
} | ||
} | ||
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) => (*f).try_into().ok(), | ||
Value::Decimal(d) => Some(d.clone()), | ||
_ => None, | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking concern: for these conversions, note that it will not be possible to tell the difference between "The
Element
was not an integer" and "TheElement
was an integer that couldn't fit in ausize
."Something I plan to eventually add to
Element
is a series ofexpect_TYPE() -> IonResult<T>
methods so the caller can receive a more detailed error if things go wrong. It also makes it easy to propagate errors with?
where needed.Also: the next release of
ion-rs
makesInt
andUInt
into opaque types withFrom
andTryFrom
implementations for the various integer types. Nothing to do for the moment though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that scenario is unlikely to be a problem—it will only occur if people are using numbers that are too large for
u64
andusize
as arguments to constraints in their ISL. We can revisit it once theexpect_TYPE()
functions are available. For now, I'll make the documentation a little more explicit.