ion-rs 1.0.0

Implementation of Amazon Ion
Documentation
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
use delegate::delegate;
use ice_code::ice as cold_path;
use std::io::Write;
use std::ops::Deref;
use std::sync::Arc;

use crate::constants::v1_0::system_symbol_ids;
use crate::constants::v1_1;
use crate::element::element_writer::ElementWriter;
use crate::lazy::encoder::annotation_seq::{AnnotationSeq, AnnotationsVec};
use crate::lazy::encoder::binary::v1_1::value_writer::BinaryValueWriter_1_1;
use crate::lazy::encoder::value_writer::internal::{
    EExpWriterInternal, FieldEncoder, MakeValueWriter,
};
use crate::lazy::encoder::value_writer::{
    AnnotatableWriter, EExpWriter, FieldWriter, SequenceWriter, StructWriter, ValueWriter,
};
use crate::lazy::encoder::value_writer_config::{
    AnnotationsEncoding, ContainerEncoding, FieldNameEncoding, SymbolValueEncoding,
    ValueWriterConfig,
};
use crate::lazy::encoder::write_as_ion::WriteAsIon;
use crate::lazy::encoder::LazyRawWriter;
use crate::lazy::encoding::{
    BinaryEncoding_1_0, BinaryEncoding_1_1, Encoding, TextEncoding_1_0, TextEncoding_1_1,
};
use crate::lazy::expanded::macro_table::{Macro, MacroRef, ION_1_1_SYSTEM_MACROS};
use crate::lazy::expanded::template::{Parameter, ParameterEncoding};
use crate::lazy::text::raw::v1_1::reader::{MacroIdLike, MacroIdRef, ModuleKind, QualifiedAddress};
use crate::raw_symbol_ref::AsRawSymbolRef;
use crate::result::IonFailure;
use crate::write_config::WriteConfig;
use crate::{
    ContextWriter, Decimal, Element, Int, IonError, IonInput, IonResult, IonType, IonVersion,
    MacroDef, MacroTable, RawSymbolRef, Symbol, SymbolId, SymbolTable, TemplateMacro, Timestamp,
    UInt, Value,
};

/// A thin wrapper around a `SymbolTable` that tracks the number of symbols whose definition has
/// not yet been written to output.
pub(crate) struct WriterSymbolTable {
    symbols: SymbolTable,
    num_pending: usize,
}

impl WriterSymbolTable {
    pub fn reset_num_pending(&mut self) {
        self.num_pending = 0;
    }

    pub fn num_pending(&self) -> usize {
        self.num_pending
    }

    pub fn pending(&self) -> &[Symbol] {
        self.symbols.symbols_tail(self.num_pending)
    }

    pub fn add_symbol_for_text<A: AsRef<str>>(&mut self, text: A) -> SymbolId {
        self.num_pending += 1;
        self.symbols.add_symbol_for_text(text)
    }

    pub fn new(symbols: SymbolTable) -> Self {
        Self {
            symbols,
            num_pending: 0,
        }
    }
}

// Read-only methods on the underlying SymbolTable can be invoked directly.
impl Deref for WriterSymbolTable {
    type Target = SymbolTable;

    fn deref(&self) -> &Self::Target {
        &self.symbols
    }
}

/// A thin wrapper around a `MacroTable` that tracks the number of macros whose definition has
/// not yet been written to output.
pub struct WriterMacroTable {
    macros: MacroTable,
    num_pending: usize,
}

impl WriterMacroTable {
    pub fn new(macros: MacroTable) -> Self {
        Self {
            macros,
            num_pending: 0,
        }
    }

    pub fn add_template_macro(&mut self, template_macro: TemplateMacro) -> IonResult<usize> {
        let address = self.macros.add_template_macro(template_macro)?;
        self.num_pending += 1;
        Ok(address)
    }

    pub(crate) fn add_macro(&mut self, macro_ref: &Arc<MacroDef>) -> IonResult<usize> {
        let address = self.macros.len();
        self.macros.append_macro(macro_ref)?;
        self.num_pending += 1;
        Ok(address)
    }

    pub fn reset_num_pending(&mut self) {
        self.num_pending = 0;
    }

    pub fn num_pending(&self) -> usize {
        self.num_pending
    }

    pub fn pending(&self) -> &[Arc<MacroDef>] {
        self.macros.macros_tail(self.num_pending)
    }
}

// Read-only methods on the underlying MacroTable can be invoked directly.
impl Deref for WriterMacroTable {
    type Target = MacroTable;

    fn deref(&self) -> &Self::Target {
        &self.macros
    }
}

/// An Ion writer that maintains a symbol table and creates new entries as needed.
#[cfg_attr(feature = "experimental-reader-writer", visibility::make(pub))]
pub(crate) struct Writer<E: Encoding, Output: Write> {
    symbols: WriterSymbolTable,
    data_writer: E::Writer<Vec<u8>>,
    directive_writer: E::Writer<Vec<u8>>,
    output: Output,
    value_writer_config: ValueWriterConfig,
}

// These aliases are used for selectively re-exporting writer types in lib.rs.
#[allow(dead_code)]
pub type TextWriter_1_0<Output> = Writer<TextEncoding_1_0, Output>;
#[allow(dead_code)]
pub type BinaryWriter_1_0<Output> = Writer<BinaryEncoding_1_0, Output>;
#[allow(dead_code)]
pub type TextWriter_1_1<Output> = Writer<TextEncoding_1_1, Output>;
#[allow(dead_code)]
pub type BinaryWriter_1_1<Output> = Writer<BinaryEncoding_1_1, Output>;

#[cfg_attr(not(feature = "experimental-reader-writer"), allow(dead_code))]
impl<E: Encoding, Output: Write> Writer<E, Output> {
    /// Constructs a writer for the requested encoding using the provided configuration.
    pub fn new(config: impl Into<WriteConfig<E>>, output: Output) -> IonResult<Self> {
        let config = config.into();
        let directive_writer = E::Writer::build(config.clone(), vec![])?;
        let mut data_writer = E::Writer::build(config, vec![])?;
        // Erase the IVM that's created by default
        data_writer.output_mut().clear();
        // TODO: LazyEncoder should define a method to construct a new symtab and/or macro table
        let ion_version = E::ion_version();
        let symbols = WriterSymbolTable::new(SymbolTable::new(ion_version));
        let mut writer = Writer {
            symbols,
            data_writer,
            directive_writer,
            output,
            value_writer_config: E::default_value_writer_config(),
        };
        writer.flush()?;
        Ok(writer)
    }

    pub(crate) fn macro_table(&self) -> &WriterMacroTable {
        self.data_writer.macro_table()
    }

    pub(crate) fn macro_table_mut(&mut self) -> Option<&mut WriterMacroTable> {
        self.data_writer.macro_table_mut()
    }

    /// Takes a TDL expression representing a macro definition and returns a `Macro` that can
    /// later be invoked by passing it to [`Writer::eexp_writer()`].
    pub fn compile_macro(&mut self, source: impl IonInput) -> IonResult<Macro> {
        self.data_writer.compile_macro(source)
    }

    /// Register a previously compiled `Macro` for use in this `Writer`.
    pub fn register_macro(&mut self, macro_: &Macro) -> IonResult<Macro> {
        self.data_writer.register_macro(macro_.definition())
    }

    /// Gets a macro with the provided ID from the default module.
    pub fn get_macro<'a>(&self, id: impl Into<MacroIdRef<'a>>) -> IonResult<Macro> {
        let id = id.into();
        let macro_table = self.macro_table();

        let qualified_address = match id {
            MacroIdRef::LocalName(name) => {
                let address = macro_table.address_for_id(id).ok_or_else(|| {
                    IonError::illegal_operation(format!(
                        "macro table does not contain a macro named '{name}'"
                    ))
                })?;
                QualifiedAddress::new(ModuleKind::Default, address)
            }
            MacroIdRef::LocalAddress(address) => {
                QualifiedAddress::new(ModuleKind::Default, address)
            }
            MacroIdRef::SystemAddress(address) => {
                QualifiedAddress::new(ModuleKind::System, address.as_usize())
            }
        };

        let macro_table: &MacroTable = match qualified_address.module() {
            ModuleKind::Default => self.macro_table(),
            ModuleKind::System => &ION_1_1_SYSTEM_MACROS,
        };

        let macro_def = macro_table
            .clone_macro_with_address(qualified_address.address())
            .ok_or_else(|| {
                IonError::encoding_error(format!("no macro with the specified ID ({id:?}) found"))
            })?;

        Ok(Macro::new(macro_def, qualified_address))
    }

    pub fn output(&self) -> &Output {
        &self.output
    }

    pub fn output_mut(&mut self) -> &mut Output {
        &mut self.output
    }

    pub fn write<V: WriteAsIon>(&mut self, value: V) -> IonResult<&mut Self> {
        // This method forwards the call to the trait method implementation. It's here so
        // you can call `write()` on an ApplicationWriter without having to import SequenceWriter
        // separately.
        <Self as SequenceWriter>::write(self, value)
    }

    /// Writes bytes of previously encoded values to the output stream.
    pub fn flush(&mut self) -> IonResult<()> {
        if self.symbols.num_pending() > 0 {
            match E::ion_version() {
                IonVersion::v1_0 => self.write_lst_append()?,
                IonVersion::v1_1 => self.write_append_symbols_directive()?,
            }
            self.symbols.reset_num_pending();
        }

        // TODO: In Ion 1.1, new symbols and new macros could be added using the same directive.
        if self.macro_table().num_pending() > 0 {
            self.write_append_macros_directive()?;
            self.macro_table_mut()
                .expect("the pending macro count is >0")
                .reset_num_pending();
        }

        self.directive_writer.flush()?;
        self.output
            .write_all(self.directive_writer.output().as_slice())?;
        self.directive_writer.output_mut().clear();

        self.data_writer.flush()?;
        self.output
            .write_all(self.data_writer.output().as_slice())?;
        self.data_writer.output_mut().clear();

        self.output.flush()?;
        Ok(())
    }

    pub fn close(mut self) -> IonResult<Output> {
        self.flush()?;
        Ok(self.output)
    }

    #[cfg(feature = "experimental-reader-writer")]
    #[inline]
    pub fn symbol_table(&self) -> &SymbolTable {
        &self.symbols
    }

    #[cfg(not(feature = "experimental-reader-writer"))]
    #[inline]
    pub(crate) fn symbol_table(&self) -> &SymbolTable {
        &self.symbols
    }

    /// Helper method to encode an LST append containing pending symbols.
    fn write_lst_append(&mut self) -> IonResult<()> {
        let Self {
            symbols,
            directive_writer,
            ..
        } = self;

        let mut lst = directive_writer
            .value_writer()
            .with_annotations(system_symbol_ids::ION_SYMBOL_TABLE)?
            .struct_writer()?;

        lst.field_writer(system_symbol_ids::IMPORTS)
            .write_symbol(system_symbol_ids::ION_SYMBOL_TABLE)?;

        let mut new_symbol_list = lst.field_writer(system_symbol_ids::SYMBOLS).list_writer()?;

        let pending_symbols = symbols.pending().iter().map(Symbol::text);

        new_symbol_list.write_all(pending_symbols)?;
        new_symbol_list.close()?;

        lst.close()
    }

    fn write_append_macros_directive(&mut self) -> IonResult<()> {
        let Self {
            data_writer,
            directive_writer,
            ..
        } = self;

        let macros = data_writer.macro_table();

        // TODO: Once expression group serialization is complete, this can be replaced by a call
        //       to the `add_macros` system macro.
        let mut directive = directive_writer
            .value_writer()
            .with_annotations("$ion")?
            .sexp_writer()?;

        directive
            .write_symbol(v1_1::system_symbols::MODULE)?
            .write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?;

        let mut symbols_sexp = directive.sexp_writer()?;
        symbols_sexp
            .write_symbol(v1_1::system_symbols::SYMBOL_TABLE)?
            .write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?;
        symbols_sexp.close()?;

        let pending_macros = macros
            .pending()
            .iter()
            // Only user-defined template macros can be added to the macro table.
            .map(|m| m.require_template());

        let mut macro_table = directive.sexp_writer()?;
        macro_table
            .write_symbol(v1_1::system_symbols::MACRO_TABLE)?
            .write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?
            .write_all(pending_macros)?;
        macro_table.close()?;
        directive.close()
    }

    /// Helper method to encode an LST append containing pending symbols.
    fn write_append_symbols_directive(&mut self) -> IonResult<()> {
        let Self {
            symbols,
            directive_writer,
            ..
        } = self;

        let mut directive = directive_writer
            .value_writer()
            .with_annotations(v1_1::system_symbols::ION)?
            .sexp_writer()?;

        directive
            .write_symbol(v1_1::system_symbols::MODULE)?
            .write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?;

        let pending_symbols = symbols.pending().iter().map(Symbol::text);

        let mut symbol_table = directive.sexp_writer()?;
        symbol_table
            .write_symbol(v1_1::system_symbols::SYMBOL_TABLE)?
            .write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?
            .write_list(pending_symbols)?;
        symbol_table.close()?;
        directive.close()
    }
}

impl<E: Encoding, Output: Write> ContextWriter for Writer<E, Output> {
    type NestedValueWriter<'a>
        = ApplicationValueWriter<'a, <E::Writer<Vec<u8>> as ContextWriter>::NestedValueWriter<'a>>
    where
        Self: 'a;
}

impl<E: Encoding, Output: Write> MakeValueWriter for Writer<E, Output> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        let raw_value_writer = self.data_writer.make_value_writer();
        let symbols = &mut self.symbols;

        ApplicationValueWriter {
            raw_value_writer,
            symbols,
            value_writer_config: self.value_writer_config,
        }
    }
}

impl<E: Encoding, Output: Write> SequenceWriter for Writer<E, Output> {
    type Resources = Output;

    fn close(mut self) -> IonResult<Self::Resources> {
        self.flush()?;
        Ok(self.output)
    }
}

pub struct ApplicationValueWriter<'a, V: ValueWriter> {
    symbols: &'a mut WriterSymbolTable,
    raw_value_writer: V,
    value_writer_config: ValueWriterConfig,
}

impl<'a, V: ValueWriter> ApplicationValueWriter<'a, V> {
    pub(crate) fn new(
        symbols: &'a mut WriterSymbolTable,
        value_writer_config: ValueWriterConfig,
        raw_value_writer: V,
    ) -> Self {
        Self {
            symbols,
            value_writer_config,
            raw_value_writer,
        }
    }

    #[cfg(feature = "experimental-reader-writer")]
    #[inline]
    pub fn symbol_table(&self) -> &SymbolTable {
        self.symbols
    }

    #[cfg(not(feature = "experimental-reader-writer"))]
    #[inline]
    pub(crate) fn symbol_table(&self) -> &SymbolTable {
        self.symbols
    }
}

// Generally useful methods, but currently only called in unit tests.
#[cfg_attr(not(feature = "experimental-reader-writer"), allow(dead_code))]
impl ApplicationValueWriter<'_, BinaryValueWriter_1_1<'_, '_>> {
    pub fn with_container_encoding(mut self, container_encoding: ContainerEncoding) -> Self {
        self.value_writer_config = self
            .value_writer_config
            .with_container_encoding(container_encoding);
        self
    }

    pub fn with_annotations_encoding(mut self, annotations_encoding: AnnotationsEncoding) -> Self {
        self.value_writer_config = self
            .value_writer_config
            .with_annotations_encoding(annotations_encoding);
        self
    }

    pub fn with_symbol_value_encoding(
        mut self,
        symbol_value_encoding: SymbolValueEncoding,
    ) -> Self {
        self.value_writer_config = self
            .value_writer_config
            .with_symbol_value_encoding(symbol_value_encoding);
        self
    }

    pub fn with_field_name_encoding(mut self, field_name_encoding: FieldNameEncoding) -> Self {
        self.value_writer_config = self
            .value_writer_config
            .with_field_name_encoding(field_name_encoding);
        self
    }
}

impl<V: ValueWriter> AnnotatableWriter for ApplicationValueWriter<'_, V> {
    type AnnotatedValueWriter<'a>
        = ApplicationValueWriter<'a, V::AnnotatedValueWriter<'a>>
    where
        Self: 'a;

    fn with_annotations<'a>(
        mut self,
        annotations: impl AnnotationSeq<'a>,
    ) -> IonResult<Self::AnnotatedValueWriter<'a>>
    where
        Self: 'a,
    {
        let mut annotations = annotations.into_annotations_vec();
        match self.value_writer_config.annotations_encoding() {
            AnnotationsEncoding::SymbolIds => {
                // Intern all text so everything we write is a symbol ID
                self.intern_all_annotations(&mut annotations)?
            }
            AnnotationsEncoding::InlineText => {
                // Validate the symbol IDs, write the text as-is
                self.validate_all_symbol_ids(&mut annotations)?
            }
            AnnotationsEncoding::NewSymbolsAsInlineText => {
                // Map all known strings to symbol IDs, leave new text as is.
                self.map_known_symbols_to_symbol_ids(&mut annotations)?
            }
        };

        Ok(ApplicationValueWriter {
            symbols: self.symbols,
            raw_value_writer: self.raw_value_writer.with_annotations(annotations)?,
            value_writer_config: self.value_writer_config,
        })
    }
}

impl<V: ValueWriter> ApplicationValueWriter<'_, V> {
    /// Converts each annotation in `annotations` to a symbol ID, adding symbols to the symbol table
    /// as necessary. If one of the annotations is a symbol ID that is not in the symbol table,
    /// returns an `Err`.
    fn intern_all_annotations<'a>(&mut self, annotations: &mut AnnotationsVec<'a>) -> IonResult<()>
    where
        Self: 'a,
    {
        for annotation in annotations {
            match annotation.as_raw_symbol_ref() {
                // The token is already a symbol ID.
                RawSymbolRef::SymbolId(sid) => {
                    if !self.symbol_table().sid_is_valid(sid) {
                        return IonResult::encoding_error(format!(
                            "annotation symbol ID {sid} is out of range"
                        ));
                    }
                }
                RawSymbolRef::SystemSymbol_1_1(_symbol) => {
                    // The system symbol was validated on creation.
                }
                // The token is text...
                RawSymbolRef::Text(text) => {
                    let sid = match self.symbol_table().sid_for(text) {
                        Some(sid) => {
                            //...that was already in the symbol table.
                            sid
                        }
                        None => {
                            // ...that we need to add to the symbol table.
                            self.symbols.add_symbol_for_text(text)
                        }
                    };
                    *annotation = RawSymbolRef::SymbolId(sid);
                }
            };
        }
        Ok(())
    }

    /// Confirms all SIDs are in the symbol table while leaving text annotations as-is.
    pub(crate) fn validate_all_symbol_ids<'a>(
        &mut self,
        annotations: &mut AnnotationsVec<'a>,
    ) -> IonResult<()>
    where
        Self: 'a,
    {
        for annotation in annotations {
            if let RawSymbolRef::SymbolId(sid) = annotation.as_raw_symbol_ref() {
                if !self.symbol_table().sid_is_valid(sid) {
                    return IonResult::encoding_error(format!(
                        "annotation symbol ID {sid} is not in the symbol table"
                    ));
                }
            }
        }
        Ok(())
    }

    /// Converts annotations with known text to their corresponding symbol ID. Annotations with
    /// text not yet in the symbol table are left as-is. If a symbol ID is not in the symbol table,
    /// returns an `Err`.
    fn map_known_symbols_to_symbol_ids<'a>(
        &mut self,
        annotations: &mut AnnotationsVec<'a>,
    ) -> IonResult<()>
    where
        Self: 'a,
    {
        for annotation in annotations {
            match annotation.as_raw_symbol_ref() {
                // The token is already a symbol ID.
                RawSymbolRef::SymbolId(sid) => {
                    if !self.symbol_table().sid_is_valid(sid) {
                        return IonResult::encoding_error(format!(
                            "annotation symbol ID {sid} is out of range"
                        ));
                    }
                }
                RawSymbolRef::SystemSymbol_1_1(_symbol) => {
                    // Symbol was validated on creation, nothing to do.
                }
                // The token is text...
                RawSymbolRef::Text(text) => {
                    match self.symbols.sid_for(text) {
                        Some(sid) => {
                            //...that was already in the symbol table.
                            *annotation = RawSymbolRef::SymbolId(sid);
                        }
                        None => {
                            // ...that is not in the symbol table. Leave it as-is.
                        }
                    };
                }
            };
        }
        Ok(())
    }
}

impl<'value, V: ValueWriter> ValueWriter for ApplicationValueWriter<'value, V> {
    type ListWriter = ApplicationListWriter<'value, V>;
    type SExpWriter = ApplicationSExpWriter<'value, V>;
    type StructWriter = ApplicationStructWriter<'value, V>;
    type EExpWriter = ApplicationEExpWriter<'value, V>;

    delegate! {
        to self.raw_value_writer {
            fn write_null(self, ion_type: IonType) -> IonResult<()> ;
            fn write_bool(self, value: bool) -> IonResult<()>;
            fn write_i64(self, value: i64) -> IonResult<()>;
            fn write_int(self, value: &Int) -> IonResult<()>;
            fn write_f32(self, value: f32) -> IonResult<()>;
            fn write_f64(self, value: f64) -> IonResult<()>;
            fn write_decimal(self, value: &Decimal) -> IonResult<()>;
            fn write_timestamp(self, value: &Timestamp) -> IonResult<()>;
            fn write_string(self, value: impl AsRef<str>) -> IonResult<()>;
            fn write_clob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
            fn write_blob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
        }
    }

    fn write_symbol(self, value: impl AsRawSymbolRef) -> IonResult<()> {
        use RawSymbolRef::*;
        use SymbolValueEncoding::*;

        let Self {
            symbols,
            raw_value_writer,
            value_writer_config,
            ..
        } = self;

        // Depending on the symbol value encoding config option, map the provided symbol reference
        // from text to SID or vice versa, performing any validation needed.
        let symbol_ref = match value.as_raw_symbol_ref() {
            SymbolId(symbol_id) => {
                // We can write the symbol ID as-is. Make sure it's in the symbol table.
                if !symbols.sid_is_valid(symbol_id) {
                    return cold_path!(IonResult::encoding_error(format!(
                        "symbol value ID ${symbol_id} is not in the symbol table"
                    )));
                }
                SymbolId(symbol_id)
            }
            SystemSymbol_1_1(symbol) => SystemSymbol_1_1(symbol),
            Text(text) => {
                match value_writer_config.symbol_value_encoding() {
                    SymbolIds => {
                        // Map the text to a symbol ID.
                        match symbols.sid_for(text) {
                            // If it's already in the symbol table, use that SID.
                            Some(symbol_id) => SymbolId(symbol_id),
                            // Otherwise, add it to the symbol table.
                            None => SymbolId(symbols.add_symbol_for_text(text)),
                        }
                    }
                    NewSymbolsAsInlineText => {
                        // If the text is in the symbol table, use the symbol ID. Otherwise, use the text itself.
                        match symbols.sid_for(text) {
                            Some(symbol_id) => SymbolId(symbol_id),
                            None => Text(text),
                        }
                    }
                    // We have text and we want to write text. Nothing to do.
                    InlineText => Text(text),
                }
            }
        };

        raw_value_writer.write_symbol(symbol_ref)
    }

    fn list_writer(self) -> IonResult<Self::ListWriter> {
        Ok(ApplicationListWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_value_writer.list_writer()?,
        ))
    }

    fn sexp_writer(self) -> IonResult<Self::SExpWriter> {
        Ok(ApplicationSExpWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_value_writer.sexp_writer()?,
        ))
    }

    fn struct_writer(self) -> IonResult<Self::StructWriter> {
        let config = self.value_writer_config;
        Ok(ApplicationStructWriter::new(
            self.symbols,
            config,
            self.raw_value_writer.struct_writer()?,
        ))
    }

    fn eexp_writer<'a>(self, macro_id: impl MacroIdLike<'a>) -> IonResult<Self::EExpWriter>
    where
        Self: 'a,
    {
        Ok(ApplicationEExpWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_value_writer.eexp_writer(macro_id)?,
        ))
    }
}

pub struct ApplicationStructWriter<'value, V: ValueWriter> {
    symbols: &'value mut WriterSymbolTable,
    raw_struct_writer: V::StructWriter,
    value_writer_config: ValueWriterConfig,
}

impl<'value, V: ValueWriter> ApplicationStructWriter<'value, V> {
    pub(crate) fn new(
        symbols: &'value mut WriterSymbolTable,
        config: ValueWriterConfig,
        raw_struct_writer: V::StructWriter,
    ) -> Self {
        Self {
            symbols,
            raw_struct_writer,
            value_writer_config: config,
        }
    }

    // Generally useful, but currently only called in unit tests.
    #[cfg_attr(not(feature = "experimental-reader-writer"), allow(dead_code))]
    pub fn with_field_name_encoding(mut self, field_name_encoding: FieldNameEncoding) -> Self {
        self.value_writer_config = self
            .value_writer_config
            .with_field_name_encoding(field_name_encoding);
        self
    }
}

impl<V: ValueWriter> ContextWriter for ApplicationStructWriter<'_, V> {
    type NestedValueWriter<'a>
        = ApplicationValueWriter<'a, <V::StructWriter as ContextWriter>::NestedValueWriter<'a>>
    where
        Self: 'a;
}

impl<V: ValueWriter> MakeValueWriter for ApplicationStructWriter<'_, V> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        ApplicationValueWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_struct_writer.make_value_writer(),
        )
    }
}

impl<V: ValueWriter> FieldEncoder for ApplicationStructWriter<'_, V> {
    fn encode_field_name(&mut self, name: impl AsRawSymbolRef) -> IonResult<()> {
        let text = match name.as_raw_symbol_ref() {
            // This is an application-level struct writer. It is expected that method calls will
            // almost always be provided text; if the user passes in a symbol ID, it means they have
            // special knowledge of the encoding environment and are bypassing the 'managed' layer.
            // We range check the SID and write it as-is. In the unusual circumstance that the user
            // has a SID at the application level and wants to write text, they can and should
            // resolve the SID in the symbol table before calling this method.
            RawSymbolRef::SymbolId(symbol_id) => {
                if !self.symbols.sid_is_valid(symbol_id) {
                    return cold_path!(IonResult::encoding_error(format!(
                        "symbol ID ${symbol_id} is not in the symbol table"
                    )));
                }
                return self.raw_struct_writer.encode_field_name(symbol_id);
            }
            RawSymbolRef::SystemSymbol_1_1(symbol) => {
                return self.raw_struct_writer.encode_field_name(symbol);
            }
            // Otherwise, get its associated text.
            RawSymbolRef::Text(text) => text,
        };

        // From here on, we're dealing with text.

        // If the struct writer is configured to write field names as text, do that.
        if self.value_writer_config.field_name_encoding() == FieldNameEncoding::InlineText {
            return self.raw_struct_writer.encode_field_name(text);
        }

        // Otherwise, see if the symbol is already in the symbol table.
        let token: RawSymbolRef<'_> = match self.symbols.sid_for(text) {
            // If so, use the existing ID.
            Some(sid) => sid.into(),
            // If it's not but the struct writer is configured to intern new text, add it to the
            // symbol table.
            None if self.value_writer_config.field_name_encoding()
                == FieldNameEncoding::SymbolIds =>
            {
                self.symbols.add_symbol_for_text(text).into()
            }
            // Otherwise, we'll write the text as-is.
            None => text.into(),
        };

        // Finally, encode the field name using the selected token representation
        self.raw_struct_writer.encode_field_name(token)
    }
}

impl<V: ValueWriter> StructWriter for ApplicationStructWriter<'_, V> {
    fn field_writer<'a>(&'a mut self, name: impl Into<RawSymbolRef<'a>>) -> FieldWriter<'a, Self> {
        FieldWriter::new(name.into(), self.value_writer_config, self)
    }

    fn close(self) -> IonResult<()> {
        self.raw_struct_writer.close()
    }

    fn config(&self) -> ValueWriterConfig {
        self.value_writer_config
    }
}

pub struct ApplicationListWriter<'value, V: ValueWriter> {
    symbols: &'value mut WriterSymbolTable,
    raw_list_writer: V::ListWriter,
    value_writer_config: ValueWriterConfig,
}

impl<'value, V: ValueWriter> ApplicationListWriter<'value, V> {
    pub(crate) fn new(
        symbols: &'value mut WriterSymbolTable,
        value_writer_config: ValueWriterConfig,
        raw_list_writer: V::ListWriter,
    ) -> Self {
        Self {
            symbols,
            value_writer_config,
            raw_list_writer,
        }
    }
}

impl<V: ValueWriter> ContextWriter for ApplicationListWriter<'_, V> {
    type NestedValueWriter<'a>
        = ApplicationValueWriter<'a, <V::ListWriter as ContextWriter>::NestedValueWriter<'a>>
    where
        Self: 'a;
}

impl<V: ValueWriter> MakeValueWriter for ApplicationListWriter<'_, V> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        ApplicationValueWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_list_writer.make_value_writer(),
        )
    }
}

impl<V: ValueWriter> SequenceWriter for ApplicationListWriter<'_, V> {
    type Resources = ();

    fn close(self) -> IonResult<Self::Resources> {
        self.raw_list_writer.close()
    }
}

pub struct ApplicationSExpWriter<'value, V: ValueWriter> {
    symbols: &'value mut WriterSymbolTable,
    raw_sexp_writer: V::SExpWriter,
    value_writer_config: ValueWriterConfig,
}

impl<'value, V: ValueWriter> ApplicationSExpWriter<'value, V> {
    pub(crate) fn new(
        symbols: &'value mut WriterSymbolTable,
        value_writer_config: ValueWriterConfig,
        raw_sexp_writer: V::SExpWriter,
    ) -> Self {
        Self {
            symbols,
            value_writer_config,
            raw_sexp_writer,
        }
    }
}

impl<V: ValueWriter> ContextWriter for ApplicationSExpWriter<'_, V> {
    type NestedValueWriter<'a>
        = ApplicationValueWriter<'a, <V::SExpWriter as ContextWriter>::NestedValueWriter<'a>>
    where
        Self: 'a;
}

impl<V: ValueWriter> MakeValueWriter for ApplicationSExpWriter<'_, V> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        ApplicationValueWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_sexp_writer.make_value_writer(),
        )
    }
}

impl<V: ValueWriter> SequenceWriter for ApplicationSExpWriter<'_, V> {
    type Resources = ();

    fn close(self) -> IonResult<Self::Resources> {
        self.raw_sexp_writer.close()
    }
}

pub struct ApplicationEExpWriter<'value, V: ValueWriter> {
    symbols: &'value mut WriterSymbolTable,
    raw_eexp_writer: V::EExpWriter,
    value_writer_config: ValueWriterConfig,
}

impl<'value, V: ValueWriter> ApplicationEExpWriter<'value, V> {
    pub(crate) fn new(
        symbols: &'value mut WriterSymbolTable,
        value_writer_config: ValueWriterConfig,
        raw_eexp_writer: V::EExpWriter,
    ) -> Self {
        Self {
            symbols,
            value_writer_config,
            raw_eexp_writer,
        }
    }

    /// Returns a reference to the macro signature parameter for which the next argument corresponds.
    /// If no more parameters remain in the signature, returns `None`.
    pub fn current_parameter(&self) -> Option<&Parameter> {
        self.raw_eexp_writer.current_parameter()
    }

    /// Helper method. If there are no more parameters, returns `Err`. Otherwise, returns
    /// `Ok(next_parameter)`.
    #[inline]
    fn expect_next_parameter(&mut self) -> IonResult<&Parameter> {
        self.raw_eexp_writer.expect_next_parameter()
    }
}

impl<V: ValueWriter> SequenceWriter for ApplicationEExpWriter<'_, V> {
    type Resources = ();

    /// Writes a value in the current context (list, s-expression, or stream) and upon success
    /// returns another reference to `self` to enable method chaining.
    fn write<Value: WriteAsIon>(&mut self, value: Value) -> IonResult<&mut Self> {
        value.write_as_ion(self.make_value_writer())?;
        Ok(self)
    }

    fn close(self) -> IonResult<Self::Resources> {
        self.raw_eexp_writer.close()
    }
}

impl<V: ValueWriter> ContextWriter for ApplicationEExpWriter<'_, V> {
    type NestedValueWriter<'a>
        = ApplicationValueWriter<
        'a,
        <<V as ValueWriter>::EExpWriter as ContextWriter>::NestedValueWriter<'a>,
    >
    where
        Self: 'a;
}

impl<V: ValueWriter> MakeValueWriter for ApplicationEExpWriter<'_, V> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        ApplicationValueWriter::new(
            self.symbols,
            self.value_writer_config,
            self.raw_eexp_writer.make_value_writer(),
        )
    }
}

impl<V: ValueWriter> EExpWriterInternal for ApplicationEExpWriter<'_, V> {
    fn expect_next_parameter(&mut self) -> IonResult<&Parameter> {
        Self::expect_next_parameter(self) // Delegate to the inherent impl
    }
}

impl<V: ValueWriter> EExpWriter for ApplicationEExpWriter<'_, V> {
    type ExprGroupWriter<'group>
        = <V::EExpWriter as EExpWriter>::ExprGroupWriter<'group>
    where
        Self: 'group;

    fn invoked_macro(&self) -> MacroRef<'_> {
        self.raw_eexp_writer.invoked_macro()
    }

    fn current_parameter(&self) -> Option<&Parameter> {
        Self::current_parameter(self) // Delegate to the inherent impl
    }

    fn write_flex_uint(&mut self, value: impl Into<UInt>) -> IonResult<()> {
        self.expect_next_parameter()
            .and_then(|p| p.expect_encoding(&ParameterEncoding::FlexUInt))?;
        self.raw_eexp_writer.write_flex_uint(value)
    }

    fn write_fixed_uint8(&mut self, value: impl Into<u8>) -> IonResult<()> {
        self.expect_next_parameter()
            .and_then(|p| p.expect_encoding(&ParameterEncoding::UInt8))?;
        self.raw_eexp_writer.write_fixed_uint8(value)
    }

    fn expr_group_writer(&mut self) -> IonResult<Self::ExprGroupWriter<'_>> {
        let _param = self
            .expect_next_parameter()
            .and_then(|p| p.expect_variadic())?;
        // TODO: Pass `Parameter` to group writer so it can do its own validation
        self.raw_eexp_writer.expr_group_writer()
    }
}

impl<S: SequenceWriter> ElementWriter for S {
    fn write_value(&mut self, value: &Value) -> IonResult<()> {
        self.write(value)?;
        Ok(())
    }

    fn write_element(&mut self, element: &Element) -> IonResult<()> {
        self.write(element)?;
        Ok(())
    }
}

#[cfg(feature = "experimental-ion-1-1")]
#[cfg(test)]
mod tests {
    use crate::lazy::encoder::value_writer::AnnotatableWriter;
    use crate::lazy::encoder::value_writer_config::{AnnotationsEncoding, SymbolValueEncoding};
    use crate::raw_symbol_ref::AsRawSymbolRef;
    use crate::{
        v1_0, v1_1, EExpWriter, Element, FieldNameEncoding, HasSpan, IonResult, LazyRawValue,
        RawSymbolRef, SequenceWriter, StructWriter, SystemReader, TextFormat, ValueWriter, Writer,
    };
    use std::io::BufWriter;

    fn symbol_value_encoding_test<const N: usize, A: AsRawSymbolRef>(
        encoding: SymbolValueEncoding,
        symbol_and_encoding_pairs: [(A, &[u8]); N],
    ) -> IonResult<()> {
        let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
        for (symbol, _expected_bytes) in &symbol_and_encoding_pairs {
            writer
                .value_writer()
                .with_symbol_value_encoding(encoding)
                .write_symbol(symbol)?;
        }
        let bytes = writer.close()?;
        let mut reader = SystemReader::new(v1_1::Binary, bytes.as_slice());
        for (symbol, expected_bytes) in &symbol_and_encoding_pairs {
            let value = reader.expect_next_value()?;
            let raw_value = value.raw().unwrap();
            let actual_bytes = raw_value.span().bytes();
            assert_eq!(
                actual_bytes, *expected_bytes,
                "actual {actual_bytes:02X?} != expected {expected_bytes:02X?}",
            );
            println!(
                "{:?} {:02X?} == {:02X?}",
                symbol.as_raw_symbol_ref(),
                actual_bytes,
                expected_bytes
            )
        }
        Ok(())
    }

    #[test]
    fn intern_new_symbol_values() -> IonResult<()> {
        use RawSymbolRef::*;
        symbol_value_encoding_test(
            SymbolValueEncoding::SymbolIds,
            [
                (Text("$ion_symbol_table"), &[0xE1, 0x03]),
                (Text("name"), &[0xE1, 0x04]),
                (SymbolId(6), &[0xE1, 0x06]), // SIDs are written as-is
                (Text("foo"), &[0xE1, 0x40]), // Text is added to the symbol table and encoded as a SID
            ],
        )
    }

    #[test]
    fn do_not_intern_new_symbol_values() -> IonResult<()> {
        use RawSymbolRef::*;
        symbol_value_encoding_test(
            SymbolValueEncoding::NewSymbolsAsInlineText,
            [
                // Known text symbols are written as SIDs
                (Text("$ion_symbol_table"), &[0xE1, 0x03]),
                (Text("name"), &[0xE1, 0x04]),
                // SIDs are written as-is
                (SymbolId(6), &[0xE1, 0x06]),
                // New text symbols are written as inline text
                //                 f     o     o
                (Text("foo"), &[0xA3, 0x66, 0x6F, 0x6F]),
            ],
        )
    }

    #[test]
    fn encode_all_text_as_is() -> IonResult<()> {
        use RawSymbolRef::*;
        symbol_value_encoding_test(
            SymbolValueEncoding::InlineText,
            [
                // Known text symbols are written as inline text
                (Text("name"), &[0xA4, 0x6E, 0x61, 0x6D, 0x65]),
                // SIDs are written as-is
                (SymbolId(6), &[0xE1, 0x06]),
                // New text symbols are written as inline text
                //                 f     o     o
                (Text("foo"), &[0xA3, 0x66, 0x6F, 0x6F]),
            ],
        )
    }

    fn annotations_sequence_encoding_test(
        encoding: AnnotationsEncoding,
        sequence: &[RawSymbolRef<'_>],
        expected_encoding: &[u8],
    ) -> IonResult<()> {
        let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
        writer
            .value_writer()
            .with_annotations_encoding(encoding)
            .with_annotations(sequence)?
            .write_string("foo")?;
        let bytes = writer.close()?;
        let mut reader = SystemReader::new(v1_1::Binary, bytes.as_slice());
        let value = reader.expect_next_value()?;
        let raw_value = value.raw().unwrap();
        let annotations = raw_value.annotations_span();
        assert_eq!(
            annotations.bytes(),
            expected_encoding,
            "{:02X?} != {:02X?}",
            annotations.bytes(),
            expected_encoding
        );
        Ok(())
    }

    #[test]
    fn intern_new_annotations() -> IonResult<()> {
        use RawSymbolRef::*;
        annotations_sequence_encoding_test(
            AnnotationsEncoding::SymbolIds,
            &[
                Text("$ion_symbol_table"),
                Text("name"),
                SymbolId(6),
                Text("foo"),
            ],
            &[
                0xE9, // Opcode: FlexUInt follows with byte length of sequence
                0x0B, // FlexUInt byte length: 4
                0x07, // FlexSym SID $3
                0x09, // FlexSym SID $4
                0x0D, // FlexSym SID $6
                0x02, 0x01, // FlexSym SID $64
            ],
        )
    }

    #[test]
    fn write_new_annotations_as_text() -> IonResult<()> {
        use RawSymbolRef::*;
        annotations_sequence_encoding_test(
            AnnotationsEncoding::NewSymbolsAsInlineText,
            &[
                Text("$ion_symbol_table"),
                Text("name"),
                SymbolId(6),
                Text("foo"),
            ],
            &[
                0xE9, // Opcode: FlexUInt follows with byte length of sequence
                0x0F, // FlexUInt byte length: 7
                0x07, // FlexSym: SID $3
                0x09, // FlexSym: SID $4
                0x0D, // FlexSym: SID $6
                0xFB, // FlexSym: 3 UTF-8 bytes
                // f     o     o
                0x66, 0x6F, 0x6F,
            ],
        )
    }

    #[test]
    #[rustfmt::skip]
    fn write_text_annotations_as_is() -> IonResult<()> {
        use RawSymbolRef::*;
        annotations_sequence_encoding_test(
            AnnotationsEncoding::InlineText,
            &[Text("name"), SymbolId(6), Text("foo")],
            &[
                0xE9, // Opcode: FlexUInt follows with byte length of sequence
                0x15, // FlexUInt byte length: 10
                0xF9, // FlexSym: 4 UTF-8 bytes
                // n     a     m     e
                0x6E, 0x61, 0x6D, 0x65,
                0x0D, // FlexSym: SID $6
                0xFB, // FlexSym: 3 UTF-8 bytes
                // f     o     o
                0x66, 0x6F, 0x6F,
            ],
        )
    }

    /// Writes a struct with all of the provided field names using the requested field name encoding.
    /// For simplicity, the value for each field is the integer 0.
    fn struct_field_encoding_test(
        encoding: FieldNameEncoding,
        field_names_and_encodings: &[(RawSymbolRef<'_>, &[u8])],
    ) -> IonResult<()> {
        // Configure a struct writer that uses the requested field name encoding
        let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
        let mut struct_writer = writer
            .value_writer()
            .struct_writer()?
            .with_field_name_encoding(encoding);

        for (name, _) in field_names_and_encodings {
            struct_writer.write(name, /* same value for every field*/ 0)?;
        }
        struct_writer.close()?;
        let bytes = writer.close()?;

        println!("encoded bytes: {bytes:02X?}");

        let mut reader = SystemReader::new(v1_1::Binary, bytes.as_slice());
        let struct_ = reader.expect_next_value()?.read()?.expect_struct()?;
        for (field, (_name, expected_encoding)) in
            struct_.iter().zip(field_names_and_encodings.iter())
        {
            let raw_name = field?.raw_name().unwrap();
            let raw_name_encoding = raw_name.span().bytes();
            assert_eq!(
                raw_name_encoding, *expected_encoding,
                "actual {:#02X?}\n!=\nexpected {:#02X?}",
                raw_name_encoding, *expected_encoding
            );
        }

        Ok(())
    }

    #[test]
    fn intern_all_field_names() -> IonResult<()> {
        struct_field_encoding_test(
            FieldNameEncoding::SymbolIds,
            &[
                // New symbols
                (RawSymbolRef::Text("foo"), &[0x81]), // FlexUInt SID $64,
                (RawSymbolRef::Text("bar"), &[0x83]), // FlexUInt SID $65,
                (RawSymbolRef::Text("baz"), &[0x85]), // FlexUInt SID $66,
                // Symbols that are already in the symbol table
                (RawSymbolRef::Text("name"), &[0x09]), // FlexUInt SID $4,
                (RawSymbolRef::Text("foo"), &[0x81]),  // FlexUInt SID $64,
            ],
        )
    }

    #[test]
    fn write_all_field_names_as_text() -> IonResult<()> {
        struct_field_encoding_test(
            FieldNameEncoding::InlineText,
            &[
                // New symbols
                (RawSymbolRef::Text("foo"), &[0xFB, 0x66, 0x6F, 0x6F]), // FlexSym -3, "foo"
                (RawSymbolRef::Text("bar"), &[0xFB, 0x62, 0x61, 0x72]), // FlexSym -3, "bar"
                (RawSymbolRef::Text("baz"), &[0xFB, 0x62, 0x61, 0x7A]), // FlexSym -3, "baz"
                // Symbols that are already in the symbol table are still written as text
                (RawSymbolRef::Text("name"), &[0xF9, 0x6E, 0x61, 0x6D, 0x65]), // FlexSym -4, "name"
            ],
        )
    }

    #[test]
    fn write_new_field_names_as_text() -> IonResult<()> {
        struct_field_encoding_test(
            FieldNameEncoding::NewSymbolsAsInlineText,
            &[
                // New symbols
                (RawSymbolRef::Text("foo"), &[0xFB, 0x66, 0x6F, 0x6F]), // FlexSym -3, "foo"
                (RawSymbolRef::Text("bar"), &[0xFB, 0x62, 0x61, 0x72]), // FlexSym -3, "bar"
                (RawSymbolRef::Text("baz"), &[0xFB, 0x62, 0x61, 0x7A]), // FlexSym -3, "baz"
                // Symbols that are already in the symbol table are written as SIDs
                (RawSymbolRef::Text("name"), &[0x09]), // FlexSym 4, SID $4,
            ],
        )
    }

    #[test]
    fn define_new_macro() -> IonResult<()> {
        let mut writer = Writer::new(v1_1::Text.with_format(TextFormat::Lines), Vec::new())?;

        // Define a macro
        let identity = writer.compile_macro("(macro identity (x*) (%x))")?;

        // Invoke that macro
        let mut eexp_writer = writer.eexp_writer(&identity)?;
        let mut group_writer = eexp_writer.expr_group_writer()?;
        group_writer.write_all(["foo", "bar", "baz"])?;
        group_writer.close()?;
        eexp_writer.close()?;

        writer.flush()?;

        // Confirm the output is as expected
        let output = writer.output().as_slice();

        println!("output:\n{}", std::str::from_utf8(output).unwrap());

        let actual = Element::read_all(output)?;

        let expected = Element::read_all(
            r#"
            "foo"
            "bar"
            "baz"
        "#,
        )?;

        assert_eq!(
            actual, expected,
            "// actual\n{actual:?}\n// !=\n// expected\n{expected:?}"
        );

        Ok(())
    }

    #[test]
    fn flush_underlying_sink() -> IonResult<()> {
        // The final output destination
        let mut buffer: Vec<u8> = Vec::new();
        // A BufWriter that, when flushed, writes bytes to the final output destination
        let buf_writer = BufWriter::new(&mut buffer);
        // An Ion Writer that writes bytes to the buf_writer
        let mut writer = Writer::new(v1_0::Binary, buf_writer)?;
        let len_before_flush = writer.output().get_ref().len();
        // Write some data
        writer.write([1, 2, 3, 4, 5])?;
        // At this point, we've written some data but haven't flushed the inner `buf_writer`,
        // so the length of its underlying `Vec<u8>` should remain unchanged.
        assert_eq!(writer.output().get_ref().len(), len_before_flush);
        // Flush the Ion writer. This should in turn flush the buf_writer, causing bytes to be
        // written to the underlying `Vec<u8>`.
        writer.flush()?;
        // Make sure that this caused the encoded bytes to reach the `Vec<u8>`.
        assert!(writer.output().get_ref().len() > len_before_flush);
        Ok(())
    }

    mod eexp_parameter_validation {
        use super::*;
        use num_traits::{PrimInt, Unsigned};
        use rstest::*;

        #[test]
        fn accept_valid_parameter_encoding() -> IonResult<()> {
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro("(macro foo (a flex_uint::b) (.values (%a) (%b)))")?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // The argument passed as parameter `a` is "hello"
            eexp_writer.write("hello")?;
            // The argument passed as parameter `b` is 42
            eexp_writer.write_flex_uint(42usize)?;
            eexp_writer.close()?;
            let bytes = writer.close()?;
            // Reading the encoded data back, we get the expected output.
            let actual = Element::read_all(&bytes)?;
            let expected = Element::read_all("\"hello\" 42")?;
            assert_eq!(actual, expected);
            Ok(())
        }

        #[test]
        fn tagged_parameter_rejects_flex_uint() -> IonResult<()> {
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro("(macro foo (a flex_uint::b) (.values (%a) (%b)))")?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // Attempt to write a FlexUInt where a tagged value is required, resulting in an error.
            assert!(eexp_writer.write_flex_uint(42usize).is_err());
            Ok(())
        }

        #[test]
        fn flex_uint_parameter_rejects_tagged_value() -> IonResult<()> {
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro("(macro foo (a flex_uint::b) (.values (%a) (%b)))")?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            eexp_writer.write("hello")?;
            // Attempt to write a tagged value where a FlexUInt is required, resulting in an error.
            assert!(eexp_writer.write("world").is_err());
            Ok(())
        }

        #[test]
        fn exactly_one_parameter_rejects_expr_group() -> IonResult<()> {
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro("(macro foo (a) (%a)))")?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // Attempt to start an expression group for parameter `a`, which has a cardinality of
            // exactly-one.
            assert!(eexp_writer.expr_group_writer().is_err());
            Ok(())
        }

        #[test]
        fn zero_or_more_parameter_rejects_tagged_value() -> IonResult<()> {
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro("(macro foo (a*) (%a)))")?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // Attempt to write a tagged value for parameter `a`, which has a cardinality of
            // zero-or-more, and therefore requires an expression group.
            assert!(eexp_writer.write("hello").is_err());
            Ok(())
        }

        #[test]
        fn tagless_uint8_encoding() -> IonResult<()> {
            let macro_source = "(macro foo (uint8::x) (%x))";
            #[rustfmt::skip]
            let expected: &[u8] = &[
                0xE0, 0x01, 0x01, 0xEA,                       // IVM
                0xE7, 0xF9, 0x24, 0x69, 0x6F, 0x6E,           // $ion::
                0xFC, 0x55, 0xEE, 0x10, 0xA1, 0x5F,           // (module _
                0xC4, 0xEE, 0x0F, 0xA1, 0x5F,                 //   (symbol_table _ )
                0xFC, 0x3F, 0xEE, 0x0E, 0xA1, 0x5F,           //   (macro_table _
                0xFC, 0x33,                                   //      (
                0xA5, 0x6d, 0x61, 0x63, 0x72, 0x6F,           //        macro
                0xA3, 0x66, 0x6F, 0x6F,                       //        foo
                0xC9,                                         //        (
                0xE7, 0xF7, 0x75, 0x69, 0x6E, 0x74, 0x38,     //          uint8::
                0xA1, 0x78,                                   //          x )
                0xC4, 0xA1, 0x25, 0xA1, 0x78,                 //        ('%' x))))
                0x18, 0x05,                                   //  (:foo 5)

            ];

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // eexp_writer should do the "right thing" given the parameter's encoding.
            eexp_writer.write_int(&5.into())?;
            let _ = eexp_writer.close();
            let output = writer.close()?;
            assert_eq!(output.as_slice(), expected);

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // eexp_writer should do the "right thing" given the parameter's encoding.
            eexp_writer.write_i64(5i64)?;
            let _ = eexp_writer.close();
            let output = writer.close()?;
            assert_eq!(output.as_slice(), expected);

            Ok(())
        }

        #[test]
        fn tagless_uint8_encoding_fails() -> IonResult<()> {
            let macro_source = "(macro foo (uint8::x) (%x))";

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // eexp_writer should do the "right thing" given the parameter's encoding.
            let result = eexp_writer.write_int(&1024.into());
            // the "right thing" should be to error, since `x` can only be an 8bit unsigned int.
            assert!(result.is_err(), "unexpected success");

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // eexp_writer should do the "right thing" given the parameter's encoding.
            let result = eexp_writer.write_i64(1024.into());
            // the "right thing" should be to error, since `x` can only be an 8bit unsigned int.
            assert!(result.is_err(), "unexpected success");

            Ok(())
        }

        #[rstest]
        #[case::uint8("(macro foo (uint8::x) (%x))", 5, "5")]
        #[case::uint16("(macro foo (uint16::x) (%x))", 5, "5")]
        #[case::uint32("(macro foo (uint32::x) (%x))", 5, "5")]
        #[case::uint64("(macro foo (uint64::x) (%x))", 5, "5")]
        fn tagless_uint_encoding(
            #[case] macro_source: &str,
            #[case] input: i64,
            #[case] expected: &str,
        ) -> IonResult<()> {
            use crate::{Element, Int};

            // write_int

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            let int: Int = input.into();
            eexp_writer.write_int(&int)?;
            eexp_writer.close()?;

            let output = writer.close()?;
            let actual = Element::read_all(&output)?;
            let exp_elem = Element::read_all(expected)?;
            assert_eq!(actual, exp_elem);

            // write_i64

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            eexp_writer.write_i64(input)?;
            eexp_writer.close()?;

            let output = writer.close()?;
            let actual = Element::read_all(&output)?;
            let exp_elem = Element::read_all(expected)?;
            assert_eq!(actual, exp_elem);

            Ok(())
        }

        #[rstest]
        #[case::uint8("(macro foo (uint8::x) (%x))", 5u8)]
        #[case::uint16("(macro foo (uint16::x) (%x))", 5u16)]
        #[case::uint32("(macro foo (uint32::x) (%x))", 5u32)]
        #[case::uint64("(macro foo (uint64::x) (%x))", 5u64)]
        fn tagless_uint_encoding_write_int_fails<T: PrimInt + Unsigned>(
            #[case] macro_source: &str,
            #[case] input: T,
        ) -> IonResult<()> {
            let max_int = T::max_value();
            let max_int_plus_one = num_traits::cast::cast::<_, i128>(max_int).unwrap() + 1i128;
            let neg_input = -num_traits::cast::cast::<_, i128>(input).unwrap();

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            let result = eexp_writer.write_int(&max_int_plus_one.into());
            assert!(result.is_err(), "unexpected success");

            // Ensure we cannot write a negative value..
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            let result = eexp_writer.write_int(&neg_input.into());
            assert!(result.is_err(), "unexpected success");

            Ok(())
        }

        #[rstest]
        #[case::uint8("(macro foo (uint8::x) (%x))", 5u8)]
        #[case::uint16("(macro foo (uint16::x) (%x))", 5u16)]
        #[case::uint32("(macro foo (uint32::x) (%x))", 5u32)]
        fn tagless_uint_encoding_write_i64_fails<T: PrimInt + Unsigned>(
            #[case] macro_source: &str,
            #[case] input: T,
        ) -> IonResult<()> {
            let max_int = T::max_value();
            let max_int_plus_one = num_traits::cast::cast::<_, i128>(max_int).unwrap() + 1i128;
            let neg_input = -num_traits::cast::cast::<_, i128>(input).unwrap();

            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            // eexp_writer should do the "right thing" given the parameter's encoding.
            let result = eexp_writer.write_i64(max_int_plus_one.try_into().unwrap());
            // the "right thing" should be to error, since `x` can only be an 8bit unsigned int.
            assert!(result.is_err(), "unexpected success");

            // Ensure we cannot write a negative value..
            let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
            let foo = writer.compile_macro(macro_source)?;
            let mut eexp_writer = writer.eexp_writer(&foo)?;
            let result = eexp_writer.write_i64(neg_input.try_into().unwrap());
            assert!(result.is_err(), "unexpected success");

            Ok(())
        }
    }
}