pub trait IonCReader {
Show 18 methods
// Required methods
fn next(&mut self) -> IonCResult<ION_TYPE>;
fn get_type(&self) -> IonCResult<ION_TYPE>;
fn step_in(&mut self) -> IonCResult<()>;
fn step_out(&mut self) -> IonCResult<()>;
fn depth(&self) -> IonCResult<i32>;
fn is_null(&self) -> IonCResult<bool>;
fn is_in_struct(&self) -> IonCResult<bool>;
fn get_field_name(&mut self) -> IonCResult<StrSliceRef<'_>>;
fn get_annotations(&mut self) -> IonCResult<StrSlicesRef<'_>>;
fn read_bool(&mut self) -> IonCResult<bool>;
fn read_i64(&mut self) -> IonCResult<i64>;
fn read_bigint(&mut self) -> IonCResult<BigInt>;
fn read_f64(&mut self) -> IonCResult<f64>;
fn read_bigdecimal(&mut self) -> IonCResult<BigDecimal>;
fn read_datetime(&mut self) -> IonCResult<IonDateTime>;
fn read_string(&mut self) -> IonCResult<StrSliceRef<'_>>;
fn read_bytes(&mut self) -> IonCResult<Vec<u8>>;
fn pos(&self) -> IonCResult<Position>;
}Expand description
The reading API for Ion C.
See also IonCReaderHandle.
§Usage
let mut reader = IonCReaderHandle::try_from(b"\xE0\x01\x00\xEA\x85hello".as_ref())?;
let tid = reader.next()?;
assert_eq!(ION_TYPE_STRING, tid);
// reader_handle implements Drop, so we're good to go!Required Methods§
Sourcefn next(&mut self) -> IonCResult<ION_TYPE>
fn next(&mut self) -> IonCResult<ION_TYPE>
Advances the reader to the next value and returns the type.
Sourcefn get_type(&self) -> IonCResult<ION_TYPE>
fn get_type(&self) -> IonCResult<ION_TYPE>
Returns the type of the current position.
§Usage
let mut reader = IonCReaderHandle::try_from("'''hello!'''")?;
assert_eq!(ION_TYPE_NONE, reader.get_type()?);
assert_eq!(ION_TYPE_STRING, reader.next()?);
assert_eq!(ION_TYPE_STRING, reader.get_type()?);
assert_eq!(ION_TYPE_EOF, reader.next()?);
assert_eq!(ION_TYPE_EOF, reader.get_type()?);Sourcefn step_in(&mut self) -> IonCResult<()>
fn step_in(&mut self) -> IonCResult<()>
Steps in to the current container.
Sourcefn step_out(&mut self) -> IonCResult<()>
fn step_out(&mut self) -> IonCResult<()>
Steps out of the current container.
Sourcefn depth(&self) -> IonCResult<i32>
fn depth(&self) -> IonCResult<i32>
Returns the current container depth.
§Usage
let mut reader = IonCReaderHandle::try_from("[[]]")?;
assert_eq!(ION_TYPE_LIST, reader.next()?);
reader.step_in()?;
assert_eq!(1, reader.depth()?);
assert_eq!(ION_TYPE_LIST, reader.next()?);
reader.step_in()?;
assert_eq!(2, reader.depth()?);Sourcefn is_null(&self) -> IonCResult<bool>
fn is_null(&self) -> IonCResult<bool>
Returns if the reader is positioned on a null value.
§Usage
let mut reader = IonCReaderHandle::try_from("null.int 4")?;
assert_eq!(ION_TYPE_INT, reader.next()?);
assert!(reader.is_null()?);
assert_eq!(ION_TYPE_INT, reader.next()?);
assert!(!reader.is_null()?);Sourcefn is_in_struct(&self) -> IonCResult<bool>
fn is_in_struct(&self) -> IonCResult<bool>
Returns if the reader is positioned within a struct value.
§Usage
let mut reader = IonCReaderHandle::try_from("{}")?;
assert_eq!(ION_TYPE_STRUCT, reader.next()?);
assert!(!reader.is_in_struct()?);
reader.step_in()?;
assert!(reader.is_in_struct()?);Sourcefn get_field_name(&mut self) -> IonCResult<StrSliceRef<'_>>
fn get_field_name(&mut self) -> IonCResult<StrSliceRef<'_>>
Returns the field name if the reader positioned within a structure.
§Usage
let mut reader = IonCReaderHandle::try_from("{a:5}")?;
assert_eq!(ION_TYPE_STRUCT, reader.next()?);
reader.step_in()?;
assert_eq!(ION_TYPE_INT, reader.next()?);
assert_eq!("a", reader.get_field_name()?.as_str());Sourcefn get_annotations(&mut self) -> IonCResult<StrSlicesRef<'_>>
fn get_annotations(&mut self) -> IonCResult<StrSlicesRef<'_>>
Retrieves the annotations associated with the current value.
Note that this allocates a vector on the heap for the IonCStringRef instances.
If this is not desired, use the low-level annotation functions.
§Usage
let mut reader = IonCReaderHandle::try_from("ab::cde::fghi::5")?;
assert_eq!(ION_TYPE_INT, reader.next()?);
let annotations = reader.get_annotations()?;
assert_eq!(
vec!["ab", "cde", "fghi"].as_slice(),
annotations.as_ref()
);Sourcefn read_bool(&mut self) -> IonCResult<bool>
fn read_bool(&mut self) -> IonCResult<bool>
Reads a bool value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("true")?;
assert_eq!(ION_TYPE_BOOL, reader.next()?);
assert!(reader.read_bool()?);Sourcefn read_i64(&mut self) -> IonCResult<i64>
fn read_i64(&mut self) -> IonCResult<i64>
Reads an int value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("42")?;
assert_eq!(ION_TYPE_INT, reader.next()?);
assert_eq!(42, reader.read_i64()?);Sourcefn read_bigint(&mut self) -> IonCResult<BigInt>
fn read_bigint(&mut self) -> IonCResult<BigInt>
Reads an int value from the reader as a BigInt.
§Usage
let mut reader = IonCReaderHandle::try_from("0x5195a4b154400e07dee3a7378c403b2d5dd6dd58735")?;
assert_eq!(ION_TYPE_INT, reader.next()?);
assert_eq!(
BigInt::parse_bytes(b"1907775120294590714755986204580814176547217067050805", 10).unwrap(),
reader.read_bigint()?
);Sourcefn read_f64(&mut self) -> IonCResult<f64>
fn read_f64(&mut self) -> IonCResult<f64>
Reads a float value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("3.0e0")?;
assert_eq!(ION_TYPE_FLOAT, reader.next()?);
assert_eq!(3.0, reader.read_f64()?);Sourcefn read_bigdecimal(&mut self) -> IonCResult<BigDecimal>
fn read_bigdecimal(&mut self) -> IonCResult<BigDecimal>
Reads a bigdecimal value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("3.0")?;
assert_eq!(ION_TYPE_DECIMAL, reader.next()?);
let value = BigDecimal::parse_bytes(b"30E-1", 10).unwrap();
assert_eq!(value, reader.read_bigdecimal()?);Sourcefn read_datetime(&mut self) -> IonCResult<IonDateTime>
fn read_datetime(&mut self) -> IonCResult<IonDateTime>
Reads a timestamp value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("2020-10-10T12:34:45.123-00:00")?;
assert_eq!(ION_TYPE_TIMESTAMP, reader.next()?);
let ion_dt = reader.read_datetime()?;
// the point in time should be the same
let expected_dt = DateTime::parse_from_rfc3339("2020-10-10T12:34:45.123Z").unwrap();
assert_eq!(&expected_dt, ion_dt.as_datetime());
// precision should be millisecond level
if let Fractional(Digits(digits)) = ion_dt.precision() {
assert_eq!(3, *digits);
} else {
assert!(false, "Expected digits precision!");
}
// we should have an unknown offset
assert_eq!(UnknownOffset, ion_dt.offset_kind());Sourcefn read_string(&mut self) -> IonCResult<StrSliceRef<'_>>
fn read_string(&mut self) -> IonCResult<StrSliceRef<'_>>
Reads a string/symbol value from the reader.
§Usage
let mut reader = IonCReaderHandle::try_from("\"🦄\" '✨'")?;
assert_eq!(ION_TYPE_STRING, reader.next()?);
assert_eq!("🦄", reader.read_string()?.as_str());
assert_eq!(ION_TYPE_SYMBOL, reader.next()?);
assert_eq!("✨", reader.read_string()?.as_str());Sourcefn read_bytes(&mut self) -> IonCResult<Vec<u8>>
fn read_bytes(&mut self) -> IonCResult<Vec<u8>>
Reads a clob/blob value from the reader.
This method implements a vector on the heap to store a copy of the LOB. If this is not desired, use the low-level length and read methods directly.
§Usage
let mut reader = IonCReaderHandle::try_from("{{\"hello\"}} {{d29ybGQ=}} {{}}")?;
assert_eq!(ION_TYPE_CLOB, reader.next()?);
assert_eq!(b"hello", reader.read_bytes()?.as_slice());
assert_eq!(ION_TYPE_BLOB, reader.next()?);
assert_eq!(b"world", reader.read_bytes()?.as_slice());
assert_eq!(ION_TYPE_BLOB, reader.next()?);
assert_eq!(b"", reader.read_bytes()?.as_slice());Sourcefn pos(&self) -> IonCResult<Position>
fn pos(&self) -> IonCResult<Position>
Returns the current position. TODO: blah blah.