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
use crate::lazy::encoder::annotation_seq::AnnotationSeq;
use crate::lazy::encoder::text::v1_0::value_writer::{
    TextAnnotatedValueWriter_1_0, TextContainerWriter_1_0, TextListWriter_1_0, TextSExpWriter_1_0,
    TextStructWriter_1_0, TextValueWriter_1_0,
};
use crate::lazy::encoder::text::v1_0::writer::LazyRawTextWriter_1_0;
use crate::lazy::encoder::value_writer::internal::{
    EExpWriterInternal, FieldEncoder, MakeValueWriter,
};
use crate::lazy::encoder::value_writer::{
    AnnotatableWriter, EExpWriter, SequenceWriter, StructWriter, ValueWriter,
};
use crate::lazy::expanded::macro_table::MacroRef;
use crate::lazy::expanded::template::{Parameter, SignatureIterator};
use crate::lazy::text::raw::v1_1::reader::{MacroIdLike, MacroIdRef};
use crate::raw_symbol_ref::AsRawSymbolRef;
use crate::result::IonFailure;
use crate::types::{ContainerType, ParentType};
use crate::{
    v1_1, ContextWriter, Decimal, Encoding, Int, IonResult, IonType, MacroTable, Timestamp,
    ValueWriterConfig,
};
use compact_str::format_compact;
use delegate::delegate;
use std::io::Write;

pub struct TextValueWriter_1_1<'value, W: Write> {
    pub(crate) value_writer_1_0: TextValueWriter_1_0<'value, W>,
    pub(crate) macros: &'value MacroTable,
}

pub struct TextAnnotatedValueWriter_1_1<'value, W: Write> {
    value_writer_1_0: TextAnnotatedValueWriter_1_0<'value, W>,
    macros: &'value MacroTable,
}

impl<'value, W: Write + 'value> AnnotatableWriter for TextValueWriter_1_1<'value, W> {
    type AnnotatedValueWriter<'a>
        = TextAnnotatedValueWriter_1_1<'a, W>
    where
        Self: 'a;

    fn with_annotations<'a>(
        self,
        annotations: impl AnnotationSeq<'a>,
    ) -> IonResult<Self::AnnotatedValueWriter<'a>>
    where
        Self: 'a,
    {
        Ok(TextAnnotatedValueWriter_1_1 {
            value_writer_1_0: self.value_writer_1_0.with_annotations(annotations)?,
            macros: self.macros,
        })
    }
}

impl<'value, W: Write + 'value> ValueWriter for TextValueWriter_1_1<'value, W> {
    type ListWriter = TextListWriter_1_1<'value, W>;
    type SExpWriter = TextSExpWriter_1_1<'value, W>;
    type StructWriter = TextStructWriter_1_1<'value, W>;
    type EExpWriter = TextEExpWriter_1_1<'value, W>;

    // For all of the scalars, delegate to the existing 1.0 writing logic.
    delegate! {
        to self.value_writer_1_0 {
            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_symbol(self, value: impl AsRawSymbolRef) -> IonResult<()>;
            fn write_clob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
            fn write_blob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
        }
    }

    fn list_writer(self) -> IonResult<Self::ListWriter> {
        Ok(TextListWriter_1_1 {
            writer_1_0: self.value_writer_1_0.list_writer()?,
            macros: self.macros,
        })
    }

    fn sexp_writer(self) -> IonResult<Self::SExpWriter> {
        Ok(TextSExpWriter_1_1 {
            writer_1_0: self.value_writer_1_0.sexp_writer()?,
            macros: self.macros,
        })
    }

    fn struct_writer(self) -> IonResult<Self::StructWriter> {
        Ok(TextStructWriter_1_1 {
            writer_1_0: self.value_writer_1_0.struct_writer()?,
            macros: self.macros,
        })
    }

    fn eexp_writer<'a>(self, macro_id: impl MacroIdLike<'a>) -> IonResult<Self::EExpWriter>
    where
        Self: 'a,
    {
        let macro_ref = macro_id.resolve(self.macros)?;
        let opening_text = match macro_id.prefer_name() {
            MacroIdRef::LocalName(name) => format_compact!("(:{} ", name),
            MacroIdRef::LocalAddress(address) => format_compact!("(:{} ", address),
            MacroIdRef::SystemAddress(system_address) => {
                format_compact!("(:$ion::{} ", system_address.as_usize())
            }
        };
        TextEExpWriter_1_1::new(
            self.value_writer_1_0.writer,
            self.value_writer_1_0.depth,
            self.value_writer_1_0.parent_type,
            // Pretend we're in a sexp for syntax purposes
            opening_text.as_str(),
            self.macros,
            macro_ref,
        )
    }
}

impl<'value, W: Write + 'value> AnnotatableWriter for TextAnnotatedValueWriter_1_1<'value, W> {
    type AnnotatedValueWriter<'a>
        = TextAnnotatedValueWriter_1_1<'a, W>
    where
        Self: 'a;

    fn with_annotations<'a>(
        self,
        annotations: impl AnnotationSeq<'a>,
    ) -> IonResult<Self::AnnotatedValueWriter<'a>>
    where
        Self: 'a,
    {
        Ok(TextAnnotatedValueWriter_1_1 {
            value_writer_1_0: self.value_writer_1_0.with_annotations(annotations)?,
            macros: self.macros,
        })
    }
}

impl<'value, W: Write + 'value> ValueWriter for TextAnnotatedValueWriter_1_1<'value, W> {
    type ListWriter = TextListWriter_1_1<'value, W>;
    type SExpWriter = TextSExpWriter_1_1<'value, W>;
    type StructWriter = TextStructWriter_1_1<'value, W>;
    type EExpWriter = TextEExpWriter_1_1<'value, W>;
    // For all of the scalars, delegate to the existing 1.0 writing logic.
    delegate! {
        to self.value_writer_1_0 {
            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_symbol(self, value: impl AsRawSymbolRef) -> IonResult<()>;
            fn write_clob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
            fn write_blob(self, value: impl AsRef<[u8]>) -> IonResult<()>;
        }
    }

    fn list_writer(self) -> IonResult<Self::ListWriter> {
        Ok(TextListWriter_1_1 {
            writer_1_0: self.value_writer_1_0.list_writer()?,
            macros: self.macros,
        })
    }

    fn sexp_writer(self) -> IonResult<Self::SExpWriter> {
        Ok(TextSExpWriter_1_1 {
            writer_1_0: self.value_writer_1_0.sexp_writer()?,
            macros: self.macros,
        })
    }

    fn struct_writer(self) -> IonResult<Self::StructWriter> {
        Ok(TextStructWriter_1_1 {
            writer_1_0: self.value_writer_1_0.struct_writer()?,
            macros: self.macros,
        })
    }

    fn eexp_writer<'a>(self, _macro_id: impl MacroIdLike<'a>) -> IonResult<Self::EExpWriter> {
        IonResult::encoding_error("e-expressions cannot have annotations")
    }
}

pub struct TextListWriter_1_1<'value, W: Write> {
    writer_1_0: TextListWriter_1_0<'value, W>,
    macros: &'value MacroTable,
}

impl<W: Write> ContextWriter for TextListWriter_1_1<'_, W> {
    type NestedValueWriter<'a>
        = TextValueWriter_1_1<'a, W>
    where
        Self: 'a;
}

impl<W: Write> MakeValueWriter for TextListWriter_1_1<'_, W> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        TextValueWriter_1_1 {
            value_writer_1_0: self.writer_1_0.make_value_writer(),
            macros: self.macros,
        }
    }
}

impl<W: Write> SequenceWriter for TextListWriter_1_1<'_, W> {
    type Resources = ();

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

pub struct TextSExpWriter_1_1<'value, W: Write> {
    writer_1_0: TextSExpWriter_1_0<'value, W>,
    macros: &'value MacroTable,
}

impl<W: Write> ContextWriter for TextSExpWriter_1_1<'_, W> {
    type NestedValueWriter<'a>
        = TextValueWriter_1_1<'a, W>
    where
        Self: 'a;
}

impl<W: Write> MakeValueWriter for TextSExpWriter_1_1<'_, W> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        TextValueWriter_1_1 {
            value_writer_1_0: self.writer_1_0.make_value_writer(),
            macros: self.macros,
        }
    }
}

impl<W: Write> SequenceWriter for TextSExpWriter_1_1<'_, W> {
    type Resources = ();

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

pub struct TextStructWriter_1_1<'value, W: Write> {
    writer_1_0: TextStructWriter_1_0<'value, W>,
    macros: &'value MacroTable,
}

impl<W: Write> FieldEncoder for TextStructWriter_1_1<'_, W> {
    fn encode_field_name(&mut self, name: impl AsRawSymbolRef) -> IonResult<()> {
        self.writer_1_0.encode_field_name(name)
    }
}

impl<W: Write> ContextWriter for TextStructWriter_1_1<'_, W> {
    type NestedValueWriter<'a>
        = TextValueWriter_1_1<'a, W>
    where
        Self: 'a;
}

impl<W: Write> MakeValueWriter for TextStructWriter_1_1<'_, W> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        TextValueWriter_1_1 {
            value_writer_1_0: self.writer_1_0.make_value_writer(),
            macros: self.macros,
        }
    }
}

impl<W: Write> StructWriter for TextStructWriter_1_1<'_, W> {
    fn close(self) -> IonResult<()> {
        self.writer_1_0.close()
    }

    fn config(&self) -> ValueWriterConfig {
        v1_1::Text::default_value_writer_config()
    }
}

pub struct TextEExpWriter_1_1<'value, W: Write> {
    // There is no e-exp writer in 1.0 to which we can delegate,
    // but we can re-use the TextContainerWriter_1_0 for a lot of the formatting.
    container_writer: TextContainerWriter_1_0<'value, W>,
    macros: &'value MacroTable,
    signature_iter: SignatureIterator<'value>,
}

impl<'value, W: Write> TextEExpWriter_1_1<'value, W> {
    pub(crate) fn new(
        writer: &'value mut LazyRawTextWriter_1_0<W>,
        depth: usize,
        parent_type: ParentType,
        opening_text: &str,
        macros: &'value MacroTable,
        invoked_macro: MacroRef<'value>,
    ) -> IonResult<Self> {
        let trailing_delimiter = match parent_type {
            ParentType::Struct | ParentType::List => ",",
            _ => "",
        };
        let value_delimiter = " ";

        let container_writer = TextContainerWriter_1_0::new(
            writer,
            depth,
            parent_type,
            // Pretend we're in a SExp for syntax purposes
            ContainerType::SExp,
            opening_text,
            value_delimiter,
            trailing_delimiter,
        )?;
        let signature_iter = invoked_macro.iter_signature();
        Ok(Self {
            container_writer,
            macros,
            signature_iter,
        })
    }
}

impl<'value, W: Write + 'value> SequenceWriter for TextEExpWriter_1_1<'value, W> {
    type Resources = ();

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

impl<'value, W: Write + 'value> ContextWriter for TextEExpWriter_1_1<'value, W> {
    type NestedValueWriter<'a>
        = TextValueWriter_1_1<'a, W>
    where
        Self: 'a;
}

impl<'value, W: Write + 'value> MakeValueWriter for TextEExpWriter_1_1<'value, W> {
    fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> {
        TextValueWriter_1_1 {
            value_writer_1_0: self.container_writer.value_writer(),
            macros: self.macros,
        }
    }
}

impl<'eexp, W: Write + 'eexp> EExpWriterInternal for TextEExpWriter_1_1<'eexp, W> {
    fn expect_next_parameter(&mut self) -> IonResult<&Parameter> {
        self.signature_iter.expect_next_parameter()
    }
}

impl<'eexp, W: Write + 'eexp> EExpWriter for TextEExpWriter_1_1<'eexp, W> {
    type ExprGroupWriter<'group>
        = TextExprGroupWriter<'group, W>
    where
        Self: 'group;

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

    fn current_parameter(&self) -> Option<&Parameter> {
        self.signature_iter.current_parameter()
    }

    fn expr_group_writer(&mut self) -> IonResult<Self::ExprGroupWriter<'_>> {
        TextExprGroupWriter::new(
            self.container_writer.writer,
            self.container_writer.depth,
            self.container_writer.container_type.into(),
            " ",
            self.macros,
        )
    }
    // Default SequenceWriter methods
}

pub struct TextExprGroupWriter<'group, W: Write> {
    // There is no expr group writer in 1.0 to which we can delegate,
    // but we can re-use the TextContainerWriter_1_0 for a lot of the formatting.
    container_writer: TextContainerWriter_1_0<'group, W>,
    macros: &'group MacroTable,
}

impl<'group, W: Write> TextExprGroupWriter<'group, W> {
    pub(crate) fn new(
        writer: &'group mut LazyRawTextWriter_1_0<W>,
        depth: usize,
        parent_type: ParentType,
        trailing_delimiter: &'static str,
        macros: &'group MacroTable,
    ) -> IonResult<Self> {
        let container_writer = TextContainerWriter_1_0::new(
            writer,
            depth + 1,
            parent_type,
            ContainerType::SExp,
            "(::",
            " ",
            trailing_delimiter,
        )?;
        Ok(Self {
            container_writer,
            macros,
        })
    }
}

impl<W: Write> MakeValueWriter for TextExprGroupWriter<'_, W> {
    fn make_value_writer(&mut self) -> <Self as ContextWriter>::NestedValueWriter<'_> {
        TextValueWriter_1_1 {
            value_writer_1_0: self.container_writer.value_writer(),
            macros: self.macros,
        }
    }
}

impl<W: Write> SequenceWriter for TextExprGroupWriter<'_, W> {
    type Resources = ();

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

impl<W: Write> ContextWriter for TextExprGroupWriter<'_, W> {
    type NestedValueWriter<'a>
        = TextValueWriter_1_1<'a, W>
    where
        Self: 'a;
}