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

Module cleanup, adds Element::expect_*, removes IntAccess #595

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/read_all_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ fn main() {

#[cfg(feature = "experimental-reader")]
mod example {
use ion_rs::IonResult;
use ion_rs::RawBinaryReader;
use ion_rs::RawStreamItem;
use ion_rs::{BlockingRawBinaryReader, IonReader, IonType, RawReader, StreamItem, UserReader};
use ion_rs::{
BlockingRawBinaryReader, IonReader, IonResult, IonType, RawBinaryReader, RawReader,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we're exporting "raw" readers at the root?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't really thought through how I'd like to expose all of the types that are behind experimental feature flags yet. Open to suggestions.

RawStreamItem, StreamItem, UserReader,
};
use memmap::MmapOptions;
use std::fs::File;
use std::process::exit;
Expand Down
2 changes: 1 addition & 1 deletion src/binary/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::constants::v1_0::system_symbol_ids;
use crate::ion_writer::IonWriter;
use crate::raw_symbol_token_ref::{AsRawSymbolTokenRef, RawSymbolTokenRef};
use crate::result::{IonFailure, IonResult};
use crate::types::{Decimal, Int, IonType, SymbolId, Timestamp};
use crate::SymbolTable;
use crate::{Decimal, Int, IonType, SymbolId, Timestamp};
use delegate::delegate;
use std::io::Write;

Expand Down
2 changes: 1 addition & 1 deletion src/binary/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::binary::var_uint::VarUInt;
use crate::ion_data::IonEq;
use crate::result::{IonFailure, IonResult};
use crate::types::integer::UIntData;
use crate::types::{Coefficient, Decimal, Sign, UInt};
use crate::IonError;
use crate::{Coefficient, Decimal, Sign, UInt};

const DECIMAL_BUFFER_SIZE: usize = 32;
const DECIMAL_POSITIVE_ZERO: Decimal = Decimal {
Expand Down
2 changes: 1 addition & 1 deletion src/binary/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::binary::constants::v1_0::length_codes;
use crate::binary::nibbles::nibbles_from_byte;
use crate::binary::IonTypeCode;
use crate::result::IonResult;
use crate::types::IonType;
use crate::IonType;

/// Contains all of the information that can be extracted from the one-octet type descriptor
/// found at the beginning of each value in a binary Ion stream.
Expand Down
9 changes: 4 additions & 5 deletions src/binary/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::mem;

use crate::result::IonResult;
use crate::types;
use crate::types::integer::Int;
use crate::types::Coefficient;
use crate::{Coefficient, Int};
use num_traits::Zero;
use std::io::Write;

Expand Down Expand Up @@ -102,9 +101,9 @@ impl DecodedInt {
}

impl From<DecodedInt> for Int {
/// Note that if the DecodedInt represents -0, converting it to an Integer will result in a 0.
/// If negative zero is significant to your use case, check it using [DecodedInt::is_negative_zero]
/// before converting it to an Integer.
// Note that if the DecodedInt represents -0, converting it to an Integer will result in a 0.
// If negative zero is significant to your use case, check it using [DecodedInt::is_negative_zero]
// before converting it to an Integer.
fn from(uint: DecodedInt) -> Self {
let DecodedInt {
value,
Expand Down
3 changes: 1 addition & 2 deletions src/binary/non_blocking/binary_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::binary::uint::DecodedUInt;
use crate::binary::var_int::VarInt;
use crate::binary::var_uint::VarUInt;
use crate::result::IonFailure;
use crate::types::Int;
use crate::{IonError, IonResult, IonType};
use crate::{Int, IonError, IonResult, IonType};
use num_bigint::{BigInt, BigUint, Sign};
use std::io::Read;
use std::mem;
Expand Down
8 changes: 4 additions & 4 deletions src/binary/non_blocking/raw_binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::binary::var_uint::VarUInt;
use crate::binary::IonTypeCode;
use crate::ion_reader::IonReader;
use crate::raw_reader::{BufferedRawReader, Expandable, RawStreamItem};
use crate::raw_symbol_token::RawSymbolToken;
use crate::result::IonFailure;
use crate::types::{Blob, Clob, Decimal, IntAccess, Str, SymbolId};
use crate::{Int, IonError, IonResult, IonType, RawSymbolToken, Timestamp};
use crate::{Blob, Clob, Decimal, Int, IonError, IonResult, IonType, Str, SymbolId, Timestamp};
use bytes::{BigEndian, ByteOrder};
use num_bigint::BigUint;
use num_traits::Zero;
Expand Down Expand Up @@ -876,8 +876,8 @@ impl<A: AsRef<[u8]> + Expandable> IonReader for RawBinaryReader<A> {

fn read_i64(&mut self) -> IonResult<i64> {
self.read_int().and_then(|i| {
i.as_i64()
.ok_or_else(|| IonError::decoding_error("integer was too large to fit in an i64"))
i64::try_from(i)
.map_err(|_| IonError::decoding_error("integer was too large to fit in an i64"))
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/binary/non_blocking/type_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::binary::constants::v1_0::length_codes;
use crate::binary::nibbles::nibbles_from_byte;
use crate::binary::IonTypeCode;
use crate::types::IonType;
use crate::IonType;

/// Contains all of the information that can be extracted from the one-octet type descriptor
/// found at the beginning of each value, annotations wrapper, IVM, or NOP in a binary Ion stream.
Expand Down
6 changes: 3 additions & 3 deletions src/binary/raw_binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::ion_writer::IonWriter;
use crate::raw_symbol_token_ref::{AsRawSymbolTokenRef, RawSymbolTokenRef};
use crate::result::{IonFailure, IonResult};
use crate::types::integer::IntData;
use crate::types::{ContainerType, Decimal, Int, SymbolId, Timestamp};
use crate::IonType;
use crate::types::ContainerType;
use crate::{Decimal, Int, IonType, SymbolId, Timestamp};

use super::decimal::DecimalBinaryEncoder;
use super::timestamp::TimestampBinaryEncoder;
Expand Down Expand Up @@ -910,7 +910,7 @@ mod writer_tests {
use crate::ion_reader::IonReader;
use crate::raw_symbol_token::{local_sid_token, RawSymbolToken};
use crate::reader::{Reader, ReaderBuilder, StreamItem};
use crate::types::{Blob, Clob, Symbol};
use crate::{Blob, Clob, Symbol};
use num_bigint::BigInt;
use num_traits::Float;
use std::convert::TryInto;
Expand Down
11 changes: 6 additions & 5 deletions src/binary/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::binary::raw_binary_writer::MAX_INLINE_LENGTH;
use crate::binary::var_int::VarInt;
use crate::binary::var_uint::VarUInt;
use crate::result::IonResult;
use crate::types::{Decimal, Mantissa, Precision, Timestamp};
use crate::types::{Mantissa, TimestampPrecision};
use crate::{Decimal, Timestamp};

const MAX_TIMESTAMP_LENGTH: usize = 32;

Expand Down Expand Up @@ -62,14 +63,14 @@ where
bytes_written += VarUInt::write_u64(self, utc.year() as u64)?;

// So far, we've written required fields. The rest are optional!
if timestamp.precision > Precision::Year {
if timestamp.precision > TimestampPrecision::Year {
bytes_written += VarUInt::write_u64(self, utc.month() as u64)?;
if timestamp.precision > Precision::Month {
if timestamp.precision > TimestampPrecision::Month {
bytes_written += VarUInt::write_u64(self, utc.day() as u64)?;
if timestamp.precision > Precision::Day {
if timestamp.precision > TimestampPrecision::Day {
bytes_written += VarUInt::write_u64(self, utc.hour() as u64)?;
bytes_written += VarUInt::write_u64(self, utc.minute() as u64)?;
if timestamp.precision > Precision::HourAndMinute {
if timestamp.precision > TimestampPrecision::HourAndMinute {
bytes_written += VarUInt::write_u64(self, utc.second() as u64)?;
if let Some(ref mantissa) = timestamp.fractional_seconds {
// TODO: Both branches encode directly due to one
Expand Down
2 changes: 1 addition & 1 deletion src/binary/type_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::IonResult;
use std::convert::TryFrom;

use crate::result::{IonError, IonFailure};
use crate::types::IonType;
use crate::IonType;

/// Represents the type information found in the header byte of each binary Ion value.
/// While this value can be readily mapped to a user-level [`IonType`], it is a distinct concept.
Expand Down
12 changes: 1 addition & 11 deletions src/binary/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::mem;

use crate::result::IonResult;
use crate::types::integer::UIntData;
use crate::types::{Int, UInt};
use crate::{Int, UInt};

// This limit is used for stack-allocating buffer space to encode/decode UInts.
const UINT_STACK_BUFFER_SIZE: usize = 16;
Expand Down Expand Up @@ -116,16 +116,6 @@ impl AsRef<[u8]> for EncodedUInt {
}

/// Returns the magnitude as big-endian bytes.
///
/// ```
/// use ion_rs::binary::uint;
///
/// let repr = uint::encode_u64(5u64);
/// assert_eq!(&[0x05], repr.as_bytes());
///
/// let two_bytes = uint::encode_u64(256u64);
/// assert_eq!(&[0x01, 0x00], two_bytes.as_bytes());
/// ```
pub fn encode_u64(magnitude: u64) -> EncodedUInt {
// We can divide the number of leading zero bits by 8
// to to get the number of leading zero bytes.
Expand Down
3 changes: 1 addition & 2 deletions src/blocking_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::ops::Range;

use crate::binary::non_blocking::raw_binary_reader::RawBinaryReader;
use crate::data_source::IonDataSource;
use crate::element::{Blob, Clob};
use crate::ion_reader::IonReader;
use crate::raw_reader::BufferedRawReader;
use crate::result::IonResult;
use crate::text::non_blocking::raw_text_reader::RawTextReader;
use crate::types::Timestamp;
use crate::{Blob, Clob, Timestamp};
use crate::{Decimal, Int, IonError, IonType, Str};

pub type BlockingRawTextReader<T> = BlockingRawReader<RawTextReader<Vec<u8>>, T>;
Expand Down
2 changes: 1 addition & 1 deletion src/element/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::cmp::Ordering;
/// things (including [`&str`] and [`String`]) into this sequence.
///
/// ```
/// use ion_rs::element::{Annotations, IntoAnnotations};
/// use ion_rs::{Annotations, IntoAnnotations};
/// let annotations: Annotations = ["foo", "bar", "baz"].into_annotations();
/// for annotation in &annotations {
/// assert_eq!(annotation.text().map(|s| s.len()), Some(3));
Expand Down
47 changes: 19 additions & 28 deletions src/element/builders.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::element::{Element, Sequence, Struct};
use crate::Symbol;
use crate::{Element, Sequence, Struct};

/// Constructs [Sequence], [List], and [SExp] values incrementally.
///
/// Building a [Sequence]:
/// ```
/// use ion_rs::element::{Element, Sequence};
/// use ion_rs::{Element, Sequence};
/// let actual: Sequence = Sequence::builder().push(1).push(true).push("foo").build();
/// let expected: Sequence = Sequence::new([
/// Element::integer(1),
Expand All @@ -16,7 +16,7 @@ use crate::Symbol;
/// ```
/// Building a [List]:
/// ```
/// use ion_rs::element::{Element, List, Sequence};
/// use ion_rs::{Element, Sequence, List};
/// let actual: List = Sequence::builder()
/// .push(1)
/// .push(true)
Expand All @@ -31,7 +31,7 @@ use crate::Symbol;
/// ```
/// Building a [SExp]:
/// ```
/// use ion_rs::element::{Element, SExp, Sequence};
/// use ion_rs::{Element, SExp, Sequence};
/// let actual: SExp = Sequence::builder()
/// .push(1)
/// .push(true)
Expand Down Expand Up @@ -97,8 +97,7 @@ impl SequenceBuilder {
/// Constructs [Struct] values incrementally.
///
/// ```
/// use ion_rs::element::Element;
/// use ion_rs::ion_struct;
/// use ion_rs::{Element, ion_struct};
/// let actual: Element = ion_struct! {
/// "a": 1,
/// "b": true,
Expand All @@ -110,8 +109,7 @@ impl SequenceBuilder {
/// ```
///
/// ```
/// use ion_rs::element::{Element, Struct};
/// use ion_rs::ion_struct;
/// use ion_rs::{Element, ion_struct, Struct};
/// let base_struct: Struct = ion_struct! {
/// "foo": 1,
/// "bar": 2,
Expand Down Expand Up @@ -160,8 +158,7 @@ impl StructBuilder {
/// Adds all of the provided `(name, value)` pairs to the [`Struct`] being constructed.
///
/// ```
/// use ion_rs::element::Element;
/// use ion_rs::ion_struct;
/// use ion_rs::{Element, ion_struct};
///
/// let struct1 = ion_struct! {
/// "foo": 1,
Expand Down Expand Up @@ -219,8 +216,7 @@ impl StructBuilder {
/// Constructs a list [`Element`] with the specified child values.
///
/// ```
/// use ion_rs::element::Element;
/// use ion_rs::ion_list;
/// use ion_rs::{Element, ion_list};
/// // Construct a list Element from Rust values
/// let actual: Element = ion_list!["foo", 7, false, ion_list![1.5f64, -8.25f64]].into();
/// // Construct an Element from serialized Ion data
Expand All @@ -234,8 +230,7 @@ impl StructBuilder {
///
/// ```
/// // Construct a list Element from existing Elements
/// use ion_rs::element::{Element, IntoAnnotatedElement};
/// use ion_rs::ion_list;
/// use ion_rs::{Element, ion_list, IntoAnnotatedElement};
///
/// let string_element: Element = "foo".into();
/// let bool_element: Element = true.into();
Expand All @@ -256,16 +251,15 @@ impl StructBuilder {
#[macro_export]
macro_rules! ion_list {
($($element:expr),* $(,)?) => {{
use $crate::element::Sequence;
use $crate::Sequence;
Sequence::builder()$(.push($element))*.build_list()
}};
}

/// Constructs an s-expression [`Element`] with the specified child values.
///
/// ```
/// use ion_rs::ion_sexp;
/// use ion_rs::element::Element;
/// use ion_rs::{Element, ion_sexp};
/// // Construct an s-expression Element from Rust values
/// let actual: Element = ion_sexp!("foo" 7 false ion_sexp!(1.5f64 8.25f64)).into();
/// // Construct an Element from serialized Ion data
Expand All @@ -279,8 +273,7 @@ macro_rules! ion_list {
///
/// ```
/// // Construct a s-expression Element from existing Elements
/// use ion_rs::ion_sexp;
/// use ion_rs::element::{Element, IntoAnnotatedElement};
/// use ion_rs::{Element, ion_sexp, IntoAnnotatedElement};
///
/// let string_element: Element = "foo".into();
/// let bool_element: Element = true.into();
Expand All @@ -300,7 +293,7 @@ macro_rules! ion_list {
#[macro_export]
macro_rules! ion_sexp {
($($element:expr)*) => {{
use $crate::element::Sequence;
use $crate::Sequence;
Sequence::builder()$(.push($element))*.build_sexp()
}};
}
Expand All @@ -311,8 +304,7 @@ macro_rules! ion_sexp {
/// `Into<Element>`.
///
/// ```
/// use ion_rs::element::Element;
/// use ion_rs::{ion_struct, IonType};
/// use ion_rs::{Element, ion_struct, IonType};
/// let field_name_2 = "x";
/// let prefix = "abc";
/// let suffix = "def";
Expand Down Expand Up @@ -340,7 +332,7 @@ macro_rules! ion_sexp {
#[macro_export]
macro_rules! ion_struct {
($($field_name:tt : $element:expr),* $(,)?) => {{
use $crate::element::Struct;
use $crate::Struct;
Struct::builder()$(.with_field($field_name, $element))*.build()
}};
}
Expand All @@ -351,8 +343,7 @@ macro_rules! ion_struct {
/// `List` or `SExp`.
///
/// ```
/// use ion_rs::element::{Element, Sequence};
/// use ion_rs::{ion_seq, ion_list};
/// use ion_rs::{Element, ion_seq, ion_list, Sequence};
/// // Construct a Sequence from serialized Ion data
/// let expected: Sequence = Element::read_all(r#" "foo" 7 false [1.5e0, -8.25e0] "#).unwrap();
/// // Construct a Sequence from Rust values
Expand All @@ -367,16 +358,16 @@ macro_rules! ion_struct {
#[macro_export]
macro_rules! ion_seq {
($($element:expr),* $(,)?) => {{
use $crate::element::Sequence;
use $crate::Sequence;
Sequence::builder()$(.push($element))*.build()
}};
($($element:expr)*) => {{
use $crate::element::Sequence;
use $crate::Sequence;
Sequence::builder()$(.push($element))*.build()
}};
}

use crate::types::{List, SExp};
use crate::{List, SExp};
pub use {ion_list, ion_sexp, ion_struct};

#[cfg(test)]
Expand Down