Skip to content

Commit cdbc815

Browse files
authored
Version bump to 1.0.0-rc.6, improvements to IonEncoding (#785)
* Version bump to 1.0.0-rc.6 * re-export IonEncoding as part of experimental-reader-writer * Adds name(), version() methods to IonEncoding * Raw stream items can now report their encoding * Adds `expect_text` method to Symbol, SymbolRef
1 parent 62d0755 commit cdbc815

13 files changed

+116
-26
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = [
1616
"**/ion-tests/iontestdata/**",
1717
"*.pdf"
1818
]
19-
version = "1.0.0-rc.5"
19+
version = "1.0.0-rc.6"
2020
edition = "2021"
2121
# We need at least 1.65 for GATs[1] and 1.67 for `ilog`[2]
2222
# [1] https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html

src/lazy/any_encoding.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::lazy::text::value::{
5353
LazyRawTextValue_1_0, LazyRawTextValue_1_1, LazyRawTextVersionMarker_1_0,
5454
LazyRawTextVersionMarker_1_1, RawTextAnnotationsIterator,
5555
};
56-
use crate::{IonResult, IonType, RawSymbolRef};
56+
use crate::{Encoding, IonResult, IonType, RawSymbolRef};
5757
use bumpalo::Bump as BumpAllocator;
5858

5959
/// An implementation of the `LazyDecoder` trait that can read any encoding of Ion.
@@ -89,6 +89,18 @@ pub enum LazyRawAnyVersionMarkerKind<'top> {
8989
Binary_1_1(LazyRawBinaryVersionMarker_1_1<'top>),
9090
}
9191

92+
impl<'top> LazyRawAnyVersionMarker<'top> {
93+
pub fn encoding(&self) -> IonEncoding {
94+
use crate::lazy::any_encoding::LazyRawAnyVersionMarkerKind::*;
95+
match self.encoding {
96+
Text_1_0(_) => TextEncoding_1_0.encoding(),
97+
Binary_1_0(_) => BinaryEncoding_1_0.encoding(),
98+
Text_1_1(_) => TextEncoding_1_1.encoding(),
99+
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
100+
}
101+
}
102+
}
103+
92104
impl<'top> HasSpan<'top> for LazyRawAnyVersionMarker<'top> {
93105
fn span(&self) -> Span<'top> {
94106
use LazyRawAnyVersionMarkerKind::*;
@@ -165,6 +177,16 @@ pub enum LazyRawAnyEExpressionKind<'top> {
165177
Binary_1_1(Never), // TODO: RawBinaryEExpression_1_1
166178
}
167179

180+
impl<'top> LazyRawAnyEExpression<'top> {
181+
pub fn encoding(&self) -> IonEncoding {
182+
use LazyRawAnyEExpressionKind::*;
183+
match self.encoding {
184+
Text_1_1(_) => TextEncoding_1_1.encoding(),
185+
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
186+
}
187+
}
188+
}
189+
168190
impl<'top> From<RawTextEExpression_1_1<'top>> for LazyRawAnyEExpression<'top> {
169191
fn from(text_invocation: RawTextEExpression_1_1<'top>) -> Self {
170192
LazyRawAnyEExpression {
@@ -278,7 +300,7 @@ pub enum RawReaderKind<'data> {
278300
Binary_1_1(LazyRawBinaryReader_1_1<'data>),
279301
}
280302

281-
#[derive(Default, Copy, Clone)]
303+
#[derive(Default, Debug, Copy, Clone)]
282304
#[non_exhaustive]
283305
pub enum IonEncoding {
284306
// In the absence of a binary IVM, readers must assume Ion 1.0 text data until a
@@ -300,6 +322,24 @@ impl IonEncoding {
300322
use IonEncoding::*;
301323
matches!(*self, Binary_1_0 | Binary_1_1)
302324
}
325+
326+
pub fn name(&self) -> &str {
327+
use IonEncoding::*;
328+
match self {
329+
Text_1_0 => TextEncoding_1_0::name(),
330+
Binary_1_0 => BinaryEncoding_1_0::name(),
331+
Text_1_1 => TextEncoding_1_1::name(),
332+
Binary_1_1 => BinaryEncoding_1_1::name(),
333+
}
334+
}
335+
336+
pub fn version(&self) -> (u8, u8) {
337+
use IonEncoding::*;
338+
match self {
339+
Text_1_0 | Binary_1_0 => (1, 0),
340+
Text_1_1 | Binary_1_1 => (1, 1),
341+
}
342+
}
303343
}
304344

305345
impl<'data> From<LazyRawTextReader_1_0<'data>> for LazyRawAnyReader<'data> {
@@ -421,6 +461,16 @@ impl<'top> LazyRawAnyValue<'top> {
421461
pub fn kind(&self) -> LazyRawValueKind<'top> {
422462
self.encoding
423463
}
464+
465+
pub fn encoding(&self) -> IonEncoding {
466+
use LazyRawValueKind::*;
467+
match &self.encoding {
468+
Text_1_0(_) => TextEncoding_1_0.encoding(),
469+
Binary_1_0(_) => BinaryEncoding_1_0.encoding(),
470+
Text_1_1(_) => TextEncoding_1_1.encoding(),
471+
Binary_1_1(_) => BinaryEncoding_1_1.encoding(),
472+
}
473+
}
424474
}
425475

426476
#[derive(Debug, Copy, Clone)]

src/lazy/binary/raw/reader.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::lazy::decoder::{Decoder, HasRange, LazyRawFieldExpr, LazyRawReader, R
66
use crate::lazy::encoding::BinaryEncoding_1_0;
77
use crate::lazy::raw_stream_item::{EndPosition, LazyRawStreamItem, RawStreamItem};
88
use crate::result::IonFailure;
9-
use crate::IonResult;
9+
use crate::{Encoding, IonResult};
1010

1111
use crate::lazy::any_encoding::IonEncoding;
1212
use bumpalo::Bump as BumpAllocator;
@@ -66,7 +66,7 @@ impl<'data> LazyRawBinaryReader_1_0<'data> {
6666
Some(lazy_value) => lazy_value,
6767
None => {
6868
return Ok(LazyRawStreamItem::<BinaryEncoding_1_0>::EndOfStream(
69-
EndPosition::new(self.position()),
69+
EndPosition::new(BinaryEncoding_1_0.encoding(), self.position()),
7070
))
7171
}
7272
};
@@ -83,7 +83,7 @@ impl<'data> LazyRawBinaryReader_1_0<'data> {
8383
let mut buffer = self.data.advance_to_next_item()?;
8484
if buffer.is_empty() {
8585
return Ok(LazyRawStreamItem::<BinaryEncoding_1_0>::EndOfStream(
86-
EndPosition::new(self.position()),
86+
EndPosition::new(BinaryEncoding_1_0.encoding(), self.position()),
8787
));
8888
}
8989
// Peek at the first byte in the new buffer view
@@ -94,7 +94,7 @@ impl<'data> LazyRawBinaryReader_1_0<'data> {
9494
(_, buffer) = buffer.consume_nop_padding(type_descriptor)?;
9595
if buffer.is_empty() {
9696
return Ok(LazyRawStreamItem::<BinaryEncoding_1_0>::EndOfStream(
97-
EndPosition::new(buffer.offset()),
97+
EndPosition::new(BinaryEncoding_1_0.encoding(), buffer.offset()),
9898
));
9999
}
100100
type_descriptor = buffer.peek_type_descriptor()?;

src/lazy/binary/raw/v1_1/reader.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::lazy::encoder::private::Sealed;
77
use crate::lazy::encoding::BinaryEncoding_1_1;
88
use crate::lazy::raw_stream_item::{EndPosition, LazyRawStreamItem, RawStreamItem};
99
use crate::result::IonFailure;
10-
use crate::IonResult;
10+
use crate::{Encoding, IonResult};
1111

1212
use crate::lazy::any_encoding::IonEncoding;
1313
use bumpalo::Bump as BumpAllocator;
@@ -62,7 +62,7 @@ impl<'data> LazyRawBinaryReader_1_1<'data> {
6262
Some(lazy_value) => lazy_value,
6363
None => {
6464
return Ok(LazyRawStreamItem::<BinaryEncoding_1_1>::EndOfStream(
65-
EndPosition::new(self.position()),
65+
EndPosition::new(BinaryEncoding_1_1.encoding(), self.position()),
6666
))
6767
}
6868
};
@@ -93,7 +93,7 @@ impl<'data> LazyRawBinaryReader_1_1<'data> {
9393
let mut buffer = self.advance_to_next_item()?;
9494
if buffer.is_empty() {
9595
return Ok(LazyRawStreamItem::<BinaryEncoding_1_1>::EndOfStream(
96-
EndPosition::new(buffer.offset()),
96+
EndPosition::new(BinaryEncoding_1_1.encoding(), buffer.offset()),
9797
));
9898
}
9999

@@ -102,7 +102,7 @@ impl<'data> LazyRawBinaryReader_1_1<'data> {
102102
(_, buffer) = buffer.consume_nop_padding(type_descriptor)?;
103103
if buffer.is_empty() {
104104
return Ok(LazyRawStreamItem::<BinaryEncoding_1_1>::EndOfStream(
105-
EndPosition::new(buffer.offset()),
105+
EndPosition::new(BinaryEncoding_1_1.encoding(), buffer.offset()),
106106
));
107107
}
108108
}

src/lazy/raw_stream_item.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::lazy::decoder::{Decoder, HasRange, HasSpan};
22
use crate::lazy::span::Span;
33
use crate::result::IonFailure;
4-
use crate::{IonError, IonResult};
4+
use crate::{AnyEncoding, IonEncoding, IonError, IonResult};
55
use std::fmt::Debug;
66
use std::ops::Range;
77

@@ -27,6 +27,17 @@ pub type LazyRawStreamItem<'top, D> = RawStreamItem<
2727
<D as Decoder>::EExp<'top>,
2828
>;
2929

30+
impl<'top> LazyRawStreamItem<'top, AnyEncoding> {
31+
pub fn encoding(&self) -> IonEncoding {
32+
match self {
33+
LazyRawStreamItem::<AnyEncoding>::VersionMarker(m) => m.encoding(),
34+
LazyRawStreamItem::<AnyEncoding>::Value(v) => v.encoding(),
35+
LazyRawStreamItem::<AnyEncoding>::EExpression(e) => e.encoding(),
36+
LazyRawStreamItem::<AnyEncoding>::EndOfStream(eos) => eos.encoding(),
37+
}
38+
}
39+
}
40+
3041
impl<M: Debug + HasRange, V: Debug + HasRange, E: Debug + HasRange> HasRange
3142
for RawStreamItem<M, V, E>
3243
{
@@ -116,12 +127,17 @@ impl<M: Copy + Debug, V: Copy + Debug, E: Copy + Debug> RawStreamItem<M, V, E> {
116127
/// an `EndOfStream(EndPosition)` variant) to also implement them.
117128
#[derive(Debug, Copy, Clone)]
118129
pub struct EndPosition {
130+
encoding: IonEncoding,
119131
position: usize,
120132
}
121133

122134
impl EndPosition {
123-
pub(crate) fn new(position: usize) -> Self {
124-
Self { position }
135+
pub(crate) fn new(encoding: IonEncoding, position: usize) -> Self {
136+
Self { encoding, position }
137+
}
138+
139+
pub fn encoding(&self) -> IonEncoding {
140+
self.encoding
125141
}
126142
}
127143

src/lazy/system_stream_item.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,16 @@ impl<'top, D: Decoder> SystemStreamItem<'top, D> {
7373
}
7474
}
7575

76-
/// Like [`Self::symbol_table`], but returns a [`IonError::Decoding`] if this item is not
77-
/// a symbol table.
78-
pub fn symbol_table(self) -> Option<LazyStruct<'top, D>> {
76+
/// If this item is a symbol table, returns `Some(lazy_struct)`. Otherwise, returns `None`.
77+
pub fn as_symbol_table(self) -> Option<LazyStruct<'top, D>> {
7978
if let Self::SymbolTable(struct_) = self {
8079
Some(struct_)
8180
} else {
8281
None
8382
}
8483
}
8584

86-
/// Like [`Self::symbol_table`], but returns a [`IonError::Decoding`] if this item is not
85+
/// Like [`Self::as_symbol_table`], but returns a [`IonError::Decoding`] if this item is not
8786
/// a symbol table.
8887
pub fn expect_symbol_table(self) -> IonResult<LazyStruct<'top, D>> {
8988
if let Self::SymbolTable(value) = self {

src/lazy/text/buffer.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::lazy::text::value::{
4343
LazyRawTextValue, LazyRawTextValue_1_0, LazyRawTextValue_1_1, LazyRawTextVersionMarker,
4444
};
4545
use crate::result::DecodingError;
46-
use crate::{IonError, IonResult, IonType, TimestampPrecision};
46+
use crate::{Encoding, IonError, IonResult, IonType, TimestampPrecision};
4747

4848
impl<'a> Debug for TextBufferView<'a> {
4949
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@@ -558,7 +558,10 @@ impl<'top> TextBufferView<'top> {
558558
if input_after_ws.is_empty() {
559559
return Ok((
560560
input_after_ws,
561-
RawStreamItem::EndOfStream(EndPosition::new(input_after_ws.offset())),
561+
RawStreamItem::EndOfStream(EndPosition::new(
562+
TextEncoding_1_0.encoding(),
563+
input_after_ws.offset(),
564+
)),
562565
));
563566
}
564567
// Otherwise, the next item must be an IVM or a value.
@@ -581,7 +584,10 @@ impl<'top> TextBufferView<'top> {
581584
if input_after_ws.is_empty() {
582585
return Ok((
583586
input_after_ws,
584-
RawStreamItem::EndOfStream(EndPosition::new(input_after_ws.offset())),
587+
RawStreamItem::EndOfStream(EndPosition::new(
588+
TextEncoding_1_1.encoding(),
589+
input_after_ws.offset(),
590+
)),
585591
));
586592
}
587593
// Otherwise, the next item must be an IVM or a value.

src/lazy/text/raw/reader.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::lazy::raw_stream_item::{EndPosition, LazyRawStreamItem, RawStreamItem
99
use crate::lazy::text::buffer::TextBufferView;
1010
use crate::lazy::text::parse_result::AddContext;
1111
use crate::result::IonFailure;
12-
use crate::IonResult;
12+
use crate::{Encoding, IonResult};
1313

1414
/// A text Ion 1.0 reader that yields [`LazyRawStreamItem`]s representing the top level values found
1515
/// in the provided input stream.
@@ -59,6 +59,7 @@ impl<'data> LazyRawTextReader_1_0<'data> {
5959
.with_context("reading whitespace/comments at the top level", input)?;
6060
if buffer_after_whitespace.is_empty() {
6161
return Ok(RawStreamItem::EndOfStream(EndPosition::new(
62+
TextEncoding_1_0.encoding(),
6263
buffer_after_whitespace.offset(),
6364
)));
6465
}

src/lazy/text/raw/v1_1/reader.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::lazy::text::matched::{MatchedFieldName, MatchedValue};
2424
use crate::lazy::text::parse_result::{AddContext, ToIteratorOutput};
2525
use crate::lazy::text::value::{LazyRawTextValue_1_1, RawTextAnnotationsIterator};
2626
use crate::result::IonFailure;
27-
use crate::{IonResult, IonType, RawSymbolRef};
27+
use crate::{Encoding, IonResult, IonType, RawSymbolRef};
2828

2929
pub struct LazyRawTextReader_1_1<'data> {
3030
input: &'data [u8],
@@ -168,6 +168,7 @@ impl<'data> LazyRawReader<'data, TextEncoding_1_1> for LazyRawTextReader_1_1<'da
168168
.with_context("reading v1.1 whitespace/comments at the top level", input)?;
169169
if buffer_after_whitespace.is_empty() {
170170
return Ok(RawStreamItem::EndOfStream(EndPosition::new(
171+
TextEncoding_1_1.encoding(),
171172
buffer_after_whitespace.offset(),
172173
)));
173174
}

src/lazy/value.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,17 @@ impl<'top, D: Decoder> LazyValue<'top, D> {
6666
LazyValue { expanded_value }
6767
}
6868

69-
fn symbol_table(&'top self) -> &'top SymbolTable {
69+
#[cfg(feature = "experimental-tooling-apis")]
70+
pub fn symbol_table(&self) -> &SymbolTable {
71+
self.expanded_value.context.symbol_table
72+
}
73+
74+
// When the `experimental-tooling-apis` feature is disabled, this method is `pub(crate)`
75+
#[cfg(not(feature = "experimental-tooling-apis"))]
76+
pub(crate) fn symbol_table(&self) -> &SymbolTable {
7077
self.expanded_value.context.symbol_table
7178
}
79+
7280
/// Returns the [`IonType`] of this value.
7381
/// ```
7482
///# use ion_rs::IonResult;

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ macro_rules! v1_x_reader_writer {
213213
lazy::r#struct::{LazyStruct, LazyField},
214214
lazy::sequence::{LazyList, LazySExp},
215215
lazy::encoder::value_writer::{ValueWriter, StructWriter, SequenceWriter, EExpWriter},
216+
lazy::any_encoding::IonEncoding,
216217
};
217218
};
218219
}

src/symbol_ref.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::raw_symbol_ref::{AsRawSymbolRef, RawSymbolRef};
2-
use crate::{Str, Symbol};
2+
use crate::result::IonFailure;
3+
use crate::{IonResult, Str, Symbol};
34
use std::borrow::Borrow;
45
use std::fmt::{Debug, Formatter};
56
use std::hash::{Hash, Hasher};
@@ -39,6 +40,13 @@ impl<'a> SymbolRef<'a> {
3940
Some(text) => Symbol::owned(Str::from(text)),
4041
}
4142
}
43+
44+
pub fn expect_text(&self) -> IonResult<&str> {
45+
match self.text() {
46+
Some(text) => Ok(text),
47+
None => IonResult::decoding_error("symbol has unknown text"),
48+
}
49+
}
4250
}
4351

4452
impl<'a, A> PartialEq<A> for SymbolRef<'a>

src/types/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Symbol {
114114
self.text.text()
115115
}
116116

117-
pub fn text_or_error(&self) -> IonResult<&str> {
117+
pub fn expect_text(&self) -> IonResult<&str> {
118118
match self.text() {
119119
Some(text) => Ok(text),
120120
None => IonResult::decoding_error("symbol has unknown text"),

0 commit comments

Comments
 (0)