#![allow(non_camel_case_types)]
use crate::lazy::binary::raw::annotations_iterator::RawBinaryAnnotationsIterator as RawBinaryAnnotationsIterator_1_0;
use crate::lazy::binary::raw::r#struct::{
LazyRawBinaryFieldName_1_0, LazyRawBinaryStruct_1_0, RawBinaryStructIterator_1_0,
};
use crate::lazy::binary::raw::reader::LazyRawBinaryReader_1_0;
use crate::lazy::binary::raw::sequence::{
LazyRawBinaryList_1_0, LazyRawBinarySExp_1_0, RawBinarySequenceIterator_1_0,
};
use crate::lazy::binary::raw::v1_1::e_expression::{
BinaryEExpArgGroup, BinaryEExpArgGroupIterator, BinaryEExpression_1_1,
};
use crate::lazy::binary::raw::v1_1::r#struct::{
LazyRawBinaryFieldName_1_1, LazyRawBinaryStruct_1_1, RawBinaryStructIterator_1_1,
};
use crate::lazy::binary::raw::v1_1::reader::LazyRawBinaryReader_1_1;
use crate::lazy::binary::raw::v1_1::sequence::{
LazyRawBinaryList_1_1, LazyRawBinarySExp_1_1, RawBinarySequenceIterator_1_1,
};
use crate::lazy::binary::raw::v1_1::value::{
LazyRawBinaryValue_1_1, LazyRawBinaryVersionMarker_1_1,
};
use crate::lazy::binary::raw::v1_1::RawBinaryAnnotationsIterator_1_1;
use crate::lazy::binary::raw::value::{LazyRawBinaryValue_1_0, LazyRawBinaryVersionMarker_1_0};
use crate::lazy::decoder::private::LazyContainerPrivate;
use crate::lazy::decoder::{
Decoder, HasRange, HasSpan, LazyRawContainer, LazyRawFieldExpr, LazyRawFieldName,
LazyRawReader, LazyRawSequence, LazyRawStruct, LazyRawValue, LazyRawValueExpr, RawValueExpr,
RawVersionMarker,
};
use crate::lazy::encoding::{
BinaryEncoding_1_0, BinaryEncoding_1_1, TextEncoding_1_0, TextEncoding_1_1,
};
use crate::lazy::expanded::e_expression::EExpArgGroup;
use crate::lazy::expanded::macro_evaluator::{
EExpressionArgGroup, IsExhaustedIterator, RawEExpression,
};
use crate::lazy::expanded::template::ParameterEncoding;
use crate::lazy::expanded::EncodingContextRef;
use crate::lazy::raw_stream_item::LazyRawStreamItem;
use crate::lazy::raw_value_ref::RawValueRef;
use crate::lazy::span::Span;
use crate::lazy::streaming_raw_reader::RawReaderState;
use crate::lazy::text::raw::r#struct::LazyRawTextFieldName;
use crate::lazy::text::raw::reader::LazyRawTextReader_1_0;
use crate::lazy::text::raw::sequence::{RawTextList, RawTextSExp};
use crate::lazy::text::raw::v1_1::arg_group::{
EExpArg, EExpArgExpr, TextEExpArgGroup, TextEExpArgGroupIterator,
};
use crate::lazy::text::raw::v1_1::reader::{
LazyRawTextReader_1_1, LazyRawTextStruct, MacroIdRef, RawTextSequenceCacheIterator,
RawTextStructCacheIterator, TextEExpression_1_1,
};
use crate::lazy::text::value::{
LazyRawTextValue_1_0, LazyRawTextValue_1_1, LazyRawTextVersionMarker_1_0,
LazyRawTextVersionMarker_1_1, RawTextAnnotationsIterator,
};
use crate::symbol_table::{SystemSymbolTable, SYSTEM_SYMBOLS_1_0, SYSTEM_SYMBOLS_1_1};
use crate::{try_next, Encoding, IonResult, IonType, RawStreamItem, RawSymbolRef};
use std::fmt::Debug;
use std::ops::Range;
#[derive(Debug, Clone, Copy)]
pub struct AnyEncoding;
impl Decoder for AnyEncoding {
const INITIAL_ENCODING_EXPECTED: IonEncoding = IonEncoding::Text_1_0;
type Reader<'data> = LazyRawAnyReader<'data>;
type Value<'top> = LazyRawAnyValue<'top>;
type SExp<'top> = LazyRawAnySExp<'top>;
type List<'top> = LazyRawAnyList<'top>;
type Struct<'top> = LazyRawAnyStruct<'top>;
type FieldName<'top> = LazyRawAnyFieldName<'top>;
type AnnotationsIterator<'top> = RawAnyAnnotationsIterator<'top>;
type EExp<'top> = LazyRawAnyEExpression<'top>;
type VersionMarker<'top> = LazyRawAnyVersionMarker<'top>;
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyVersionMarker<'top> {
encoding: LazyRawAnyVersionMarkerKind<'top>,
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawAnyVersionMarkerKind<'top> {
Text_1_0(LazyRawTextVersionMarker_1_0<'top>),
Binary_1_0(LazyRawBinaryVersionMarker_1_0<'top>),
Text_1_1(LazyRawTextVersionMarker_1_1<'top>),
Binary_1_1(LazyRawBinaryVersionMarker_1_1<'top>),
}
impl LazyRawAnyVersionMarker<'_> {
pub fn encoding(&self) -> IonEncoding {
use crate::lazy::any_encoding::LazyRawAnyVersionMarkerKind::*;
match self.encoding {
Text_1_0(_) => TextEncoding_1_0.encoding(),
Binary_1_0(_) => BinaryEncoding_1_0.encoding(),
Text_1_1(_) => TextEncoding_1_1.encoding(),
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
}
}
}
impl<'top> HasSpan<'top> for LazyRawAnyVersionMarker<'top> {
fn span(&self) -> Span<'top> {
use LazyRawAnyVersionMarkerKind::*;
match self.encoding {
Text_1_0(marker) => marker.span(),
Binary_1_0(marker) => marker.span(),
Text_1_1(marker) => marker.span(),
Binary_1_1(marker) => marker.span(),
}
}
}
impl HasRange for LazyRawAnyVersionMarker<'_> {
fn range(&self) -> Range<usize> {
use LazyRawAnyVersionMarkerKind::*;
match self.encoding {
Text_1_0(marker) => marker.range(),
Binary_1_0(marker) => marker.range(),
Text_1_1(marker) => marker.range(),
Binary_1_1(marker) => marker.range(),
}
}
}
impl<'top> RawVersionMarker<'top> for LazyRawAnyVersionMarker<'top> {
fn major_minor(&self) -> (u8, u8) {
use LazyRawAnyVersionMarkerKind::*;
match self.encoding {
Text_1_0(marker) => marker.major_minor(),
Binary_1_0(marker) => marker.major_minor(),
Text_1_1(marker) => marker.major_minor(),
Binary_1_1(marker) => marker.major_minor(),
}
}
fn stream_encoding_before_marker(&self) -> IonEncoding {
use LazyRawAnyVersionMarkerKind::*;
match self.encoding {
Text_1_0(_) => IonEncoding::Text_1_0,
Binary_1_0(_) => IonEncoding::Binary_1_0,
Text_1_1(_) => IonEncoding::Text_1_1,
Binary_1_1(_) => IonEncoding::Binary_1_1,
}
}
}
impl<'top> From<LazyRawBinaryVersionMarker_1_0<'top>> for LazyRawAnyVersionMarker<'top> {
fn from(value: LazyRawBinaryVersionMarker_1_0<'top>) -> Self {
LazyRawAnyVersionMarker {
encoding: LazyRawAnyVersionMarkerKind::Binary_1_0(value),
}
}
}
impl<'top> From<LazyRawBinaryVersionMarker_1_1<'top>> for LazyRawAnyVersionMarker<'top> {
fn from(value: LazyRawBinaryVersionMarker_1_1<'top>) -> Self {
LazyRawAnyVersionMarker {
encoding: LazyRawAnyVersionMarkerKind::Binary_1_1(value),
}
}
}
impl<'top> From<LazyRawTextVersionMarker_1_0<'top>> for LazyRawAnyVersionMarker<'top> {
fn from(value: LazyRawTextVersionMarker_1_0<'top>) -> Self {
LazyRawAnyVersionMarker {
encoding: LazyRawAnyVersionMarkerKind::Text_1_0(value),
}
}
}
impl<'top> From<LazyRawTextVersionMarker_1_1<'top>> for LazyRawAnyVersionMarker<'top> {
fn from(value: LazyRawTextVersionMarker_1_1<'top>) -> Self {
LazyRawAnyVersionMarker {
encoding: LazyRawAnyVersionMarkerKind::Text_1_1(value),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyEExpression<'top> {
encoding: LazyRawAnyEExpressionKind<'top>,
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawAnyEExpressionKind<'top> {
Text_1_1(TextEExpression_1_1<'top>),
Binary_1_1(&'top BinaryEExpression_1_1<'top>),
}
impl<'top> LazyRawAnyEExpression<'top> {
pub fn kind(&self) -> LazyRawAnyEExpressionKind<'top> {
self.encoding
}
pub fn encoding(&self) -> IonEncoding {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(_) => TextEncoding_1_1.encoding(),
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
}
}
}
impl<'top> From<TextEExpression_1_1<'top>> for LazyRawAnyEExpression<'top> {
fn from(text_invocation: TextEExpression_1_1<'top>) -> Self {
LazyRawAnyEExpression {
encoding: LazyRawAnyEExpressionKind::Text_1_1(text_invocation),
}
}
}
impl<'top> From<&'top BinaryEExpression_1_1<'top>> for LazyRawAnyEExpression<'top> {
fn from(binary_invocation: &'top BinaryEExpression_1_1<'top>) -> Self {
LazyRawAnyEExpression {
encoding: LazyRawAnyEExpressionKind::Binary_1_1(binary_invocation),
}
}
}
impl<'top> HasSpan<'top> for LazyRawAnyEExpression<'top> {
fn span(&self) -> Span<'top> {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(m) => m.span(),
Binary_1_1(m) => m.span(),
}
}
}
impl HasRange for LazyRawAnyEExpression<'_> {
fn range(&self) -> Range<usize> {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(m) => m.range(),
Binary_1_1(m) => m.range(),
}
}
}
impl<'top> RawEExpression<'top, AnyEncoding> for LazyRawAnyEExpression<'top> {
type RawArgumentsIterator = AnyEExpArgsIterator<'top>;
type ArgGroup = AnyEExpArgGroup<'top>;
fn id(self) -> MacroIdRef<'top> {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(ref m) => m.id(),
Binary_1_1(m) => m.id(),
}
}
fn raw_arguments(&self) -> Self::RawArgumentsIterator {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(e) => AnyEExpArgsIterator {
encoding: LazyRawAnyEExpArgsIteratorKind::Text_1_1(e.raw_arguments()),
},
Binary_1_1(e) => AnyEExpArgsIterator {
encoding: LazyRawAnyEExpArgsIteratorKind::Binary_1_1(e.raw_arguments()),
},
}
}
fn context(&self) -> EncodingContextRef<'top> {
use LazyRawAnyEExpressionKind::*;
match self.encoding {
Text_1_1(e) => e.context(),
Binary_1_1(e) => e.context(),
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct AnyEExpArgGroup<'top> {
kind: AnyEExpArgGroupKind<'top>,
}
impl<'a> AnyEExpArgGroup<'a> {
pub fn kind(&self) -> AnyEExpArgGroupKind<'a> {
self.kind
}
}
#[derive(Copy, Clone, Debug)]
pub enum AnyEExpArgGroupKind<'top> {
Text_1_1(TextEExpArgGroup<'top>),
Binary_1_1(BinaryEExpArgGroup<'top>),
}
impl AnyEExpArgGroupKind<'_> {
#[allow(dead_code)] fn encoding(&self) -> &ParameterEncoding {
match self {
AnyEExpArgGroupKind::Text_1_1(g) => g.encoding(),
AnyEExpArgGroupKind::Binary_1_1(g) => g.encoding(),
}
}
}
impl HasRange for AnyEExpArgGroup<'_> {
fn range(&self) -> Range<usize> {
match self.kind {
AnyEExpArgGroupKind::Text_1_1(group) => group.range(),
AnyEExpArgGroupKind::Binary_1_1(group) => group.range(),
}
}
}
impl<'top> HasSpan<'top> for AnyEExpArgGroup<'top> {
fn span(&self) -> Span<'top> {
match self.kind {
AnyEExpArgGroupKind::Text_1_1(group) => group.span(),
AnyEExpArgGroupKind::Binary_1_1(group) => group.span(),
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct AnyEExpArgGroupIterator<'top> {
kind: AnyEExpArgGroupIteratorKind<'top>,
}
impl<
'top,
D: Decoder<Value<'top> = LazyRawAnyValue<'top>, EExp<'top> = LazyRawAnyEExpression<'top>>,
> IsExhaustedIterator<'top, D> for AnyEExpArgGroupIterator<'top>
{
fn is_exhausted(&self) -> bool {
match self.kind {
AnyEExpArgGroupIteratorKind::Text_1_1(ref i) => i.is_exhausted(),
AnyEExpArgGroupIteratorKind::Binary_1_1(ref i) => i.is_exhausted(),
}
}
}
impl<'top> IntoIterator for AnyEExpArgGroup<'top> {
type Item = IonResult<LazyRawValueExpr<'top, AnyEncoding>>;
type IntoIter = AnyEExpArgGroupIterator<'top>;
fn into_iter(self) -> Self::IntoIter {
match self.kind {
AnyEExpArgGroupKind::Text_1_1(group) => AnyEExpArgGroupIterator {
kind: AnyEExpArgGroupIteratorKind::Text_1_1(group.into_iter()),
},
AnyEExpArgGroupKind::Binary_1_1(group) => AnyEExpArgGroupIterator {
kind: AnyEExpArgGroupIteratorKind::Binary_1_1(group.into_iter()),
},
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum AnyEExpArgGroupIteratorKind<'top> {
Text_1_1(TextEExpArgGroupIterator<'top>),
Binary_1_1(BinaryEExpArgGroupIterator<'top>),
}
impl<'top> Iterator for AnyEExpArgGroupIterator<'top> {
type Item = IonResult<LazyRawValueExpr<'top, AnyEncoding>>;
fn next(&mut self) -> Option<Self::Item> {
match self.kind {
AnyEExpArgGroupIteratorKind::Text_1_1(ref mut i) => {
Some(Ok(try_next!(i.next()).into()))
}
AnyEExpArgGroupIteratorKind::Binary_1_1(ref mut i) => {
Some(Ok(try_next!(i.next()).into()))
}
}
}
}
impl<'top> EExpressionArgGroup<'top, AnyEncoding> for AnyEExpArgGroup<'top> {
type Iterator = AnyEExpArgGroupIterator<'top>;
fn encoding(&self) -> &ParameterEncoding {
self.kind.encoding()
}
fn resolve(self, context: EncodingContextRef<'top>) -> EExpArgGroup<'top, AnyEncoding> {
EExpArgGroup::new(self, context)
}
}
#[derive(Copy, Clone, Debug)]
pub enum LazyRawAnyEExpArgsIteratorKind<'top> {
Text_1_1(
<TextEExpression_1_1<'top> as RawEExpression<
'top,
TextEncoding_1_1,
>>::RawArgumentsIterator,
),
Binary_1_1(
<&'top BinaryEExpression_1_1<'top> as RawEExpression<
'top,
BinaryEncoding_1_1,
>>::RawArgumentsIterator,
),
}
#[derive(Copy, Clone, Debug)]
pub struct AnyEExpArgsIterator<'top> {
encoding: LazyRawAnyEExpArgsIteratorKind<'top>,
}
impl<'top> Iterator for AnyEExpArgsIterator<'top> {
type Item = IonResult<EExpArg<'top, AnyEncoding>>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.encoding {
LazyRawAnyEExpArgsIteratorKind::Text_1_1(ref mut iter) => {
let arg = try_next!(iter.next());
use EExpArgExpr::*;
let any_expr = match arg.expr() {
ValueLiteral(v) => ValueLiteral(LazyRawAnyValue::from(*v)),
EExp(e) => EExp(LazyRawAnyEExpression::from(*e)),
ArgGroup(g) => ArgGroup(AnyEExpArgGroup {
kind: AnyEExpArgGroupKind::Text_1_1(*g),
}),
};
Some(Ok(EExpArg::new(arg.encoding(), any_expr)))
}
LazyRawAnyEExpArgsIteratorKind::Binary_1_1(ref mut iter) => {
let arg = try_next!(iter.next());
use EExpArgExpr::*;
let any_expr = match arg.expr() {
ValueLiteral(v) => ValueLiteral(LazyRawAnyValue::from(*v)),
EExp(e) => EExp(LazyRawAnyEExpression::from(*e)),
ArgGroup(g) => ArgGroup(AnyEExpArgGroup {
kind: AnyEExpArgGroupKind::Binary_1_1(*g),
}),
};
Some(Ok(EExpArg::new(arg.encoding(), any_expr)))
}
}
}
}
pub struct LazyRawAnyReader<'data> {
new_encoding: Option<IonEncoding>,
encoding_reader: RawReaderKind<'data>,
}
impl LazyRawAnyReader<'_> {
fn detect_encoding(data: &[u8]) -> IonEncoding {
match *data {
[0xE0, 0x01, 0x00, 0xEA, ..] => IonEncoding::Binary_1_0,
[0xE0, 0x01, 0x01, 0xEA, ..] => IonEncoding::Binary_1_1,
_ => IonEncoding::Text_1_0,
}
}
}
impl<'data> From<RawReaderKind<'data>> for LazyRawAnyReader<'data> {
fn from(encoding: RawReaderKind<'data>) -> Self {
Self {
new_encoding: None,
encoding_reader: encoding,
}
}
}
pub enum RawReaderKind<'data> {
Text_1_0(LazyRawTextReader_1_0<'data>),
Binary_1_0(LazyRawBinaryReader_1_0<'data>),
Text_1_1(LazyRawTextReader_1_1<'data>),
Binary_1_1(LazyRawBinaryReader_1_1<'data>),
}
impl<'data> RawReaderKind<'data> {
fn resume_at_offset(
context: EncodingContextRef<'data>,
saved_state: RawReaderState<'data>,
) -> RawReaderKind<'data> {
use IonEncoding::*;
match saved_state.encoding() {
Text_1_0 => {
RawReaderKind::Text_1_0(LazyRawTextReader_1_0::resume(context, saved_state))
}
Binary_1_0 => {
RawReaderKind::Binary_1_0(LazyRawBinaryReader_1_0::resume(context, saved_state))
}
Text_1_1 => {
RawReaderKind::Text_1_1(LazyRawTextReader_1_1::resume(context, saved_state))
}
Binary_1_1 => {
RawReaderKind::Binary_1_1(LazyRawBinaryReader_1_1::resume(context, saved_state))
}
}
}
fn context(&self) -> EncodingContextRef<'data> {
match self {
RawReaderKind::Text_1_0(r) => r.context(),
RawReaderKind::Binary_1_0(r) => r.context(),
RawReaderKind::Text_1_1(r) => r.context(),
RawReaderKind::Binary_1_1(r) => r.context(),
}
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
#[non_exhaustive]
pub enum IonEncoding {
#[default]
Text_1_0,
Binary_1_0,
Text_1_1,
Binary_1_1,
}
impl IonEncoding {
pub fn is_text(&self) -> bool {
use IonEncoding::*;
matches!(*self, Text_1_0 | Text_1_1)
}
pub fn is_binary(&self) -> bool {
use IonEncoding::*;
matches!(*self, Binary_1_0 | Binary_1_1)
}
pub fn name(&self) -> &str {
use IonEncoding::*;
match self {
Text_1_0 => TextEncoding_1_0::name(),
Binary_1_0 => BinaryEncoding_1_0::name(),
Text_1_1 => TextEncoding_1_1::name(),
Binary_1_1 => BinaryEncoding_1_1::name(),
}
}
pub fn version(&self) -> IonVersion {
use IonEncoding::*;
match self {
Text_1_0 | Binary_1_0 => IonVersion::v1_0,
Text_1_1 | Binary_1_1 => IonVersion::v1_1,
}
}
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum IonVersion {
#[default]
v1_0,
v1_1,
}
impl IonVersion {
pub fn major_minor(&self) -> (u8, u8) {
use IonVersion::*;
match self {
v1_0 => (1, 0),
v1_1 => (1, 1),
}
}
pub fn system_symbol_table(&self) -> &'static SystemSymbolTable {
match self {
IonVersion::v1_0 => SYSTEM_SYMBOLS_1_0,
IonVersion::v1_1 => SYSTEM_SYMBOLS_1_1,
}
}
}
impl<'data> From<LazyRawTextReader_1_0<'data>> for LazyRawAnyReader<'data> {
fn from(reader: LazyRawTextReader_1_0<'data>) -> Self {
RawReaderKind::Text_1_0(reader).into()
}
}
impl<'data> From<LazyRawTextReader_1_1<'data>> for LazyRawAnyReader<'data> {
fn from(reader: LazyRawTextReader_1_1<'data>) -> Self {
RawReaderKind::Text_1_1(reader).into()
}
}
impl<'data> From<LazyRawBinaryReader_1_0<'data>> for LazyRawAnyReader<'data> {
fn from(reader: LazyRawBinaryReader_1_0<'data>) -> Self {
RawReaderKind::Binary_1_0(reader).into()
}
}
impl<'data> From<LazyRawBinaryReader_1_1<'data>> for LazyRawAnyReader<'data> {
fn from(reader: LazyRawBinaryReader_1_1<'data>) -> Self {
RawReaderKind::Binary_1_1(reader).into()
}
}
impl<'data> LazyRawReader<'data, AnyEncoding> for LazyRawAnyReader<'data> {
fn new(context: EncodingContextRef<'data>, data: &'data [u8], is_final_data: bool) -> Self {
let encoding = Self::detect_encoding(data);
let state = RawReaderState::new(data, 0, is_final_data, encoding);
LazyRawAnyReader {
new_encoding: None,
encoding_reader: RawReaderKind::resume_at_offset(context, state),
}
}
fn resume(context: EncodingContextRef<'data>, mut saved_state: RawReaderState<'data>) -> Self {
let offset = saved_state.offset();
let data = saved_state.data();
if offset == 0 {
saved_state.set_encoding(Self::detect_encoding(data));
}
match saved_state.encoding() {
IonEncoding::Text_1_0 => LazyRawTextReader_1_0::resume(context, saved_state).into(),
IonEncoding::Binary_1_0 => LazyRawBinaryReader_1_0::resume(context, saved_state).into(),
IonEncoding::Text_1_1 => LazyRawTextReader_1_1::resume(context, saved_state).into(),
IonEncoding::Binary_1_1 => LazyRawBinaryReader_1_1::resume(context, saved_state).into(),
}
}
fn save_state(&self) -> RawReaderState<'data> {
use RawReaderKind::*;
let reader_state = match &self.encoding_reader {
Text_1_0(r) => r.save_state(),
Binary_1_0(r) => r.save_state(),
Text_1_1(r) => r.save_state(),
Binary_1_1(r) => r.save_state(),
};
if let Some(new_encoding) = self.new_encoding {
return RawReaderState::new(
reader_state.data(),
reader_state.offset(),
reader_state.is_final_data(),
new_encoding,
);
}
reader_state
}
fn next(&mut self) -> IonResult<LazyRawStreamItem<'data, AnyEncoding>> {
if let Some(new_encoding) = self.new_encoding.take() {
let mut reader_state = self.save_state();
reader_state.set_encoding(new_encoding);
let new_encoding_reader =
RawReaderKind::resume_at_offset(self.encoding_reader.context(), reader_state);
self.encoding_reader = new_encoding_reader;
}
use RawReaderKind::*;
let item: LazyRawStreamItem<'_, AnyEncoding> = match &mut self.encoding_reader {
Text_1_0(r) => r.next()?.into(),
Binary_1_0(r) => r.next()?.into(),
Text_1_1(r) => r.next()?.into(),
Binary_1_1(r) => r.next()?.into(),
};
if let RawStreamItem::VersionMarker(ivm) = item {
let ivm_old_encoding = ivm.stream_encoding_before_marker();
let ivm_new_encoding = ivm.stream_encoding_after_marker()?;
if ivm_new_encoding != ivm_old_encoding {
self.new_encoding = Some(ivm_new_encoding);
}
}
Ok(item)
}
fn position(&self) -> usize {
use RawReaderKind::*;
match &self.encoding_reader {
Text_1_0(r) => r.position(),
Binary_1_0(r) => r.position(),
Text_1_1(r) => r.position(),
Binary_1_1(r) => r.position(),
}
}
fn encoding(&self) -> IonEncoding {
use RawReaderKind::*;
if let Some(new_encoding) = self.new_encoding {
return new_encoding;
}
match &self.encoding_reader {
Text_1_0(_) => IonEncoding::Text_1_0,
Binary_1_0(_) => IonEncoding::Binary_1_0,
Text_1_1(_) => IonEncoding::Text_1_1,
Binary_1_1(_) => IonEncoding::Binary_1_1,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyValue<'top> {
encoding: LazyRawValueKind<'top>,
}
impl<'top> LazyRawAnyValue<'top> {
pub fn kind(&self) -> LazyRawValueKind<'top> {
self.encoding
}
pub fn encoding(&self) -> IonEncoding {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(_) => TextEncoding_1_0.encoding(),
Binary_1_0(_) => BinaryEncoding_1_0.encoding(),
Text_1_1(_) => TextEncoding_1_1.encoding(),
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawValueKind<'top> {
Text_1_0(LazyRawTextValue_1_0<'top>),
Binary_1_0(&'top LazyRawBinaryValue_1_0<'top>),
Text_1_1(LazyRawTextValue_1_1<'top>),
Binary_1_1(&'top LazyRawBinaryValue_1_1<'top>),
}
impl<'top> From<LazyRawTextValue_1_0<'top>> for LazyRawAnyValue<'top> {
fn from(value: LazyRawTextValue_1_0<'top>) -> Self {
LazyRawAnyValue {
encoding: LazyRawValueKind::Text_1_0(value),
}
}
}
impl<'top> From<&'top LazyRawBinaryValue_1_0<'top>> for LazyRawAnyValue<'top> {
fn from(value: &'top LazyRawBinaryValue_1_0<'top>) -> Self {
LazyRawAnyValue {
encoding: LazyRawValueKind::Binary_1_0(value),
}
}
}
impl<'top> From<LazyRawTextValue_1_1<'top>> for LazyRawAnyValue<'top> {
fn from(value: LazyRawTextValue_1_1<'top>) -> Self {
LazyRawAnyValue {
encoding: LazyRawValueKind::Text_1_1(value),
}
}
}
impl<'top> From<&'top LazyRawBinaryValue_1_1<'top>> for LazyRawAnyValue<'top> {
fn from(value: &'top LazyRawBinaryValue_1_1<'top>) -> Self {
LazyRawAnyValue {
encoding: LazyRawValueKind::Binary_1_1(value),
}
}
}
impl<'top> From<LazyRawValueExpr<'top, TextEncoding_1_0>> for LazyRawValueExpr<'top, AnyEncoding> {
fn from(value: LazyRawValueExpr<'top, TextEncoding_1_0>) -> Self {
match value {
RawValueExpr::ValueLiteral(v) => RawValueExpr::ValueLiteral(v.into()),
RawValueExpr::EExp(_) => unreachable!("macro invocation in text Ion 1.0"),
}
}
}
impl<'top> From<LazyRawValueExpr<'top, BinaryEncoding_1_0>>
for LazyRawValueExpr<'top, AnyEncoding>
{
fn from(value: LazyRawValueExpr<'top, BinaryEncoding_1_0>) -> Self {
match value {
RawValueExpr::ValueLiteral(v) => RawValueExpr::ValueLiteral(v.into()),
RawValueExpr::EExp(_) => unreachable!("macro invocation in binary Ion 1.0"),
}
}
}
impl<'top> From<LazyRawValueExpr<'top, TextEncoding_1_1>> for LazyRawValueExpr<'top, AnyEncoding> {
fn from(value: LazyRawValueExpr<'top, TextEncoding_1_1>) -> Self {
match value {
RawValueExpr::ValueLiteral(v) => RawValueExpr::ValueLiteral(v.into()),
RawValueExpr::EExp(m) => {
let invocation = LazyRawAnyEExpression {
encoding: LazyRawAnyEExpressionKind::Text_1_1(m),
};
RawValueExpr::EExp(invocation)
}
}
}
}
impl<'top> From<LazyRawValueExpr<'top, BinaryEncoding_1_1>>
for LazyRawValueExpr<'top, AnyEncoding>
{
fn from(value: LazyRawValueExpr<'top, BinaryEncoding_1_1>) -> Self {
match value {
RawValueExpr::ValueLiteral(v) => RawValueExpr::ValueLiteral(v.into()),
RawValueExpr::EExp(m) => {
let invocation = LazyRawAnyEExpression {
encoding: LazyRawAnyEExpressionKind::Binary_1_1(m),
};
RawValueExpr::EExp(invocation)
}
}
}
}
impl<'top> From<RawValueRef<'top, TextEncoding_1_0>> for RawValueRef<'top, AnyEncoding> {
fn from(value: RawValueRef<'top, TextEncoding_1_0>) -> Self {
use RawValueRef::*;
match value {
Null(ion_type) => Null(ion_type),
Bool(value) => Bool(value),
Int(value) => Int(value),
Float(value) => Float(value),
Decimal(value) => Decimal(value),
Timestamp(value) => Timestamp(value),
String(value) => String(value),
Symbol(value) => Symbol(value),
Blob(value) => Blob(value),
Clob(value) => Clob(value),
SExp(value) => SExp(value.into()),
List(value) => List(value.into()),
Struct(value) => Struct(value.into()),
}
}
}
impl<'top> From<RawValueRef<'top, BinaryEncoding_1_0>> for RawValueRef<'top, AnyEncoding> {
fn from(value: RawValueRef<'top, BinaryEncoding_1_0>) -> Self {
use RawValueRef::*;
match value {
Null(ion_type) => Null(ion_type),
Bool(value) => Bool(value),
Int(value) => Int(value),
Float(value) => Float(value),
Decimal(value) => Decimal(value),
Timestamp(value) => Timestamp(value),
String(value) => String(value),
Symbol(value) => Symbol(value),
Blob(value) => Blob(value),
Clob(value) => Clob(value),
SExp(value) => SExp(value.into()),
List(value) => List(value.into()),
Struct(value) => Struct(value.into()),
}
}
}
impl<'top> From<RawValueRef<'top, TextEncoding_1_1>> for RawValueRef<'top, AnyEncoding> {
fn from(value: RawValueRef<'top, TextEncoding_1_1>) -> Self {
use RawValueRef::*;
match value {
Null(ion_type) => Null(ion_type),
Bool(value) => Bool(value),
Int(value) => Int(value),
Float(value) => Float(value),
Decimal(value) => Decimal(value),
Timestamp(value) => Timestamp(value),
String(value) => String(value),
Symbol(value) => Symbol(value),
Blob(value) => Blob(value),
Clob(value) => Clob(value),
SExp(value) => SExp(value.into()),
List(value) => List(value.into()),
Struct(value) => Struct(value.into()),
}
}
}
impl<'top> From<RawValueRef<'top, BinaryEncoding_1_1>> for RawValueRef<'top, AnyEncoding> {
fn from(value: RawValueRef<'top, BinaryEncoding_1_1>) -> Self {
use RawValueRef::*;
match value {
Null(ion_type) => Null(ion_type),
Bool(value) => Bool(value),
Int(value) => Int(value),
Float(value) => Float(value),
Decimal(value) => Decimal(value),
Timestamp(value) => Timestamp(value),
String(value) => String(value),
Symbol(value) => Symbol(value),
Blob(value) => Blob(value),
Clob(value) => Clob(value),
SExp(value) => SExp(value.into()),
List(value) => List(value.into()),
Struct(value) => Struct(value.into()),
}
}
}
impl<'top> From<LazyRawStreamItem<'top, TextEncoding_1_0>>
for LazyRawStreamItem<'top, AnyEncoding>
{
fn from(value: LazyRawStreamItem<'top, TextEncoding_1_0>) -> Self {
match value {
LazyRawStreamItem::<TextEncoding_1_0>::VersionMarker(marker) => {
LazyRawStreamItem::<AnyEncoding>::VersionMarker(marker.into())
}
LazyRawStreamItem::<TextEncoding_1_0>::Value(value) => {
LazyRawStreamItem::<AnyEncoding>::Value(value.into())
}
LazyRawStreamItem::<TextEncoding_1_0>::EExp(_) => {
unreachable!("Ion 1.0 does not support macro invocations")
}
LazyRawStreamItem::<TextEncoding_1_0>::EndOfStream(end) => {
LazyRawStreamItem::<AnyEncoding>::EndOfStream(end)
}
}
}
}
impl<'top> From<LazyRawStreamItem<'top, BinaryEncoding_1_0>>
for LazyRawStreamItem<'top, AnyEncoding>
{
fn from(value: LazyRawStreamItem<'top, BinaryEncoding_1_0>) -> Self {
match value {
LazyRawStreamItem::<BinaryEncoding_1_0>::VersionMarker(marker) => {
LazyRawStreamItem::<AnyEncoding>::VersionMarker(marker.into())
}
LazyRawStreamItem::<BinaryEncoding_1_0>::Value(value) => {
LazyRawStreamItem::<AnyEncoding>::Value(value.into())
}
LazyRawStreamItem::<BinaryEncoding_1_0>::EExp(_) => {
unreachable!("Ion 1.0 does not support macro invocations")
}
LazyRawStreamItem::<BinaryEncoding_1_0>::EndOfStream(end) => {
LazyRawStreamItem::<AnyEncoding>::EndOfStream(end)
}
}
}
}
impl<'top> From<LazyRawStreamItem<'top, TextEncoding_1_1>>
for LazyRawStreamItem<'top, AnyEncoding>
{
fn from(value: LazyRawStreamItem<'top, TextEncoding_1_1>) -> Self {
match value {
LazyRawStreamItem::<TextEncoding_1_1>::VersionMarker(marker) => {
LazyRawStreamItem::<AnyEncoding>::VersionMarker(marker.into())
}
LazyRawStreamItem::<TextEncoding_1_1>::Value(value) => {
LazyRawStreamItem::<AnyEncoding>::Value(value.into())
}
LazyRawStreamItem::<TextEncoding_1_1>::EExp(invocation) => {
LazyRawStreamItem::<AnyEncoding>::EExp(LazyRawAnyEExpression {
encoding: LazyRawAnyEExpressionKind::Text_1_1(invocation),
})
}
LazyRawStreamItem::<TextEncoding_1_1>::EndOfStream(end) => {
LazyRawStreamItem::<AnyEncoding>::EndOfStream(end)
}
}
}
}
impl<'top> From<LazyRawStreamItem<'top, BinaryEncoding_1_1>>
for LazyRawStreamItem<'top, AnyEncoding>
{
fn from(value: LazyRawStreamItem<'top, BinaryEncoding_1_1>) -> Self {
match value {
LazyRawStreamItem::<BinaryEncoding_1_1>::VersionMarker(marker) => {
LazyRawStreamItem::<AnyEncoding>::VersionMarker(marker.into())
}
LazyRawStreamItem::<BinaryEncoding_1_1>::Value(value) => {
LazyRawStreamItem::<AnyEncoding>::Value(value.into())
}
LazyRawStreamItem::<BinaryEncoding_1_1>::EExp(eexp) => {
LazyRawStreamItem::<AnyEncoding>::EExp(eexp.into())
}
LazyRawStreamItem::<BinaryEncoding_1_1>::EndOfStream(end) => {
LazyRawStreamItem::<AnyEncoding>::EndOfStream(end)
}
}
}
}
impl<'top> HasSpan<'top> for LazyRawAnyValue<'top> {
fn span(&self) -> Span<'top> {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.span(),
Binary_1_0(v) => v.span(),
Text_1_1(v) => v.span(),
Binary_1_1(v) => v.span(),
}
}
}
impl HasRange for LazyRawAnyValue<'_> {
fn range(&self) -> Range<usize> {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.range(),
Binary_1_0(v) => v.range(),
Text_1_1(v) => v.range(),
Binary_1_1(v) => v.range(),
}
}
}
impl<'top> LazyRawValue<'top, AnyEncoding> for LazyRawAnyValue<'top> {
fn ion_type(&self) -> IonType {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.ion_type(),
Binary_1_0(v) => v.ion_type(),
Text_1_1(v) => v.ion_type(),
Binary_1_1(v) => v.ion_type(),
}
}
fn is_null(&self) -> bool {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.is_null(),
Binary_1_0(v) => v.is_null(),
Text_1_1(v) => v.is_null(),
Binary_1_1(v) => v.is_null(),
}
}
fn is_delimited(&self) -> bool {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.is_delimited(),
Binary_1_0(v) => v.is_delimited(),
Text_1_1(v) => v.is_delimited(),
Binary_1_1(v) => v.is_delimited(),
}
}
fn has_annotations(&self) -> bool {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => v.has_annotations(),
Binary_1_0(v) => v.has_annotations(),
Text_1_1(v) => v.has_annotations(),
Binary_1_1(v) => v.has_annotations(),
}
}
fn annotations(&self) -> RawAnyAnnotationsIterator<'top> {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Text_1_0(v.annotations()),
},
Binary_1_0(v) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Binary_1_0(v.annotations()),
},
Text_1_1(v) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Text_1_1(v.annotations()),
},
Binary_1_1(v) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Binary_1_1(v.annotations()),
},
}
}
fn read(&self) -> IonResult<RawValueRef<'top, AnyEncoding>> {
use LazyRawValueKind::*;
match &self.encoding {
Text_1_0(v) => Ok(v.read()?.into()),
Binary_1_0(v) => Ok(v.read()?.into()),
Text_1_1(v) => Ok(v.read()?.into()),
Binary_1_1(v) => Ok(v.read()?.into()),
}
}
fn annotations_span(&self) -> Span<'top> {
match &self.encoding {
LazyRawValueKind::Text_1_0(v) => v.annotations_span(),
LazyRawValueKind::Binary_1_0(v) => v.annotations_span(),
LazyRawValueKind::Text_1_1(v) => v.annotations_span(),
LazyRawValueKind::Binary_1_1(v) => v.annotations_span(),
}
}
fn value_span(&self) -> Span<'top> {
match &self.encoding {
LazyRawValueKind::Text_1_0(v) => v.value_span(),
LazyRawValueKind::Binary_1_0(v) => v.value_span(),
LazyRawValueKind::Text_1_1(v) => v.value_span(),
LazyRawValueKind::Binary_1_1(v) => v.value_span(),
}
}
fn with_backing_data(&self, span: Span<'top>) -> Self {
Self {
encoding: match &self.encoding {
LazyRawValueKind::Text_1_0(v) => {
LazyRawValueKind::Text_1_0(v.with_backing_data(span))
}
LazyRawValueKind::Binary_1_0(v) => {
LazyRawValueKind::Binary_1_0(v.with_backing_data(span))
}
LazyRawValueKind::Text_1_1(v) => {
LazyRawValueKind::Text_1_1(v.with_backing_data(span))
}
LazyRawValueKind::Binary_1_1(v) => {
LazyRawValueKind::Binary_1_1(v.with_backing_data(span))
}
},
}
}
fn encoding(&self) -> IonEncoding {
match self.encoding {
LazyRawValueKind::Text_1_0(_) => IonEncoding::Text_1_0,
LazyRawValueKind::Binary_1_0(_) => IonEncoding::Binary_1_0,
LazyRawValueKind::Text_1_1(_) => IonEncoding::Text_1_1,
LazyRawValueKind::Binary_1_1(_) => IonEncoding::Binary_1_1,
}
}
}
pub struct RawAnyAnnotationsIterator<'top> {
encoding: RawAnnotationsIteratorKind<'top>,
}
pub enum RawAnnotationsIteratorKind<'top> {
Text_1_0(RawTextAnnotationsIterator<'top>),
Binary_1_0(RawBinaryAnnotationsIterator_1_0<'top>),
Text_1_1(RawTextAnnotationsIterator<'top>),
Binary_1_1(RawBinaryAnnotationsIterator_1_1<'top>),
}
impl<'top> Iterator for RawAnyAnnotationsIterator<'top> {
type Item = IonResult<RawSymbolRef<'top>>;
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(),
RawAnnotationsIteratorKind::Text_1_1(i) => i.next(),
RawAnnotationsIteratorKind::Binary_1_1(i) => i.next(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyList<'top> {
encoding: LazyRawListKind<'top>,
}
impl<'top> LazyRawAnyList<'top> {
pub fn as_value(&self) -> LazyRawAnyValue<'top> {
use LazyRawListKind::*;
match self.encoding {
Text_1_0(l) => l.as_value().into(),
Binary_1_0(l) => l.as_value().into(),
Text_1_1(l) => l.as_value().into(),
Binary_1_1(l) => l.as_value().into(),
}
}
}
impl<'top> LazyRawAnyList<'top> {
pub fn kind(&self) -> LazyRawListKind<'top> {
self.encoding
}
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawListKind<'top> {
Text_1_0(RawTextList<'top, TextEncoding_1_0>),
Binary_1_0(LazyRawBinaryList_1_0<'top>),
Text_1_1(RawTextList<'top, TextEncoding_1_1>),
Binary_1_1(LazyRawBinaryList_1_1<'top>),
}
impl<'top> LazyContainerPrivate<'top, AnyEncoding> for LazyRawAnyList<'top> {
fn from_value(value: LazyRawAnyValue<'top>) -> Self {
use LazyRawValueKind::*;
match value.encoding {
Text_1_0(v) => LazyRawAnyList {
encoding: LazyRawListKind::Text_1_0(RawTextList::from_value(v)),
},
Binary_1_0(v) => LazyRawAnyList {
encoding: LazyRawListKind::Binary_1_0(LazyRawBinaryList_1_0::from_value(v)),
},
Text_1_1(v) => LazyRawAnyList {
encoding: LazyRawListKind::Text_1_1(RawTextList::from_value(v)),
},
Binary_1_1(v) => LazyRawAnyList {
encoding: LazyRawListKind::Binary_1_1(LazyRawBinaryList_1_1::from_value(v)),
},
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct RawAnyListIterator<'data> {
encoding: RawAnyListIteratorKind<'data>,
}
#[derive(Debug, Copy, Clone)]
pub enum RawAnyListIteratorKind<'data> {
Text_1_0(RawTextSequenceCacheIterator<'data, TextEncoding_1_0>),
Binary_1_0(RawBinarySequenceIterator_1_0<'data>),
Text_1_1(RawTextSequenceCacheIterator<'data, TextEncoding_1_1>),
Binary_1_1(RawBinarySequenceIterator_1_1<'data>),
}
impl<'data> Iterator for RawAnyListIterator<'data> {
type Item = IonResult<LazyRawValueExpr<'data, AnyEncoding>>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.encoding {
RawAnyListIteratorKind::Text_1_0(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnyListIteratorKind::Binary_1_0(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnyListIteratorKind::Text_1_1(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnyListIteratorKind::Binary_1_1(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
}
}
}
impl<'top> LazyRawContainer<'top, AnyEncoding> for LazyRawAnyList<'top> {
fn as_value(&self) -> <AnyEncoding as Decoder>::Value<'top> {
match &self.encoding {
LazyRawListKind::Text_1_0(s) => s.as_value().into(),
LazyRawListKind::Binary_1_0(s) => s.as_value().into(),
LazyRawListKind::Text_1_1(s) => s.as_value().into(),
LazyRawListKind::Binary_1_1(s) => s.as_value().into(),
}
}
}
impl<'top> LazyRawSequence<'top, AnyEncoding> for LazyRawAnyList<'top> {
type Iterator = RawAnyListIterator<'top>;
fn annotations(&self) -> <AnyEncoding as Decoder>::AnnotationsIterator<'top> {
self.as_value().annotations()
}
fn ion_type(&self) -> IonType {
match &self.encoding {
LazyRawListKind::Text_1_0(s) => s.ion_type(),
LazyRawListKind::Binary_1_0(s) => s.ion_type(),
LazyRawListKind::Text_1_1(s) => s.ion_type(),
LazyRawListKind::Binary_1_1(s) => s.ion_type(),
}
}
fn iter(&self) -> Self::Iterator {
match &self.encoding {
LazyRawListKind::Text_1_0(s) => RawAnyListIterator {
encoding: RawAnyListIteratorKind::Text_1_0(s.iter()),
},
LazyRawListKind::Binary_1_0(s) => RawAnyListIterator {
encoding: RawAnyListIteratorKind::Binary_1_0(s.iter()),
},
LazyRawListKind::Text_1_1(s) => RawAnyListIterator {
encoding: RawAnyListIteratorKind::Text_1_1(s.iter()),
},
LazyRawListKind::Binary_1_1(s) => RawAnyListIterator {
encoding: RawAnyListIteratorKind::Binary_1_1(s.iter()),
},
}
}
}
impl<'data> From<RawTextList<'data, TextEncoding_1_0>> for LazyRawAnyList<'data> {
fn from(value: RawTextList<'data, TextEncoding_1_0>) -> Self {
LazyRawAnyList {
encoding: LazyRawListKind::Text_1_0(value),
}
}
}
impl<'data> From<LazyRawBinaryList_1_0<'data>> for LazyRawAnyList<'data> {
fn from(value: LazyRawBinaryList_1_0<'data>) -> Self {
LazyRawAnyList {
encoding: LazyRawListKind::Binary_1_0(value),
}
}
}
impl<'data> From<RawTextList<'data, TextEncoding_1_1>> for LazyRawAnyList<'data> {
fn from(value: RawTextList<'data, TextEncoding_1_1>) -> Self {
LazyRawAnyList {
encoding: LazyRawListKind::Text_1_1(value),
}
}
}
impl<'data> From<LazyRawBinaryList_1_1<'data>> for LazyRawAnyList<'data> {
fn from(value: LazyRawBinaryList_1_1<'data>) -> Self {
LazyRawAnyList {
encoding: LazyRawListKind::Binary_1_1(value),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnySExp<'data> {
encoding: LazyRawSExpKind<'data>,
}
impl<'top> LazyRawAnySExp<'top> {
pub fn kind(&self) -> LazyRawSExpKind<'top> {
self.encoding
}
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawSExpKind<'data> {
Text_1_0(RawTextSExp<'data, TextEncoding_1_0>),
Binary_1_0(LazyRawBinarySExp_1_0<'data>),
Text_1_1(RawTextSExp<'data, TextEncoding_1_1>),
Binary_1_1(LazyRawBinarySExp_1_1<'data>),
}
impl<'top> LazyRawContainer<'top, AnyEncoding> for LazyRawAnySExp<'top> {
fn as_value(&self) -> <AnyEncoding as Decoder>::Value<'top> {
use LazyRawSExpKind::*;
match self.encoding {
Text_1_0(s) => s.as_value().into(),
Binary_1_0(s) => s.as_value().into(),
Text_1_1(s) => s.as_value().into(),
Binary_1_1(s) => s.as_value().into(),
}
}
}
impl<'data> LazyContainerPrivate<'data, AnyEncoding> for LazyRawAnySExp<'data> {
fn from_value(value: LazyRawAnyValue<'data>) -> Self {
match value.encoding {
LazyRawValueKind::Text_1_0(v) => LazyRawAnySExp {
encoding: LazyRawSExpKind::Text_1_0(RawTextSExp::from_value(v)),
},
LazyRawValueKind::Binary_1_0(v) => LazyRawAnySExp {
encoding: LazyRawSExpKind::Binary_1_0(LazyRawBinarySExp_1_0::from_value(v)),
},
LazyRawValueKind::Text_1_1(v) => LazyRawAnySExp {
encoding: LazyRawSExpKind::Text_1_1(RawTextSExp::from_value(v)),
},
LazyRawValueKind::Binary_1_1(v) => LazyRawAnySExp {
encoding: LazyRawSExpKind::Binary_1_1(LazyRawBinarySExp_1_1::from_value(v)),
},
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct RawAnySExpIterator<'data> {
encoding: RawAnySExpIteratorKind<'data>,
}
#[derive(Debug, Copy, Clone)]
pub enum RawAnySExpIteratorKind<'data> {
Text_1_0(RawTextSequenceCacheIterator<'data, TextEncoding_1_0>),
Binary_1_0(RawBinarySequenceIterator_1_0<'data>),
Text_1_1(RawTextSequenceCacheIterator<'data, TextEncoding_1_1>),
Binary_1_1(RawBinarySequenceIterator_1_1<'data>),
}
impl<'data> Iterator for RawAnySExpIterator<'data> {
type Item = IonResult<LazyRawValueExpr<'data, AnyEncoding>>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.encoding {
RawAnySExpIteratorKind::Text_1_0(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnySExpIteratorKind::Binary_1_0(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnySExpIteratorKind::Text_1_1(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
RawAnySExpIteratorKind::Binary_1_1(i) => i
.next()
.map(|value_result| value_result.map(|value| value.into())),
}
}
}
impl<'top> LazyRawSequence<'top, AnyEncoding> for LazyRawAnySExp<'top> {
type Iterator = RawAnySExpIterator<'top>;
fn annotations(&self) -> <AnyEncoding as Decoder>::AnnotationsIterator<'top> {
self.as_value().annotations()
}
fn ion_type(&self) -> IonType {
match &self.encoding {
LazyRawSExpKind::Text_1_0(s) => s.ion_type(),
LazyRawSExpKind::Binary_1_0(s) => s.ion_type(),
LazyRawSExpKind::Text_1_1(s) => s.ion_type(),
LazyRawSExpKind::Binary_1_1(s) => s.ion_type(),
}
}
fn iter(&self) -> Self::Iterator {
match &self.encoding {
LazyRawSExpKind::Text_1_0(s) => RawAnySExpIterator {
encoding: RawAnySExpIteratorKind::Text_1_0(s.iter()),
},
LazyRawSExpKind::Binary_1_0(s) => RawAnySExpIterator {
encoding: RawAnySExpIteratorKind::Binary_1_0(s.iter()),
},
LazyRawSExpKind::Text_1_1(s) => RawAnySExpIterator {
encoding: RawAnySExpIteratorKind::Text_1_1(s.iter()),
},
LazyRawSExpKind::Binary_1_1(s) => RawAnySExpIterator {
encoding: RawAnySExpIteratorKind::Binary_1_1(s.iter()),
},
}
}
}
impl<'data> From<RawTextSExp<'data, TextEncoding_1_0>> for LazyRawAnySExp<'data> {
fn from(value: RawTextSExp<'data, TextEncoding_1_0>) -> Self {
LazyRawAnySExp {
encoding: LazyRawSExpKind::Text_1_0(value),
}
}
}
impl<'data> From<LazyRawBinarySExp_1_0<'data>> for LazyRawAnySExp<'data> {
fn from(value: LazyRawBinarySExp_1_0<'data>) -> Self {
LazyRawAnySExp {
encoding: LazyRawSExpKind::Binary_1_0(value),
}
}
}
impl<'data> From<RawTextSExp<'data, TextEncoding_1_1>> for LazyRawAnySExp<'data> {
fn from(value: RawTextSExp<'data, TextEncoding_1_1>) -> Self {
LazyRawAnySExp {
encoding: LazyRawSExpKind::Text_1_1(value),
}
}
}
impl<'data> From<LazyRawBinarySExp_1_1<'data>> for LazyRawAnySExp<'data> {
fn from(value: LazyRawBinarySExp_1_1<'data>) -> Self {
LazyRawAnySExp {
encoding: LazyRawSExpKind::Binary_1_1(value),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyStruct<'data> {
encoding: LazyRawStructKind<'data>,
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawStructKind<'data> {
Text_1_0(LazyRawTextStruct<'data, TextEncoding_1_0>),
Binary_1_0(LazyRawBinaryStruct_1_0<'data>),
Text_1_1(LazyRawTextStruct<'data, TextEncoding_1_1>),
Binary_1_1(LazyRawBinaryStruct_1_1<'data>),
}
impl<'top> LazyRawContainer<'top, AnyEncoding> for LazyRawAnyStruct<'top> {
fn as_value(&self) -> <AnyEncoding as Decoder>::Value<'top> {
match self.encoding {
LazyRawStructKind::Text_1_0(s) => s.as_value().into(),
LazyRawStructKind::Binary_1_0(s) => s.as_value().into(),
LazyRawStructKind::Text_1_1(s) => s.as_value().into(),
LazyRawStructKind::Binary_1_1(s) => s.as_value().into(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct LazyRawAnyFieldName<'data> {
encoding: LazyRawFieldNameKind<'data>,
}
#[derive(Debug, Copy, Clone)]
pub enum LazyRawFieldNameKind<'data> {
Text_1_0(LazyRawTextFieldName<'data, TextEncoding_1_0>),
Binary_1_0(LazyRawBinaryFieldName_1_0<'data>),
Text_1_1(LazyRawTextFieldName<'data, TextEncoding_1_1>),
Binary_1_1(LazyRawBinaryFieldName_1_1<'data>),
}
impl<'top> HasSpan<'top> for LazyRawAnyFieldName<'top> {
fn span(&self) -> Span<'top> {
use LazyRawFieldNameKind::*;
match self.encoding {
Text_1_0(name) => name.span(),
Binary_1_0(name) => name.span(),
Text_1_1(name) => name.span(),
Binary_1_1(name) => name.span(),
}
}
}
impl HasRange for LazyRawAnyFieldName<'_> {
fn range(&self) -> Range<usize> {
use LazyRawFieldNameKind::*;
match self.encoding {
Text_1_0(name) => name.range(),
Binary_1_0(name) => name.range(),
Text_1_1(name) => name.range(),
Binary_1_1(name) => name.range(),
}
}
}
impl<'top> LazyRawFieldName<'top, AnyEncoding> for LazyRawAnyFieldName<'top> {
fn read(&self) -> IonResult<RawSymbolRef<'top>> {
use LazyRawFieldNameKind::*;
match self.encoding {
Text_1_0(name) => name.read(),
Binary_1_0(name) => name.read(),
Text_1_1(name) => name.read(),
Binary_1_1(name) => name.read(),
}
}
}
impl<'top> From<LazyRawFieldNameKind<'top>> for LazyRawAnyFieldName<'top> {
fn from(value: LazyRawFieldNameKind<'top>) -> Self {
LazyRawAnyFieldName { encoding: value }
}
}
impl<'top> From<LazyRawTextFieldName<'top, TextEncoding_1_0>> for LazyRawAnyFieldName<'top> {
fn from(value: LazyRawTextFieldName<'top, TextEncoding_1_0>) -> Self {
LazyRawFieldNameKind::Text_1_0(value).into()
}
}
impl<'top> From<LazyRawTextFieldName<'top, TextEncoding_1_1>> for LazyRawAnyFieldName<'top> {
fn from(value: LazyRawTextFieldName<'top, TextEncoding_1_1>) -> Self {
LazyRawFieldNameKind::Text_1_1(value).into()
}
}
impl<'top> From<LazyRawBinaryFieldName_1_0<'top>> for LazyRawAnyFieldName<'top> {
fn from(value: LazyRawBinaryFieldName_1_0<'top>) -> Self {
LazyRawFieldNameKind::Binary_1_0(value).into()
}
}
impl<'top> From<LazyRawBinaryFieldName_1_1<'top>> for LazyRawAnyFieldName<'top> {
fn from(value: LazyRawBinaryFieldName_1_1<'top>) -> Self {
LazyRawFieldNameKind::Binary_1_1(value).into()
}
}
#[derive(Debug, Copy, Clone)]
pub struct RawAnyStructIterator<'data> {
encoding: RawAnyStructIteratorKind<'data>,
}
#[derive(Debug, Copy, Clone)]
pub enum RawAnyStructIteratorKind<'data> {
Text_1_0(RawTextStructCacheIterator<'data, TextEncoding_1_0>),
Binary_1_0(RawBinaryStructIterator_1_0<'data>),
Text_1_1(RawTextStructCacheIterator<'data, TextEncoding_1_1>),
Binary_1_1(RawBinaryStructIterator_1_1<'data>),
}
impl<'data> Iterator for RawAnyStructIterator<'data> {
type Item = IonResult<LazyRawFieldExpr<'data, AnyEncoding>>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.encoding {
RawAnyStructIteratorKind::Text_1_0(i) => i
.next()
.map(|field_result| field_result.map(|field| field.into())),
RawAnyStructIteratorKind::Binary_1_0(i) => i
.next()
.map(|field_result| field_result.map(|field| field.into())),
RawAnyStructIteratorKind::Text_1_1(i) => i
.next()
.map(|field_result| field_result.map(|field| field.into())),
RawAnyStructIteratorKind::Binary_1_1(i) => i
.next()
.map(|field_result| field_result.map(|field| field.into())),
}
}
}
impl<'data> From<LazyRawFieldExpr<'data, TextEncoding_1_0>>
for LazyRawFieldExpr<'data, AnyEncoding>
{
fn from(text_field: LazyRawFieldExpr<'data, TextEncoding_1_0>) -> Self {
use LazyRawFieldExpr::*;
match text_field {
NameValue(name, value) => NameValue(name.into(), value.into()),
NameEExp(_, _) => unreachable!("(name, e-exp) field in text Ion 1.0"),
EExp(_) => unreachable!("e-exp field in text Ion 1.0"),
}
}
}
impl<'data> From<LazyRawFieldExpr<'data, BinaryEncoding_1_0>>
for LazyRawFieldExpr<'data, AnyEncoding>
{
fn from(binary_field: LazyRawFieldExpr<'data, BinaryEncoding_1_0>) -> Self {
use LazyRawFieldExpr::*;
match binary_field {
NameValue(name, value) => NameValue(name.into(), value.into()),
NameEExp(_, _) => unreachable!("(name, e-exp) field in binary Ion 1.0"),
EExp(_) => unreachable!("e-exp field in binary Ion 1.0"),
}
}
}
impl<'data> From<LazyRawFieldExpr<'data, TextEncoding_1_1>>
for LazyRawFieldExpr<'data, AnyEncoding>
{
fn from(text_field: LazyRawFieldExpr<'data, TextEncoding_1_1>) -> Self {
use LazyRawFieldExpr::*;
match text_field {
NameValue(name, value) => NameValue(name.into(), value.into()),
NameEExp(name, eexp) => NameEExp(name.into(), eexp.into()),
EExp(eexp) => EExp(eexp.into()),
}
}
}
impl<'data> From<LazyRawFieldExpr<'data, BinaryEncoding_1_1>>
for LazyRawFieldExpr<'data, AnyEncoding>
{
fn from(binary_field: LazyRawFieldExpr<'data, BinaryEncoding_1_1>) -> Self {
use LazyRawFieldExpr::*;
match binary_field {
NameValue(name, value) => NameValue(name.into(), value.into()),
NameEExp(name, eexp) => NameEExp(name.into(), eexp.into()),
EExp(_) => todo!("e-exp field in binary Ion 1.1"),
}
}
}
impl<'data> LazyContainerPrivate<'data, AnyEncoding> for LazyRawAnyStruct<'data> {
fn from_value(value: LazyRawAnyValue<'data>) -> Self {
match value.encoding {
LazyRawValueKind::Text_1_0(v) => LazyRawAnyStruct {
encoding: LazyRawStructKind::Text_1_0(
LazyRawTextStruct::<TextEncoding_1_0>::from_value(v),
),
},
LazyRawValueKind::Binary_1_0(v) => LazyRawAnyStruct {
encoding: LazyRawStructKind::Binary_1_0(LazyRawBinaryStruct_1_0::from_value(v)),
},
LazyRawValueKind::Text_1_1(v) => LazyRawAnyStruct {
encoding: LazyRawStructKind::Text_1_1(
LazyRawTextStruct::<TextEncoding_1_1>::from_value(v),
),
},
LazyRawValueKind::Binary_1_1(v) => LazyRawAnyStruct {
encoding: LazyRawStructKind::Binary_1_1(LazyRawBinaryStruct_1_1::from_value(v)),
},
}
}
}
impl<'top> LazyRawStruct<'top, AnyEncoding> for LazyRawAnyStruct<'top> {
type Iterator = RawAnyStructIterator<'top>;
fn annotations(&self) -> <AnyEncoding as Decoder>::AnnotationsIterator<'top> {
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()),
},
LazyRawStructKind::Text_1_1(s) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Text_1_1(s.annotations()),
},
LazyRawStructKind::Binary_1_1(s) => RawAnyAnnotationsIterator {
encoding: RawAnnotationsIteratorKind::Binary_1_1(s.annotations()),
},
}
}
fn iter(&self) -> Self::Iterator {
match &self.encoding {
LazyRawStructKind::Text_1_0(s) => RawAnyStructIterator {
encoding: RawAnyStructIteratorKind::Text_1_0(s.iter()),
},
LazyRawStructKind::Binary_1_0(s) => RawAnyStructIterator {
encoding: RawAnyStructIteratorKind::Binary_1_0(s.iter()),
},
LazyRawStructKind::Text_1_1(s) => RawAnyStructIterator {
encoding: RawAnyStructIteratorKind::Text_1_1(s.iter()),
},
LazyRawStructKind::Binary_1_1(s) => RawAnyStructIterator {
encoding: RawAnyStructIteratorKind::Binary_1_1(s.iter()),
},
}
}
}
impl<'data> From<LazyRawTextStruct<'data, TextEncoding_1_0>> for LazyRawAnyStruct<'data> {
fn from(value: LazyRawTextStruct<'data, TextEncoding_1_0>) -> Self {
LazyRawAnyStruct {
encoding: LazyRawStructKind::Text_1_0(value),
}
}
}
impl<'data> From<LazyRawBinaryStruct_1_0<'data>> for LazyRawAnyStruct<'data> {
fn from(value: LazyRawBinaryStruct_1_0<'data>) -> Self {
LazyRawAnyStruct {
encoding: LazyRawStructKind::Binary_1_0(value),
}
}
}
impl<'data> From<LazyRawTextStruct<'data, TextEncoding_1_1>> for LazyRawAnyStruct<'data> {
fn from(value: LazyRawTextStruct<'data, TextEncoding_1_1>) -> Self {
LazyRawAnyStruct {
encoding: LazyRawStructKind::Text_1_1(value),
}
}
}
impl<'data> From<LazyRawBinaryStruct_1_1<'data>> for LazyRawAnyStruct<'data> {
fn from(value: LazyRawBinaryStruct_1_1<'data>) -> Self {
LazyRawAnyStruct {
encoding: LazyRawStructKind::Binary_1_1(value),
}
}
}
impl<'data> IntoIterator for LazyRawAnyStruct<'data> {
type Item = IonResult<LazyRawFieldExpr<'data, AnyEncoding>>;
type IntoIter = RawAnyStructIterator<'data>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[cfg(test)]
mod tests {
use crate::lazy::any_encoding::LazyRawAnyReader;
use crate::lazy::binary::test_utilities::to_binary_ion;
use crate::lazy::decoder::{LazyRawReader, LazyRawSequence, LazyRawValue};
use crate::lazy::expanded::EncodingContext;
use crate::lazy::raw_stream_item::LazyRawStreamItem;
use crate::lazy::raw_value_ref::RawValueRef;
use crate::{IonResult, RawSymbolRef, Timestamp};
use super::*;
#[test]
fn any_encoding() -> IonResult<()> {
fn test_input(data: &[u8]) -> IonResult<()> {
let encoding_context = EncodingContext::empty();
let context = encoding_context.get_ref();
let mut reader = LazyRawAnyReader::new(context, data, true);
assert_eq!(reader.next()?.expect_ivm()?.major_minor(), (1, 0));
let _strukt = reader.next()?.expect_value()?.read()?.expect_struct()?;
let name = reader.next()?.expect_value()?;
assert_eq!(
name.annotations().next().unwrap()?,
RawSymbolRef::SymbolId(4)
);
assert_eq!(name.read()?.expect_string()?.text(), "Gary");
assert_eq!(
reader.next()?.expect_value()?.read()?,
RawValueRef::String("foo".into())
);
assert_eq!(
reader.next()?.expect_value()?.read()?,
RawValueRef::Int(5.into())
);
assert_eq!(
reader.next()?.expect_value()?.read()?,
RawValueRef::Timestamp(Timestamp::with_year(2023).with_month(8).build()?)
);
assert_eq!(
reader.next()?.expect_value()?.read()?,
RawValueRef::Bool(false)
);
let mut sum = 0;
for lazy_value_result in reader.next()?.expect_value()?.read()?.expect_list()?.iter() {
sum += lazy_value_result?.expect_value()?.read()?.expect_i64()?;
}
assert_eq!(sum, 6);
assert!(matches!(
reader.next()?,
LazyRawStreamItem::<AnyEncoding>::EndOfStream(_)
));
Ok(())
}
let text_data = r#"
$ion_1_0
{$7: ["a", "b", "c"]}
$4::"Gary"
"foo"
5
2023-08T
false
[1, 2, 3]
"#;
let binary_data = to_binary_ion(text_data)?;
test_input(text_data.as_bytes())?;
test_input(&binary_data)?;
Ok(())
}
fn expect_version_change(
reader: &mut LazyRawAnyReader<'_>,
encoding_before: IonEncoding,
encoding_after: IonEncoding,
) -> IonResult<()> {
assert_eq!(reader.encoding(), encoding_before);
let ivm = reader.next()?.expect_ivm()?;
assert_eq!(ivm.stream_encoding_before_marker(), encoding_before);
assert_eq!(ivm.stream_encoding_after_marker()?, encoding_after);
assert_eq!(reader.encoding(), encoding_after);
Ok(())
}
fn expect_int(
reader: &mut LazyRawAnyReader<'_>,
expected_encoding: IonEncoding,
expected_int: i64,
) -> IonResult<()> {
let value = reader.next()?.expect_value()?;
let actual_int = value.read()?.expect_i64()?;
assert_eq!(actual_int, expected_int);
assert_eq!(reader.encoding(), expected_encoding);
Ok(())
}
#[test]
fn switch_text_versions() -> IonResult<()> {
const DATA: &str = r#"
1
$ion_1_0
2
$ion_1_1
3
$ion_1_1
4
$ion_1_0
5
"#;
let encoding_context = EncodingContext::empty();
let mut reader = LazyRawAnyReader::new(encoding_context.get_ref(), DATA.as_bytes(), true);
expect_int(&mut reader, IonEncoding::Text_1_0, 1)?;
expect_version_change(&mut reader, IonEncoding::Text_1_0, IonEncoding::Text_1_0)?;
expect_int(&mut reader, IonEncoding::Text_1_0, 2)?;
if cfg!(not(feature = "experimental-ion-1-1")) {
reader
.next()
.expect_err("Ion 1.1 IVM should return an error.");
return Ok(());
}
expect_version_change(&mut reader, IonEncoding::Text_1_0, IonEncoding::Text_1_1)?;
expect_int(&mut reader, IonEncoding::Text_1_1, 3)?;
expect_version_change(&mut reader, IonEncoding::Text_1_1, IonEncoding::Text_1_1)?;
expect_int(&mut reader, IonEncoding::Text_1_1, 4)?;
expect_version_change(&mut reader, IonEncoding::Text_1_1, IonEncoding::Text_1_0)?;
expect_int(&mut reader, IonEncoding::Text_1_0, 5)?;
Ok(())
}
#[test]
fn switch_binary_versions() -> IonResult<()> {
const DATA: &[u8] = &[
0xE0, 0x01, 0x00, 0xEA, 0x21, 0x02, 0xE0, 0x01, 0x01, 0xEA, 0x61, 0x03, 0xE0, 0x01, 0x01, 0xEA, 0x61, 0x04, 0xE0, 0x01, 0x00, 0xEA, 0x21, 0x05, ];
let encoding_context = EncodingContext::empty();
let mut reader = LazyRawAnyReader::new(encoding_context.get_ref(), DATA, true);
expect_version_change(
&mut reader,
IonEncoding::Binary_1_0,
IonEncoding::Binary_1_0,
)?;
expect_int(&mut reader, IonEncoding::Binary_1_0, 2)?;
if cfg!(not(feature = "experimental-ion-1-1")) {
reader
.next()
.expect_err("Ion 1.1 IVM should return an error.");
return Ok(());
}
expect_version_change(
&mut reader,
IonEncoding::Binary_1_0,
IonEncoding::Binary_1_1,
)?;
expect_int(&mut reader, IonEncoding::Binary_1_1, 3)?;
expect_version_change(
&mut reader,
IonEncoding::Binary_1_1,
IonEncoding::Binary_1_1,
)?;
expect_int(&mut reader, IonEncoding::Binary_1_1, 4)?;
expect_version_change(
&mut reader,
IonEncoding::Binary_1_1,
IonEncoding::Binary_1_0,
)?;
expect_int(&mut reader, IonEncoding::Binary_1_0, 5)?;
Ok(())
}
}