-
Notifications
You must be signed in to change notification settings - Fork 37
Adds lazy reader support for reading annotations #622
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
Changes from all commits
e0a83d8
89f79aa
840be4d
5db1ff0
07d4a70
181e0a5
357ca8f
716ff34
e29fec5
8f79a36
4cb9b2b
54470d2
78014e7
a6a3aa8
4fc9078
11174ac
719dbaa
f603872
4696ca5
c3ce71c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
use std::fmt::Debug; | ||
|
||
use crate::lazy::binary::raw::annotations_iterator::RawBinaryAnnotationsIterator; | ||
use crate::lazy::binary::raw::r#struct::{ | ||
LazyRawBinaryField, LazyRawBinaryStruct, RawBinaryStructIterator, | ||
}; | ||
|
@@ -22,7 +23,7 @@ use crate::lazy::text::raw::r#struct::{ | |
}; | ||
use crate::lazy::text::raw::reader::LazyRawTextReader; | ||
use crate::lazy::text::raw::sequence::{LazyRawTextSequence, RawTextSequenceIterator}; | ||
use crate::lazy::text::value::LazyRawTextValue; | ||
use crate::lazy::text::value::{LazyRawTextValue, RawTextAnnotationsIterator}; | ||
use crate::{IonResult, IonType, RawSymbolTokenRef}; | ||
|
||
/// An implementation of the `LazyDecoder` trait that can read either text or binary Ion. | ||
|
@@ -37,7 +38,7 @@ impl<'data> LazyDecoder<'data> for AnyEncoding { | |
type Value = LazyRawAnyValue<'data>; | ||
type Sequence = LazyRawAnySequence<'data>; | ||
type Struct = LazyRawAnyStruct<'data>; | ||
type AnnotationsIterator = Box<dyn Iterator<Item = IonResult<RawSymbolTokenRef<'data>>>>; | ||
type AnnotationsIterator = RawAnyAnnotationsIterator<'data>; | ||
} | ||
|
||
// ===== Readers ====== | ||
|
@@ -181,10 +182,10 @@ impl<'data> From<RawStreamItem<'data, BinaryEncoding>> for RawStreamItem<'data, | |
} | ||
|
||
impl<'data> LazyRawValuePrivate<'data> for LazyRawAnyValue<'data> { | ||
fn field_name(&self) -> Option<RawSymbolTokenRef<'data>> { | ||
fn field_name(&self) -> IonResult<RawSymbolTokenRef<'data>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ This trait method signature had to be changed. The binary raw reader only ever encounters |
||
match &self.encoding { | ||
LazyRawValueKind::Text_1_0(v) => v.field_name(), | ||
LazyRawValueKind::Binary_1_0(v) => v.field_name().map(RawSymbolTokenRef::SymbolId), | ||
LazyRawValueKind::Binary_1_0(v) => v.field_name(), | ||
} | ||
} | ||
} | ||
|
@@ -204,8 +205,15 @@ impl<'data> LazyRawValue<'data, AnyEncoding> for LazyRawAnyValue<'data> { | |
} | ||
} | ||
|
||
fn annotations(&self) -> <AnyEncoding as LazyDecoder<'data>>::AnnotationsIterator { | ||
todo!() | ||
Comment on lines
-207
to
-208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ As with the other |
||
fn annotations(&self) -> RawAnyAnnotationsIterator<'data> { | ||
match &self.encoding { | ||
LazyRawValueKind::Text_1_0(v) => RawAnyAnnotationsIterator { | ||
encoding: RawAnnotationsIteratorKind::Text_1_0(v.annotations()), | ||
}, | ||
LazyRawValueKind::Binary_1_0(v) => RawAnyAnnotationsIterator { | ||
encoding: RawAnnotationsIteratorKind::Binary_1_0(v.annotations()), | ||
}, | ||
} | ||
} | ||
|
||
fn read(&self) -> IonResult<RawValueRef<'data, AnyEncoding>> { | ||
|
@@ -216,6 +224,28 @@ impl<'data> LazyRawValue<'data, AnyEncoding> for LazyRawAnyValue<'data> { | |
} | ||
} | ||
|
||
// ===== Annotations ===== | ||
|
||
pub struct RawAnyAnnotationsIterator<'data> { | ||
encoding: RawAnnotationsIteratorKind<'data>, | ||
} | ||
|
||
pub enum RawAnnotationsIteratorKind<'data> { | ||
Text_1_0(RawTextAnnotationsIterator<'data>), | ||
Binary_1_0(RawBinaryAnnotationsIterator<'data>), | ||
} | ||
|
||
impl<'data> Iterator for RawAnyAnnotationsIterator<'data> { | ||
type Item = IonResult<RawSymbolTokenRef<'data>>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match &mut self.encoding { | ||
RawAnnotationsIteratorKind::Text_1_0(i) => i.next(), | ||
RawAnnotationsIteratorKind::Binary_1_0(i) => i.next(), | ||
} | ||
} | ||
} | ||
|
||
// ===== Sequences ====== | ||
|
||
#[derive(Debug, Clone)] | ||
|
@@ -422,7 +452,14 @@ impl<'data> LazyRawStruct<'data, AnyEncoding> for LazyRawAnyStruct<'data> { | |
type Iterator = RawAnyStructIterator<'data>; | ||
|
||
fn annotations(&self) -> <AnyEncoding as LazyDecoder<'data>>::AnnotationsIterator { | ||
todo!() | ||
match &self.encoding { | ||
LazyRawStructKind::Text_1_0(s) => RawAnyAnnotationsIterator { | ||
encoding: RawAnnotationsIteratorKind::Text_1_0(s.annotations()), | ||
}, | ||
LazyRawStructKind::Binary_1_0(s) => RawAnyAnnotationsIterator { | ||
encoding: RawAnnotationsIteratorKind::Binary_1_0(s.annotations()), | ||
}, | ||
} | ||
} | ||
|
||
fn find(&self, name: &str) -> IonResult<Option<LazyRawAnyValue<'data>>> { | ||
|
@@ -491,13 +528,20 @@ mod tests { | |
use crate::lazy::decoder::{LazyRawReader, LazyRawSequence, LazyRawValue}; | ||
use crate::lazy::raw_stream_item::RawStreamItem; | ||
use crate::lazy::raw_value_ref::RawValueRef; | ||
use crate::IonResult; | ||
use crate::{IonResult, RawSymbolTokenRef}; | ||
|
||
#[test] | ||
fn any_encoding() -> IonResult<()> { | ||
fn test_input(data: &[u8]) -> IonResult<()> { | ||
let mut reader = LazyRawAnyReader::new(data); | ||
assert_eq!(reader.next()?.expect_ivm()?, (1, 0)); | ||
let _strukt = reader.next()?.expect_value()?.read()?.expect_struct()?; | ||
let name = reader.next()?.expect_value()?; | ||
assert_eq!( | ||
name.annotations().next().unwrap()?, | ||
RawSymbolTokenRef::SymbolId(4) | ||
); | ||
assert_eq!(name.read()?.expect_string()?.text(), "Gary"); | ||
assert_eq!( | ||
reader.next()?.expect_value()?.read()?, | ||
RawValueRef::String("foo".into()) | ||
|
@@ -524,7 +568,15 @@ mod tests { | |
Ok(()) | ||
} | ||
|
||
let text_data = "$ion_1_0 \"foo\" 5 false [1, 2, 3] "; | ||
let text_data = r#" | ||
$ion_1_0 | ||
{$7: ["a", "b", "c"]} | ||
$4::"Gary" | ||
"foo" | ||
5 | ||
false | ||
[1, 2, 3] | ||
"#; | ||
let binary_data = to_binary_ion(text_data)?; | ||
|
||
test_input(text_data.as_bytes())?; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,14 @@ impl<'a> Debug for LazyRawBinaryValue<'a> { | |
type ValueParseResult<'data, F> = IonResult<RawValueRef<'data, F>>; | ||
|
||
impl<'data> LazyRawValuePrivate<'data> for LazyRawBinaryValue<'data> { | ||
fn field_name(&self) -> Option<RawSymbolTokenRef<'data>> { | ||
self.encoded_value.field_id.map(RawSymbolTokenRef::SymbolId) | ||
fn field_name(&self) -> IonResult<RawSymbolTokenRef<'data>> { | ||
if let Some(field_id) = self.encoded_value.field_id { | ||
Ok(RawSymbolTokenRef::SymbolId(field_id)) | ||
} else { | ||
IonResult::illegal_operation( | ||
"requested field name, but value was not in a struct field", | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -179,7 +185,7 @@ impl<'data> LazyRawBinaryValue<'data> { | |
|
||
/// If this value is within a struct, returns its associated field name as a `Some(SymbolID)`. | ||
/// Otherwise, returns `None`. | ||
pub(crate) fn field_name(&self) -> Option<SymbolId> { | ||
pub(crate) fn field_id(&self) -> Option<SymbolId> { | ||
Comment on lines
-182
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ I renamed this binary reader method from The binary reader can always return a symbol ID representing the field name if there is one. However, the raw reader trait needs to accommodate failure cases in the raw text reader and returns an |
||
self.encoded_value.field_id | ||
} | ||
|
||
|
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.
🗺️ The
Box<dyn _>
type was just a placeholder.