-
Notifications
You must be signed in to change notification settings - Fork 36
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
Adds LazyRawTextReader
support for reading lists
#617
Changes from all commits
e0a83d8
89f79aa
840be4d
5db1ff0
07d4a70
181e0a5
357ca8f
716ff34
e29fec5
8f79a36
4cb9b2b
54470d2
78014e7
eba5913
59a2496
0883ec5
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 |
---|---|---|
|
@@ -4,11 +4,12 @@ use crate::lazy::binary::raw::reader::LazyRawBinaryReader; | |
use crate::lazy::binary::raw::sequence::LazyRawBinarySequence; | ||
use crate::lazy::binary::raw::value::LazyRawBinaryValue; | ||
use crate::lazy::decoder::private::{LazyContainerPrivate, LazyRawFieldPrivate}; | ||
use crate::lazy::decoder::{LazyDecoder, LazyRawField, LazyRawSequence, LazyRawStruct}; | ||
use crate::lazy::decoder::{LazyDecoder, LazyRawField, LazyRawStruct}; | ||
use crate::lazy::raw_value_ref::RawValueRef; | ||
use crate::lazy::text::raw::reader::LazyRawTextReader; | ||
use crate::lazy::text::raw::sequence::LazyRawTextSequence; | ||
use crate::lazy::text::value::LazyRawTextValue; | ||
use crate::{IonResult, IonType, RawSymbolTokenRef}; | ||
use crate::{IonResult, RawSymbolTokenRef}; | ||
use std::marker::PhantomData; | ||
|
||
// These types derive trait implementations in order to allow types that containing them | ||
|
@@ -33,34 +34,6 @@ impl<'data> LazyDecoder<'data> for BinaryEncoding { | |
// === Placeholders === | ||
// The types below will need to be properly defined in order for the lazy text reader to be complete. | ||
// The exist to satisfy various trait definitions. | ||
#[derive(Debug, Clone)] | ||
pub struct ToDoTextSequence; | ||
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 PR defines the |
||
|
||
impl<'data> LazyContainerPrivate<'data, TextEncoding> for ToDoTextSequence { | ||
fn from_value(_value: LazyRawTextValue<'data>) -> Self { | ||
todo!() | ||
} | ||
} | ||
|
||
impl<'data> LazyRawSequence<'data, TextEncoding> for ToDoTextSequence { | ||
type Iterator = Box<dyn Iterator<Item = IonResult<LazyRawTextValue<'data>>>>; | ||
|
||
fn annotations(&self) -> ToDoTextAnnotationsIterator<'data> { | ||
todo!() | ||
} | ||
|
||
fn ion_type(&self) -> IonType { | ||
todo!() | ||
} | ||
|
||
fn iter(&self) -> Self::Iterator { | ||
todo!() | ||
} | ||
|
||
fn as_value(&self) -> &<TextEncoding as LazyDecoder<'data>>::Value { | ||
todo!() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ToDoTextStruct; | ||
|
@@ -127,7 +100,7 @@ impl<'data> Iterator for ToDoTextAnnotationsIterator<'data> { | |
impl<'data> LazyDecoder<'data> for TextEncoding { | ||
type Reader = LazyRawTextReader<'data>; | ||
type Value = LazyRawTextValue<'data>; | ||
type Sequence = ToDoTextSequence; | ||
type Sequence = LazyRawTextSequence<'data>; | ||
type Struct = ToDoTextStruct; | ||
type AnnotationsIterator = ToDoTextAnnotationsIterator<'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. 🗺️ All changes in this file stem from the change on line 175; see the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod reader; | ||
pub mod sequence; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use crate::lazy::decoder::private::LazyContainerPrivate; | ||
use crate::lazy::decoder::{LazyDecoder, LazyRawSequence, LazyRawValue}; | ||
use crate::lazy::encoding::TextEncoding; | ||
use crate::lazy::text::buffer::TextBufferView; | ||
use crate::lazy::text::parse_result::AddContext; | ||
use crate::lazy::text::parse_result::ToIteratorOutput; | ||
use crate::lazy::text::value::LazyRawTextValue; | ||
use crate::{IonResult, IonType}; | ||
use std::fmt; | ||
use std::fmt::{Debug, Formatter}; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct LazyRawTextSequence<'data> { | ||
pub(crate) value: LazyRawTextValue<'data>, | ||
} | ||
|
||
impl<'data> LazyRawTextSequence<'data> { | ||
pub fn ion_type(&self) -> IonType { | ||
self.value.ion_type() | ||
} | ||
|
||
pub fn iter(&self) -> RawTextSequenceIterator<'data> { | ||
// Make an iterator over the input bytes that follow the initial `[` | ||
RawTextSequenceIterator::new(self.value.input.slice_to_end(1)) | ||
} | ||
} | ||
|
||
impl<'data> LazyContainerPrivate<'data, TextEncoding> for LazyRawTextSequence<'data> { | ||
fn from_value(value: LazyRawTextValue<'data>) -> Self { | ||
LazyRawTextSequence { value } | ||
} | ||
} | ||
|
||
impl<'data> LazyRawSequence<'data, TextEncoding> for LazyRawTextSequence<'data> { | ||
type Iterator = RawTextSequenceIterator<'data>; | ||
|
||
fn annotations(&self) -> <TextEncoding as LazyDecoder<'data>>::AnnotationsIterator { | ||
todo!("lazy sequence annotations") | ||
} | ||
|
||
fn ion_type(&self) -> IonType { | ||
self.value.ion_type() | ||
} | ||
|
||
fn iter(&self) -> Self::Iterator { | ||
LazyRawTextSequence::iter(self) | ||
} | ||
|
||
fn as_value(&self) -> &LazyRawTextValue<'data> { | ||
&self.value | ||
} | ||
} | ||
|
||
impl<'a, 'data> IntoIterator for &'a LazyRawTextSequence<'data> { | ||
type Item = IonResult<LazyRawTextValue<'data>>; | ||
type IntoIter = RawTextSequenceIterator<'data>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.iter() | ||
} | ||
} | ||
|
||
impl<'a> Debug for LazyRawTextSequence<'a> { | ||
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 was copied from the binary sequence's impl. It tries to read the sequence's contents on a best-effort basis. We may be able to think up something more robust, but as a 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. Suggestion—can we just use 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. Good idea. Opened #641 to track this. |
||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self.value.encoded_value.ion_type() { | ||
IonType::SExp => { | ||
write!(f, "(")?; | ||
for value in self { | ||
write!( | ||
f, | ||
"{:?} ", | ||
value | ||
.map_err(|_| fmt::Error)? | ||
.read() | ||
.map_err(|_| fmt::Error)? | ||
)?; | ||
} | ||
write!(f, ")").unwrap(); | ||
} | ||
IonType::List => { | ||
write!(f, "[")?; | ||
for value in self { | ||
write!( | ||
f, | ||
"{:?},", | ||
value | ||
.map_err(|_| fmt::Error)? | ||
.read() | ||
.map_err(|_| fmt::Error)? | ||
)?; | ||
} | ||
write!(f, "]").unwrap(); | ||
} | ||
_ => unreachable!("LazyRawSequence is only created for list and sexp"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct RawTextSequenceIterator<'data> { | ||
input: TextBufferView<'data>, | ||
} | ||
|
||
impl<'data> RawTextSequenceIterator<'data> { | ||
pub(crate) fn new(input: TextBufferView<'data>) -> RawTextSequenceIterator<'data> { | ||
RawTextSequenceIterator { input } | ||
} | ||
} | ||
|
||
impl<'data> Iterator for RawTextSequenceIterator<'data> { | ||
type Item = IonResult<LazyRawTextValue<'data>>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.input.match_list_value() { | ||
Ok((remaining, Some(value))) => { | ||
self.input = remaining; | ||
Some(Ok(value)) | ||
} | ||
Ok((_remaining, None)) => None, | ||
Err(e) => e | ||
.with_context("reading the next list value", self.input) | ||
.transpose(), | ||
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 call to |
||
} | ||
} | ||
} |
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.
🗺️ Now that there are both binary and text readers,
RawSequenceIterator
has become theRawBinarySequenceIterator
.