ion_rs/lazy/encoder/
write_as_ion.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
//! Defines traits that allow Rust values to be serialized as Ion.
//!
//! In order for a [`LazyRawWriter`](crate::lazy::encoder::LazyRawWriter) to serialize a Rust value
//! as Ion, that Rust type must implement [`WriteAsIon`] and may optionally also
//! implement [`WriteAsIon`].
//!
//! [`WriteAsIon`] allows the implementor to map the Rust value to an unannotated Ion value.
//!
//! [`WriteAsIon`] builds on [`WriteAsIon`], offering a staged writer that requires the
//! implementor to specify what annotations to write before delegating to [`WriteAsIon`]
//! to serialize the value itself.
//!
//! Types that do not explicitly implement [`WriteAsIon`] will fall back to a blanket implementation
//! that uses an empty annotations sequence. A custom annotations sequence can be set on a per-value
//! basis by using the [`annotate`](crate::lazy::encoder::annotate::Annotatable::annotated_with) method
//! provided by the [`Annotate`](crate::lazy::encoder::annotate::Annotatable) trait.
use std::io;
use std::marker::PhantomData;

use crate::lazy::decoder::{Decoder, LazyRawValueExpr, RawValueExpr};
use crate::lazy::encoder::annotation_seq::AnnotationsVec;
use crate::lazy::encoder::value_writer::{SequenceWriter, StructWriter, ValueWriter};
use crate::lazy::encoding::Encoding;
use crate::lazy::expanded::macro_evaluator::RawEExpression;
use crate::lazy::text::raw::v1_1::arg_group::{EExpArg, EExpArgExpr};
use crate::lazy::value::LazyValue;
use crate::lazy::value_ref::ValueRef;
use crate::v1_0::RawValueRef;
use crate::{
    Blob, Clob, Decimal, Element, Int, IonResult, IonType, LazyList, LazyRawFieldExpr,
    LazyRawFieldName, LazyRawSequence, LazyRawStruct, LazyRawValue, LazySExp, LazyStruct, Null,
    RawSymbolRef, Symbol, SymbolRef, Timestamp, Value, WriteConfig,
};

/// Defines how a Rust type should be serialized as Ion in terms of the methods available
/// on [`ValueWriter`].
pub trait WriteAsIon {
    /// Maps this value to the Ion data model using the provided [`ValueWriter`] implementation.
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()>;

    /// Encodes this value as an Ion stream with `self` as the single top-level value.
    /// If the requested encoding is binary of any version, returns a `Vec<u8>` containing the
    /// encoded bytes. If the requested encoding is text of any version, returns a `String` instead.
    /// ```
    ///# use ion_rs::IonResult;
    ///# #[cfg(feature = "experimental-reader-writer")]
    ///# fn main() -> IonResult<()> {
    ///# use ion_rs::*;
    ///
    /// use ion_rs::WriteAsIon;
    /// let ion_text: String = "foo bar baz".encode_as(v1_0::Text)?;
    /// let element = Element::read_one(ion_text)?;
    /// assert_eq!(element.as_string().unwrap(), "foo bar baz");
    ///# Ok(())
    ///# }
    ///# #[cfg(not(feature = "experimental-reader-writer"))]
    ///# fn main() -> IonResult<()> { Ok(()) }
    /// ```
    fn encode_as<E: Encoding, C: Into<WriteConfig<E>>>(&self, config: C) -> IonResult<E::Output>
    where
        for<'a> &'a Self: WriteAsIon,
    {
        config.into().encode(self)
    }

    /// Encodes this value as an Ion stream with `self` as the single top-level value, writing
    /// the resulting stream to the specified `output`.
    fn encode_to<E: Encoding, C: Into<WriteConfig<E>>, W: io::Write>(
        &self,
        config: C,
        output: W,
    ) -> IonResult<W>
    where
        for<'a> &'a Self: WriteAsIon,
    {
        config.into().encode_to(self, output)
    }
}

impl WriteAsIon for &Element {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        if self.annotations().is_empty() {
            self.value().write_as_ion(writer)
        } else {
            self.value()
                .write_as_ion(writer.with_annotations(self.annotations().as_ref())?)
        }
    }
}

// ===== WriteAsIonValue implementations for common types =====

macro_rules! impl_write_as_ion_value {
    // End of iteration
    () => {};
    // The caller defined an expression to write other than `self` (e.g. `*self`, `*self.0`, etc)
    ($target_type:ty => $method:ident with $self:ident as $value:expr, $($rest:tt)*) => {
        impl WriteAsIon for $target_type {
            #[inline]
            fn write_as_ion<V: ValueWriter>(&$self, writer: V) -> IonResult<()> {
                writer.$method($value)
            }
        }
        impl_write_as_ion_value!($($rest)*);
    };
    // We're writing the expression `self`
    ($target_type:ty => $method:ident, $($rest:tt)*) => {
        impl WriteAsIon for $target_type {
            #[inline]
            fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
                writer.$method(self)
            }
        }
        impl_write_as_ion_value!($($rest)*);
    };
}

// TODO: For the moment, `i8` and `u8` do not directly implement `WriteAsIon` because this causes
//       the desired serialization for `&[u8]` and `&[i8]` to be ambiguous. They could be serialized
//       either as blobs or as lists of integers. We should use the same trick that `SExpTypeHint`
//       employs to make it possible for users to override the default blob serialization by writing:
//           writer.write(&[1u8, 2, 3].as_list())
impl_write_as_ion_value!(
    Null => write_null with self as self.0,
    bool => write_bool with self as *self,
    i16 => write_i64 with self as *self as i64,
    i32 => write_i64 with self as *self as i64,
    i64 => write_i64 with self as *self,
    isize => write_i64 with self as *self as i64,
    u16 => write_i64 with self as i64::from(*self),
    u32 => write_i64 with self as i64::from(*self),
    u64 => write_int with self as &Int::from(*self),
    usize => write_int with self as &Int::from(*self),
    f32 => write_f32 with self as *self,
    f64 => write_f64 with self as *self,
    Int => write_int,
    Decimal => write_decimal,
    Timestamp => write_timestamp,
    Symbol => write_symbol,
    &str => write_string,
    String => write_string,
    &[u8] => write_blob,
    Blob => write_blob,
    Clob => write_clob,
);

impl WriteAsIon for RawSymbolRef<'_> {
    #[inline]
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        writer.write_symbol(self)
    }
}

impl WriteAsIon for SymbolRef<'_> {
    #[inline]
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        writer.write_symbol(self)
    }
}

impl<const N: usize> WriteAsIon for [u8; N] {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        writer.write_blob(self)
    }
}

impl<T: WriteAsIon> WriteAsIon for &T {
    #[inline]
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        (*self).write_as_ion(writer)
    }
}

macro_rules! impl_write_as_ion_value_for_iterable {
    ($iterable:ty, $item:ident $(, const $n:ident: $n_type:ty)?) => {
        impl<'a, $item $(, const $n: $n_type)?> WriteAsIon for $iterable
        where
            $item: WriteAsIon + 'a,
        {
            fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
                writer.write_list(self.into_iter())
            }
        }
    };
}

impl_write_as_ion_value_for_iterable!(Vec<T>, T);
impl_write_as_ion_value_for_iterable!(&[T], T);
impl_write_as_ion_value_for_iterable!([T; N], T, const N: usize);

pub trait WriteAsSExp<T>: Sized
where
    T: WriteAsIon,
{
    // The name `as_sexp` makes common cases read as a short sentence:
    //     writer.write([1, 2, 3].as_sexp())?;
    // Clippy complains because in most contexts, `as_*` methods borrow by reference.
    #[allow(clippy::wrong_self_convention)]
    /// Wraps `self` (which may be a reference) in a [`SExpTypeHint`], causing the value to be
    /// serialized as an Ion S-expression instead of a list.
    fn as_sexp(self) -> SExpTypeHint<Self, T>;
}
macro_rules! impl_write_as_sexp_for_iterable {
    ($iterable:ty, $item:ident $(, const $n:ident: $n_type:ty)?) => {
        impl<$item $(, const $n: $n_type)?> WriteAsSExp<$item> for $iterable
        where
            $item: WriteAsIon,
        {
            fn as_sexp(self) -> SExpTypeHint<Self, T> {
                SExpTypeHint::new(self)
            }
        }
    };
}

impl_write_as_sexp_for_iterable!(Vec<T>, T);
impl_write_as_sexp_for_iterable!(&[T], T);
impl_write_as_sexp_for_iterable!([T; N], T, const N: usize);

/// A wrapper type that hints to the Ion writer that the sequence within should be serialized
/// as an s-expression, not a list.
///
/// Slices, `Vec`s, and arrays all implement the [`WriteAsSExp`] trait, which grants them the
/// [`as_sexp()`](WriteAsSExp::as_sexp) method.
pub struct SExpTypeHint<S, T> {
    values: S,
    spooky: PhantomData<T>,
}

impl<S, T> SExpTypeHint<S, T> {
    pub fn new(values: S) -> Self {
        Self {
            values,
            spooky: PhantomData,
        }
    }
}

macro_rules! impl_write_as_ion_value_for_sexp_type_hint {
    ($iterable:ty, $item:ident $(, const $n:ident: $n_type:ty)?) => {
        impl<$item $(, const $n: $n_type)?> WriteAsIon for SExpTypeHint<$iterable, $item>
        where
            $item: WriteAsIon,
            for<'a> &'a $item: WriteAsIon,
        {
            fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
                writer.write_sexp((&self.values).into_iter())
            }
        }
    };
}

impl_write_as_ion_value_for_sexp_type_hint!(Vec<T>, T);
impl_write_as_ion_value_for_sexp_type_hint!(&[T], T);
impl_write_as_ion_value_for_sexp_type_hint!([T; N], T, const N: usize);

impl WriteAsIon for Value {
    fn write_as_ion<V: ValueWriter>(&self, value_writer: V) -> IonResult<()> {
        use Value::*;
        match self {
            Null(i) => value_writer.write_null(*i),
            Bool(b) => value_writer.write_bool(*b),
            Int(i) => value_writer.write_int(i),
            Float(f) => value_writer.write_f64(*f),
            Decimal(d) => value_writer.write_decimal(d),
            Timestamp(t) => value_writer.write_timestamp(t),
            Symbol(s) => value_writer.write_symbol(s),
            String(s) => value_writer.write_string(s),
            Clob(c) => value_writer.write_clob(c),
            Blob(b) => value_writer.write_blob(b),
            List(l) => value_writer.write_list(l),
            SExp(s) => value_writer.write_sexp(s),
            Struct(s) => value_writer.write_struct(s.iter()),
        }
    }
}

impl<D: Decoder> WriteAsIon for LazyValue<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        if self.has_annotations() {
            let mut annotations = AnnotationsVec::new();
            for annotation in self.annotations() {
                annotations.push(annotation?.into());
            }
            self.read()?
                .write_as_ion(writer.with_annotations(annotations)?)
        } else {
            self.read()?.write_as_ion(writer)
        }
    }
}

impl<D: Decoder> WriteAsIon for RawValueRef<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, value_writer: V) -> IonResult<()> {
        use RawValueRef::*;
        match self {
            Null(i) => value_writer.write_null(*i),
            Bool(b) => value_writer.write_bool(*b),
            Int(i) => value_writer.write_int(i),
            Float(f) => value_writer.write_f64(*f),
            Decimal(d) => value_writer.write_decimal(d),
            Timestamp(t) => value_writer.write_timestamp(t),
            Symbol(s) => value_writer.write_symbol(s),
            String(s) => value_writer.write_string(s.text()),
            Clob(c) => value_writer.write_clob(c.data()),
            Blob(b) => value_writer.write_blob(b.data()),
            List(l) => {
                let mut list_writer = value_writer.list_writer()?;
                for value_result in l.iter() {
                    list_writer.write(WriteableRawValueExpr::<'_, D>::new(value_result?))?;
                }
                list_writer.close()
            }
            SExp(s) => {
                let mut sexp_writer = value_writer.sexp_writer()?;
                for value_result in s.iter() {
                    sexp_writer.write(WriteableRawValueExpr::<'_, D>::new(value_result?))?;
                }
                sexp_writer.close()
            }
            Struct(s) => {
                let mut struct_writer = value_writer.struct_writer()?;
                for field_result in s.iter() {
                    let field: LazyRawFieldExpr<'_, D> = field_result?;
                    match field {
                        LazyRawFieldExpr::NameValue(name, value) => {
                            struct_writer.write(name.read()?, WriteableRawValue::new(value))?;
                        }
                        LazyRawFieldExpr::NameEExp(name, eexp) => {
                            struct_writer.write(name.read()?, WriteableEExp::new(eexp))?;
                        }
                        LazyRawFieldExpr::EExp(_eexp) => {
                            todo!("Writing e-expressions in field name position during transcription.");
                        }
                    }
                }
                struct_writer.close()
            }
        }
    }
}

/// Wrapper type for `LazyRawValue`s that implements `WriteAsIon`.
pub struct WriteableRawValue<'a, D: Decoder, RawValue: LazyRawValue<'a, D>> {
    raw_value: RawValue,
    spooky: PhantomData<&'a D>,
}

impl<'a, D: Decoder, RawValue: LazyRawValue<'a, D>> WriteableRawValue<'a, D, RawValue> {
    pub fn new(raw_value: RawValue) -> Self {
        Self {
            raw_value,
            spooky: PhantomData,
        }
    }
}

impl<'a, D: Decoder, RawValue: LazyRawValue<'a, D>> WriteAsIon
    for WriteableRawValue<'a, D, RawValue>
{
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        if self.raw_value.has_annotations() {
            let mut annotations = AnnotationsVec::new();
            for annotation in self.raw_value.annotations() {
                annotations.push(annotation?);
            }
            self.raw_value
                .read()?
                .write_as_ion(writer.with_annotations(annotations)?)
        } else {
            self.raw_value.read()?.write_as_ion(writer)
        }
    }
}

/// Wrapper type for `RawEExpression`s that implements `WriteAsIon`.
pub struct WriteableEExp<'a, D: Decoder<EExp<'a> = RawEExp>, RawEExp: RawEExpression<'a, D> + 'a> {
    raw_eexp: RawEExp,
    spooky: PhantomData<&'a D>,
}

impl<'a, D: Decoder<EExp<'a> = RawEExp>, RawEExp: RawEExpression<'a, D> + 'a>
    WriteableEExp<'a, D, RawEExp>
{
    pub fn new(raw_eexp: RawEExp) -> Self {
        Self {
            raw_eexp,
            spooky: PhantomData,
        }
    }
}

impl<'a, D: Decoder<EExp<'a> = RawEExp>, RawEExp: RawEExpression<'a, D> + 'a> WriteAsIon
    for WriteableEExp<'a, D, RawEExp>
{
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        let id = self.raw_eexp.id();
        let mut eexp_writer = writer.eexp_writer(id)?;
        for arg_result in self.raw_eexp.raw_arguments() {
            let arg = arg_result?;
            eexp_writer.write(WriteableEExpArg::<'_, D>::new(arg))?;
        }
        eexp_writer.close()
    }
}

/// Wrapper type for `EExpArg`s that implements `WriteAsIon`.
pub struct WriteableEExpArg<'a, D: Decoder> {
    arg_expr: EExpArg<'a, D>,
    spooky: PhantomData<&'a D>,
}

impl<'a, D: Decoder> WriteableEExpArg<'a, D> {
    pub fn new(arg_expr: EExpArg<'a, D>) -> Self {
        Self {
            arg_expr,
            spooky: PhantomData,
        }
    }
}

impl<D: Decoder> WriteAsIon for WriteableEExpArg<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        use EExpArgExpr::*;
        match self.arg_expr.expr() {
            // TODO: Untagged encodings
            ValueLiteral(v) => WriteableRawValue::new(*v).write_as_ion(writer),
            EExp(e) => WriteableEExp::new(*e).write_as_ion(writer),
            ArgGroup(group) => WriteableEExpArgGroup::<'_, D>::new(*group).write_as_ion(writer),
        }
    }
}

/// Wrapper type for `WriteableEExpArgGroup`s that implements `WriteAsIon`.
pub struct WriteableEExpArgGroup<'a, D: Decoder> {
    arg_group: <<D as Decoder>::EExp<'a> as RawEExpression<'a, D>>::ArgGroup,
    spooky: PhantomData<&'a D>,
}

impl<'a, D: Decoder> WriteableEExpArgGroup<'a, D> {
    pub fn new(arg_group: <<D as Decoder>::EExp<'a> as RawEExpression<'a, D>>::ArgGroup) -> Self {
        Self {
            arg_group,
            spooky: PhantomData,
        }
    }
}

impl<D: Decoder> WriteAsIon for WriteableEExpArgGroup<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, _writer: V) -> IonResult<()> {
        todo!()
    }
}

/// Wrapper type for `LazyRawValueExpr`s that implements `WriteAsIon`.
pub struct WriteableRawValueExpr<'a, D: Decoder> {
    raw_value_expr: LazyRawValueExpr<'a, D>,
    spooky: PhantomData<&'a D>,
}

impl<'a, D: Decoder> WriteableRawValueExpr<'a, D> {
    pub fn new(raw_value_expr: LazyRawValueExpr<'a, D>) -> Self {
        Self {
            raw_value_expr,
            spooky: PhantomData,
        }
    }
}

impl<D: Decoder> WriteAsIon for WriteableRawValueExpr<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        use RawValueExpr::*;
        match self.raw_value_expr {
            ValueLiteral(v) => WriteableRawValue::new(v).write_as_ion(writer),
            EExp(e) => WriteableEExp::new(e).write_as_ion(writer),
        }
    }
}

impl<D: Decoder> WriteAsIon for ValueRef<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, value_writer: V) -> IonResult<()> {
        use ValueRef::*;
        match self {
            Null(i) => value_writer.write_null(*i),
            Bool(b) => value_writer.write_bool(*b),
            Int(i) => value_writer.write_int(i),
            Float(f) => value_writer.write_f64(*f),
            Decimal(d) => value_writer.write_decimal(d),
            Timestamp(t) => value_writer.write_timestamp(t),
            Symbol(s) => value_writer.write_symbol(s),
            String(s) => value_writer.write_string(s.text()),
            Clob(c) => value_writer.write_clob(c.data()),
            Blob(b) => value_writer.write_blob(b.data()),
            List(l) => value_writer.write(l),
            SExp(s) => value_writer.write(s),
            Struct(s) => value_writer.write(s),
        }
    }
}

impl<D: Decoder> WriteAsIon for LazyList<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        let mut list = writer.list_writer()?;
        for value in self {
            list.write(value?)?;
        }
        list.close()
    }
}

impl<D: Decoder> WriteAsIon for LazySExp<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        let mut sexp = writer.sexp_writer()?;
        for value in self {
            sexp.write(value?)?;
        }
        sexp.close()
    }
}

impl<D: Decoder> WriteAsIon for LazyStruct<'_, D> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        let mut struct_writer = writer.struct_writer()?;
        for field_result in self {
            let field = field_result?;
            struct_writer.write(field.name()?, field.value())?;
        }
        struct_writer.close()
    }
}

impl<T: WriteAsIon> WriteAsIon for Option<T> {
    fn write_as_ion<V: ValueWriter>(&self, writer: V) -> IonResult<()> {
        if let Some(value) = self {
            value.write_as_ion(writer)
        } else {
            writer.write_null(IonType::Null)
        }
    }
}