-
Notifications
You must be signed in to change notification settings - Fork 33
/
owned.rs
830 lines (719 loc) · 22.8 KB
/
owned.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
// Copyright Amazon.com, Inc. or its affiliates.
use crate::ion_eq::IonEq;
use crate::text::text_formatter::IonValueFormatter;
use crate::types::decimal::Decimal;
use crate::types::integer::Integer;
use crate::types::timestamp::Timestamp;
use crate::{IonType, Symbol};
use num_bigint::BigInt;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::iter::FromIterator;
use crate::symbol_ref::AsSymbolRef;
use crate::value::builders::{ListBuilder, SExpBuilder, StructBuilder};
use crate::value::iterators::{
ElementsIterator, FieldIterator, FieldValuesIterator, IndexVec, SymbolsIterator,
};
impl Element {
pub fn null(null_type: IonType) -> Element {
null_type.into()
}
pub fn boolean(value: bool) -> Element {
value.into()
}
pub fn string<I: Into<String>>(str: I) -> Element {
let text: String = str.into();
text.into()
}
pub fn symbol<I: Into<Symbol>>(symbol: I) -> Element {
let symbol: Symbol = symbol.into();
symbol.into()
}
pub fn integer<I: Into<Integer>>(integer: I) -> Element {
let integer: Integer = integer.into();
integer.into()
}
pub fn decimal(decimal: Decimal) -> Element {
decimal.into()
}
pub fn timestamp(timestamp: Timestamp) -> Element {
timestamp.into()
}
pub fn float(float: f64) -> Element {
float.into()
}
pub fn clob<A: AsRef<[u8]>>(bytes: A) -> Element {
let bytes: &[u8] = bytes.as_ref();
Value::Clob(bytes.into()).into()
}
pub fn blob<A: AsRef<[u8]>>(bytes: A) -> Element {
let bytes: &[u8] = bytes.as_ref();
Value::Blob(bytes.into()).into()
}
pub fn list_builder() -> ListBuilder {
ListBuilder::new()
}
pub fn sexp_builder() -> SExpBuilder {
SExpBuilder::new()
}
pub fn struct_builder() -> StructBuilder {
Struct::builder()
}
}
/// Behavior that is common to both [SExp] and [Struct].
pub trait IonSequence {
fn iter(&self) -> ElementsIterator<'_>;
fn get(&self, index: usize) -> Option<&Element>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// An in-memory representation of an Ion list
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct List {
children: Vec<Element>,
}
impl List {
pub(crate) fn new(children: Vec<Element>) -> Self {
Self { children }
}
pub fn builder() -> ListBuilder {
ListBuilder::new()
}
pub fn clone_builder(&self) -> ListBuilder {
ListBuilder::with_initial_elements(&self.children)
}
}
impl IonSequence for List {
fn iter(&self) -> ElementsIterator<'_> {
ElementsIterator::new(&self.children)
}
fn get(&self, index: usize) -> Option<&Element> {
self.children.get(index)
}
fn len(&self) -> usize {
self.children.len()
}
fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<S: IonSequence> IonEq for S {
fn ion_eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
for (item1, item2) in self.iter().zip(other.iter()) {
if !item1.ion_eq(item2) {
return false;
}
}
true
}
}
/// An in-memory representation of an Ion s-expression
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SExp {
children: Vec<Element>,
}
impl SExp {
pub(crate) fn new(children: Vec<Element>) -> Self {
Self { children }
}
pub fn builder() -> SExpBuilder {
SExpBuilder::new()
}
pub fn clone_builder(&self) -> SExpBuilder {
SExpBuilder::with_initial_elements(&self.children)
}
}
impl IonSequence for SExp {
fn iter(&self) -> ElementsIterator<'_> {
ElementsIterator::new(&self.children)
}
fn get(&self, index: usize) -> Option<&Element> {
self.children.get(index)
}
fn len(&self) -> usize {
self.children.len()
}
fn is_empty(&self) -> bool {
self.len() == 0
}
}
// This collection is broken out into its own type to allow instances of it to be shared with Arc/Rc.
#[derive(Debug, Clone)]
struct Fields {
// Key/value pairs in the order they were inserted
by_index: Vec<(Symbol, Element)>,
// Maps symbols to a list of indexes where values may be found in `by_index` above
by_name: HashMap<Symbol, IndexVec>,
}
impl Fields {
/// Gets all of the indexes that contain a value associated with the given field name.
fn get_indexes<A: AsSymbolRef>(&self, field_name: A) -> Option<&IndexVec> {
field_name
.as_symbol_ref()
.text()
.map(|text| {
// If the symbol has defined text, look it up by &str
self.by_name.get(text)
})
.unwrap_or_else(|| {
// Otherwise, construct a (cheap, stack-allocated) Symbol with unknown text...
let symbol = Symbol::unknown_text();
// ...and use the unknown text symbol to look up matching field values
self.by_name.get(&symbol)
})
}
/// Iterates over the values found at the specified indexes.
fn get_values_at_indexes<'a>(&'a self, indexes: &'a IndexVec) -> FieldValuesIterator<'a> {
FieldValuesIterator {
current: 0,
indexes: Some(indexes),
by_index: &self.by_index,
}
}
/// Gets the last value in the Struct that is associated with the specified field name.
///
/// Note that the Ion data model views a struct as a bag of (name, value) pairs and does not
/// have a notion of field ordering. In most use cases, field names are distinct and the last
/// appearance of a field in the struct's serialized form will have been the _only_ appearance.
/// If a field name appears more than once, this method makes the arbitrary decision to return
/// the value associated with the last appearance. If your application uses structs that repeat
/// field names, you are encouraged to use [get_all] instead.
fn get_last<A: AsSymbolRef>(&self, field_name: A) -> Option<&Element> {
self.get_indexes(field_name)
.and_then(|indexes| indexes.last())
.and_then(|index| self.by_index.get(*index))
.map(|(_name, value)| value)
}
/// Iterates over all of the values associated with the given field name.
fn get_all<A: AsSymbolRef>(&self, field_name: A) -> FieldValuesIterator {
let indexes = self.get_indexes(field_name);
FieldValuesIterator {
current: 0,
indexes,
by_index: &self.by_index,
}
}
/// Iterates over all of the (field name, field value) pairs in the struct.
fn iter(&self) -> impl Iterator<Item = &(Symbol, Element)> {
self.by_index.iter()
}
}
/// An in-memory representation of an Ion Struct
#[derive(Debug, Clone)]
pub struct Struct {
fields: Fields,
}
impl Struct {
pub fn builder() -> StructBuilder {
StructBuilder::new()
}
pub fn clone_builder(&self) -> StructBuilder {
StructBuilder::with_initial_fields(&self.fields.by_index)
}
/// Returns an iterator over the field name/value pairs in this Struct.
pub fn fields(&self) -> impl Iterator<Item = (&Symbol, &Element)> {
self.fields
.iter()
// Here we convert from &(name, value) to (&name, &value).
// The former makes a stronger assertion about how the data is being stored. We don't
// want that to be a mandatory part of the public API.
.map(|(name, element)| (name, element))
}
fn fields_eq(&self, other: &Self) -> bool {
// For each field name in `self`, get the list of indexes that contain a value with that name.
for (field_name, field_value_indexes) in &self.fields.by_name {
let other_value_indexes = match other.fields.get_indexes(field_name) {
Some(indexes) => indexes,
// The other struct doesn't have a field with this name so they're not equal.
None => return false,
};
if field_value_indexes.len() != other_value_indexes.len() {
// The other struct has fields with the same name, but a different number of them.
return false;
}
for field_value in self.fields.get_values_at_indexes(field_value_indexes) {
if other
.fields
.get_values_at_indexes(other_value_indexes)
.all(|other_value| !field_value.ion_eq(other_value))
{
// Couldn't find an equivalent field in the other struct
return false;
}
}
}
// If all of the above conditions hold, the two structs are equal.
true
}
/// Returns the number of fields in this Struct.
pub fn len(&self) -> usize {
self.fields.by_index.len()
}
/// Returns `true` if this struct has zero fields.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<K, V> FromIterator<(K, V)> for Struct
where
K: Into<Symbol>,
V: Into<Element>,
{
/// Returns an owned struct from the given iterator of field names/values.
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut by_index: Vec<(Symbol, Element)> = Vec::new();
let mut by_name: HashMap<Symbol, IndexVec> = HashMap::new();
for (field_name, field_value) in iter {
let field_name = field_name.into();
let field_value = field_value.into();
by_name
.entry(field_name.clone())
.or_insert_with(IndexVec::new)
.push(by_index.len());
by_index.push((field_name, field_value));
}
let fields = Fields { by_index, by_name };
Self { fields }
}
}
impl Struct {
pub fn iter(&self) -> FieldIterator<'_> {
FieldIterator::new(&self.fields.by_index)
}
pub fn get<A: AsSymbolRef>(&self, field_name: A) -> Option<&Element> {
self.fields.get_last(field_name)
}
pub fn get_all<A: AsSymbolRef>(&self, field_name: A) -> FieldValuesIterator<'_> {
self.fields.get_all(field_name)
}
}
impl PartialEq for Struct {
fn eq(&self, other: &Self) -> bool {
// check if both fields have same length
self.len() == other.len()
// we need to test equality in both directions for both fields
// A good example for this is annotated vs not annotated values in struct
// { a:4, a:4 } vs. { a:4, a:a::4 } // returns true
// { a:4, a:a::4 } vs. { a:4, a:4 } // returns false
&& self.fields_eq(other) && other.fields_eq(self)
}
}
impl Eq for Struct {}
impl IonEq for Value {
fn ion_eq(&self, other: &Self) -> bool {
use Value::*;
match (self, other) {
(Float(f1), Float(f2)) => return f1.ion_eq(f2),
(Decimal(d1), Decimal(d2)) => return d1.ion_eq(d2),
(Timestamp(t1), Timestamp(t2)) => return t1.ion_eq(t2),
(List(l1), List(l2)) => return l1.ion_eq(l2),
(SExpression(s1), SExpression(s2)) => return s1.ion_eq(s2),
_ => {}
};
// For any other case, fall back to vanilla equality
self == other
}
}
impl IonEq for Element {
fn ion_eq(&self, other: &Self) -> bool {
self.annotations == other.annotations && self.value.ion_eq(&other.value)
}
}
impl IonEq for Vec<Element> {
fn ion_eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
for (v1, v2) in self.iter().zip(other.iter()) {
if !v1.ion_eq(v2) {
return false;
}
}
true
}
}
/// Variants for all _values_ within an [`Element`].
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Null(IonType),
Integer(Integer),
Float(f64),
Decimal(Decimal),
Timestamp(Timestamp),
String(String),
Symbol(Symbol),
Boolean(bool),
Blob(Vec<u8>),
Clob(Vec<u8>),
SExpression(SExp),
List(List),
Struct(Struct),
}
/// An `(annotations, value)` pair representing an Ion value.
#[derive(Debug, Clone)]
pub struct Element {
annotations: Vec<Symbol>,
value: Value,
}
impl Element {
pub fn new(annotations: Vec<Symbol>, value: Value) -> Self {
Self { annotations, value }
}
}
impl Display for Element {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut ivf = IonValueFormatter { output: f };
// display for annotations of this owned_element
ivf.format_annotations(&self.annotations)
.map_err(|_| std::fmt::Error)?;
match &self.value {
Value::Null(ion_type) => ivf.format_null(*ion_type),
Value::Boolean(bool) => ivf.format_bool(*bool),
Value::Integer(integer) => ivf.format_integer(integer),
Value::Float(float) => ivf.format_float(*float),
Value::Decimal(decimal) => ivf.format_decimal(decimal),
Value::Timestamp(timestamp) => ivf.format_timestamp(timestamp),
Value::Symbol(symbol) => ivf.format_symbol(symbol),
Value::String(string) => ivf.format_string(string),
Value::Clob(clob) => ivf.format_clob(clob),
Value::Blob(blob) => ivf.format_blob(blob),
Value::Struct(struct_) => ivf.format_struct(struct_),
Value::SExpression(sexp) => ivf.format_sexp(sexp),
Value::List(list) => ivf.format_list(list),
}
.map_err(|_| std::fmt::Error)?;
Ok(())
}
}
impl PartialEq for Element {
fn eq(&self, other: &Self) -> bool {
self.value == other.value && self.annotations == other.annotations
}
}
impl Eq for Element {}
// Anything that can be turned into a `Value` can then be turned into an `Element`
// by associating it with an empty annotations sequence.
impl<T> From<T> for Element
where
T: Into<Value>,
{
fn from(value: T) -> Self {
Element::new(Vec::new(), value.into())
}
}
impl From<IonType> for Value {
fn from(ion_type: IonType) -> Self {
Value::Null(ion_type)
}
}
impl From<i64> for Value {
fn from(i64_val: i64) -> Self {
Value::Integer(Integer::I64(i64_val))
}
}
impl From<BigInt> for Value {
fn from(big_int_val: BigInt) -> Self {
Value::Integer(Integer::BigInt(big_int_val))
}
}
impl From<Integer> for Value {
fn from(integer_val: Integer) -> Self {
Value::Integer(integer_val)
}
}
impl From<f64> for Value {
fn from(f64_val: f64) -> Self {
Value::Float(f64_val)
}
}
impl From<Decimal> for Value {
fn from(decimal_val: Decimal) -> Self {
Value::Decimal(decimal_val)
}
}
impl From<Timestamp> for Value {
fn from(timestamp_val: Timestamp) -> Self {
Value::Timestamp(timestamp_val)
}
}
impl From<bool> for Value {
fn from(bool_val: bool) -> Self {
Value::Boolean(bool_val)
}
}
impl From<&str> for Value {
fn from(string_val: &str) -> Self {
Value::String(string_val.to_owned())
}
}
impl From<String> for Value {
fn from(string_val: String) -> Self {
Value::String(string_val)
}
}
impl From<Symbol> for Value {
fn from(sym_val: Symbol) -> Self {
Value::Symbol(sym_val)
}
}
impl From<List> for Value {
fn from(list: List) -> Self {
Value::List(list)
}
}
impl From<SExp> for Value {
fn from(s_expr: SExp) -> Self {
Value::SExpression(s_expr)
}
}
impl From<Struct> for Value {
fn from(struct_val: Struct) -> Self {
Value::Struct(struct_val)
}
}
impl<A, S, V> From<(A, V)> for Element
where
A: IntoIterator<Item = S>,
S: Into<Symbol>,
V: Into<Value>,
{
fn from(pair: (A, V)) -> Self {
let annotations: Vec<Symbol> = pair.0.into_iter().map(|s| s.into()).collect();
let value: Value = pair.1.into();
Element::new(annotations, value)
}
}
impl Element {
pub fn ion_type(&self) -> IonType {
use Value::*;
match &self.value {
Null(t) => *t,
Integer(_) => IonType::Integer,
Float(_) => IonType::Float,
Decimal(_) => IonType::Decimal,
Timestamp(_) => IonType::Timestamp,
String(_) => IonType::String,
Symbol(_) => IonType::Symbol,
Boolean(_) => IonType::Boolean,
Blob(_) => IonType::Blob,
Clob(_) => IonType::Clob,
SExpression(_) => IonType::SExpression,
List(_) => IonType::List,
Struct(_) => IonType::Struct,
}
}
pub fn annotations(&self) -> SymbolsIterator<'_> {
SymbolsIterator::new(&self.annotations)
}
pub fn with_annotations<S: Into<Symbol>, I: IntoIterator<Item = S>>(
self,
annotations: I,
) -> Self {
let annotations: Vec<Symbol> = annotations.into_iter().map(|i| i.into()).collect();
Element::new(annotations, self.value)
}
pub fn has_annotation(&self, annotation: &str) -> bool {
self.annotations
.iter()
.any(|a| a.text() == Some(annotation))
}
pub fn is_null(&self) -> bool {
matches!(&self.value, Value::Null(_))
}
pub fn as_integer(&self) -> Option<&Integer> {
match &self.value {
Value::Integer(i) => Some(i),
_ => None,
}
}
pub fn as_float(&self) -> Option<f64> {
match &self.value {
Value::Float(f) => Some(*f),
_ => None,
}
}
pub fn as_decimal(&self) -> Option<&Decimal> {
match &self.value {
Value::Decimal(d) => Some(d),
_ => None,
}
}
pub fn as_timestamp(&self) -> Option<&Timestamp> {
match &self.value {
Value::Timestamp(t) => Some(t),
_ => None,
}
}
pub fn as_text(&self) -> Option<&str> {
match &self.value {
Value::String(text) => Some(text),
Value::Symbol(sym) => sym.text(),
_ => None,
}
}
pub fn as_string(&self) -> Option<&str> {
match &self.value {
Value::String(text) => Some(text),
_ => None,
}
}
pub fn as_symbol(&self) -> Option<&Symbol> {
match &self.value {
Value::Symbol(sym) => Some(sym),
_ => None,
}
}
pub fn as_boolean(&self) -> Option<bool> {
match &self.value {
Value::Boolean(b) => Some(*b),
_ => None,
}
}
pub fn as_lob(&self) -> Option<&[u8]> {
match &self.value {
Value::Blob(bytes) | Value::Clob(bytes) => Some(bytes),
_ => None,
}
}
pub fn as_blob(&self) -> Option<&[u8]> {
match &self.value {
Value::Blob(bytes) => Some(bytes),
_ => None,
}
}
pub fn as_clob(&self) -> Option<&[u8]> {
match &self.value {
Value::Clob(bytes) => Some(bytes),
_ => None,
}
}
pub fn as_sequence(&self) -> Option<&dyn IonSequence> {
match &self.value {
Value::SExpression(sexp) => Some(sexp),
Value::List(list) => Some(list),
_ => None,
}
}
pub fn as_sexp(&self) -> Option<&SExp> {
match &self.value {
Value::SExpression(sexp) => Some(sexp),
_ => None,
}
}
pub fn as_list(&self) -> Option<&List> {
match &self.value {
Value::List(list) => Some(list),
_ => None,
}
}
pub fn as_struct(&self) -> Option<&Struct> {
match &self.value {
Value::Struct(structure) => Some(structure),
_ => None,
}
}
}
#[cfg(test)]
mod value_tests {
use super::*;
use crate::{ion, ion_list, ion_sexp, ion_struct};
use rstest::*;
#[rstest(
e1, e2,
case::strings(
Element::from("hello"), // An explicitly constructed String Element
"hello" // A Rust &str, which implements Into<Element>
),
case::symbols(
Element::from(Symbol::owned("hello")), // An explicitly constructed Symbol Element
Symbol::owned("hello") // A Symbol, which implements Into<Element>
),
case::struct_(
ion_struct!{"greetings": "hello"},
ion!(r#"{greetings: "hello"}"#)
),
)]
fn owned_element_accessors<E1, E2>(e1: E1, e2: E2)
where
E1: Into<Element>,
E2: Into<Element>,
{
// assert that both element construction methods create the same element
assert_eq!(e1.into(), e2.into());
}
#[rstest(
container, length,
case::struct_(
ion_struct!{"greetings": "hello", "name": "Ion"},
2
),
case::list(
ion_list!["greetings", 5, true],
3
),
case::sexp(
ion_sexp!(5 true),
2
),
)]
fn owned_container_len_test<I: Into<Element>>(container: I, length: usize) {
let container = container.into();
match container.ion_type() {
IonType::List | IonType::SExpression => {
// check length for given sequence value
assert_eq!(container.as_sequence().unwrap().len(), length);
}
IonType::Struct => {
// check length for given struct value
assert_eq!(container.as_struct().unwrap().len(), length);
}
_ => {
unreachable!("This test is only for container type elements")
}
}
}
#[rstest(
container, is_empty,
case::struct_(
ion_struct!{"greetings": "hello", "name": "Ion"},
false
),
case::list(
ion_list!["greetings", 5, true],
false
),
case::list_empty(
ion_list![],
true
),
case::sexp(
ion_sexp!(5 true),
false
),
case::sexp_empty(
ion_sexp!(),
true
),
)]
fn owned_container_is_empty_test<I: Into<Element>>(container: I, is_empty: bool) {
let container = container.into();
match container.ion_type() {
IonType::List | IonType::SExpression => {
// check length for given sequence value
assert_eq!(container.as_sequence().unwrap().is_empty(), is_empty);
}
IonType::Struct => {
// check length for given struct value
assert_eq!(container.as_struct().unwrap().is_empty(), is_empty);
}
_ => {
unreachable!("This test is only for container type elements")
}
}
}
}