deser-core 0.10.1

Core traits and types of deser, use the deser crate instead
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
//! Serialization support for enums with data.
//!
//! These are used by the derive.
use alloc::borrow::Cow;
use alloc::format;
use alloc::vec::Vec;

use crate::error::{Error, ErrorKind};
use crate::event::{Atom, ContainerShape};
use crate::ser::begin::Begin;
use crate::ser::flatten::FlattenedStruct;
use crate::ser::{
    Describe, Emit, MapEmitter, SeqEmitter, Serialize, SerializeHandle, SerializeRef,
    StructEmitter, Variant, VariantKind, VariantRepr, is_unit_struct,
};
use crate::{State, Text};

/// Serializes a map with a single entry.
///
/// This is used for externally tagged variants with a tag that is not a
/// static string.
pub struct EntrySer<'a> {
    key: SerializeHandle<'a>,
    value: SerializeHandle<'a>,
}

impl<'a> EntrySer<'a> {
    /// Creates a new entry.
    pub fn new(key: SerializeHandle<'a>, value: SerializeHandle<'a>) -> EntrySer<'a> {
        EntrySer { key, value }
    }

    /// Converts the entry into an [`Emit`].
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::map(
            EntryEmitter {
                entry: self,
                index: 0,
            },
            state,
        )
    }
}

struct EntryEmitter<'a> {
    entry: EntrySer<'a>,
    index: usize,
}

impl<'a> MapEmitter for EntryEmitter<'a> {
    fn next_key(&mut self, _state: &mut State) -> Result<Option<SerializeHandle<'_>>, Error> {
        let index = self.index;
        self.index += 1;
        Ok(if index == 0 {
            Some(SerializeHandle::from(self.entry.key.get()))
        } else {
            None
        })
    }

    fn next_value(&mut self, _state: &mut State) -> Result<SerializeHandle<'_>, Error> {
        Ok(SerializeHandle::from(self.entry.value.get()))
    }
}

/// Serializes a list of named fields as a struct.
pub struct FieldsSer<'a>(pub Vec<(&'static str, SerializeHandle<'a>)>);

impl<'a> FieldsSer<'a> {
    /// Converts the fields into an [`Emit`].
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::structure(
            FieldsEmitter {
                fields: self.0,
                index: 0,
            },
            state,
        )
    }
}

impl<'a> Serialize for FieldsSer<'a> {
    fn serialize<'b>(this: &'b Self, state: &mut State) -> Result<Emit<'b>, Error> {
        Ok(Emit::structure(
            FieldsEmitter {
                fields: this
                    .0
                    .iter()
                    .map(|(name, value)| (*name, SerializeHandle::from(value.get())))
                    .collect(),
                index: 0,
            },
            state,
        ))
    }
}

struct FieldsEmitter<'a> {
    fields: Vec<(&'static str, SerializeHandle<'a>)>,
    index: usize,
}

impl<'a> StructEmitter for FieldsEmitter<'a> {
    fn next(
        &mut self,
        _state: &mut State,
    ) -> Result<Option<(Cow<'_, str>, SerializeHandle<'_>)>, Error> {
        let index = self.index;
        self.index += 1;
        Ok(self
            .fields
            .get(index)
            .map(|(name, value)| (Cow::Borrowed(*name), SerializeHandle::from(value.get()))))
    }
}

/// A field of a [`FlatFieldsSer`].
pub enum FieldSer<'a> {
    /// A field with its name.
    Field(&'static str, SerializeHandle<'a>),
    /// A value whose fields are merged into the struct.
    Flatten(SerializeRef<'a>),
}

/// Serializes a list of fields as a struct, some of which are flattened.
///
/// This is used for struct variants with flattened fields.  The fields of
/// flattened values that are optional are skipped if `skip_optionals` is
/// set (the other fields are skipped by the derive).
pub struct FlatFieldsSer<'a> {
    pub fields: Vec<FieldSer<'a>>,
    pub skip_optionals: bool,
}

impl<'a> FlatFieldsSer<'a> {
    /// Converts the fields into an [`Emit`].
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::structure(
            FlatFieldsEmitter {
                fields: self.fields,
                skip_optionals: self.skip_optionals,
                index: 0,
                nested: None,
            },
            state,
        )
    }
}

impl<'a> Serialize for FlatFieldsSer<'a> {
    fn serialize<'b>(this: &'b Self, state: &mut State) -> Result<Emit<'b>, Error> {
        Ok(Emit::structure(
            FlatFieldsEmitter {
                fields: this
                    .fields
                    .iter()
                    .map(|field| match *field {
                        FieldSer::Field(name, ref value) => {
                            FieldSer::Field(name, SerializeHandle::from(value.get()))
                        }
                        FieldSer::Flatten(value) => FieldSer::Flatten(value),
                    })
                    .collect(),
                skip_optionals: this.skip_optionals,
                index: 0,
                nested: None,
            },
            state,
        ))
    }
}

struct FlatFieldsEmitter<'a> {
    fields: Vec<FieldSer<'a>>,
    skip_optionals: bool,
    index: usize,
    // the fields of the flattened value at `index`
    nested: Option<FlattenedStruct<'a>>,
}

impl<'a> StructEmitter for FlatFieldsEmitter<'a> {
    fn next(
        &mut self,
        state: &mut State,
    ) -> Result<Option<(Cow<'_, str>, SerializeHandle<'_>)>, Error> {
        loop {
            if let Some(ref mut nested) = self.nested {
                let item = nested.next(state)?;
                // SAFETY: the item borrows from `self.nested`.  If it's
                // returned, `self.nested` is not touched again in this call,
                // otherwise it's dropped before `self.nested` is replaced.
                // The borrow checker does not understand that the borrow
                // does not continue into the next loop iteration (this can
                // be validated with `-Zpolonius`).
                let item = unsafe {
                    core::mem::transmute::<
                        Option<(Cow<'_, str>, SerializeHandle<'_>)>,
                        Option<(Cow<'a, str>, SerializeHandle<'a>)>,
                    >(item)
                };
                match item {
                    Some((_, ref handle)) if self.skip_optionals && handle.get().is_optional() => {
                        continue;
                    }
                    Some(item) => return Ok(Some(item)),
                    None => {
                        self.nested = None;
                        if let Some(FieldSer::Flatten(value)) = self.fields.get(self.index) {
                            // the values it forwarded to were finished with
                            // the last field, now the value itself
                            value.finish(state)?;
                        }
                        self.index += 1;
                        continue;
                    }
                }
            }
            match self.fields.get(self.index) {
                None => return Ok(None),
                Some(FieldSer::Field(name, value)) => {
                    self.index += 1;
                    return Ok(Some((
                        Cow::Borrowed(*name),
                        SerializeHandle::from(value.get()),
                    )));
                }
                Some(FieldSer::Flatten(value)) => {
                    self.nested = Some(FlattenedStruct::new(*value, state)?);
                }
            }
        }
    }
}

/// Serializes a list of values as a sequence.
pub struct SeqSer<'a>(pub Vec<SerializeHandle<'a>>);

impl<'a> SeqSer<'a> {
    /// Converts the values into an [`Emit`].
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::seq(
            SeqValuesEmitter {
                values: self.0,
                index: 0,
            },
            state,
        )
    }
}

impl<'a> Serialize for SeqSer<'a> {
    fn serialize<'b>(this: &'b Self, state: &mut State) -> Result<Emit<'b>, Error> {
        Ok(Emit::seq(
            SeqValuesEmitter {
                values: this
                    .0
                    .iter()
                    .map(|value| SerializeHandle::from(value.get()))
                    .collect(),
                index: 0,
            },
            state,
        ))
    }
}

struct SeqValuesEmitter<'a> {
    values: Vec<SerializeHandle<'a>>,
    index: usize,
}

impl<'a> SeqEmitter for SeqValuesEmitter<'a> {
    fn next(&mut self, _state: &mut State) -> Result<Option<SerializeHandle<'_>>, Error> {
        let index = self.index;
        self.index += 1;
        Ok(self
            .values
            .get(index)
            .map(|value| SerializeHandle::from(value.get())))
    }
}

/// Serializes a newtype variant of an internally tagged enum.
///
/// The tag is emitted first, followed by the fields of the inner value which
/// has to serialize as a struct or map.  Unit structs have no fields, the
/// variant is the tag alone (which is deserialized as null, see
/// `InternallyTaggedSink`).
pub struct TaggedNewtype<'a> {
    tag: &'static str,
    name: SerializeHandle<'a>,
    inner: SerializeRef<'a>,
}

impl<'a> TaggedNewtype<'a> {
    /// Creates a new tagged newtype.
    ///
    /// `name` is the value of the tag.
    pub fn new(tag: &'static str, name: SerializeHandle<'a>, inner: SerializeRef<'a>) -> Self {
        TaggedNewtype { tag, name, inner }
    }

    /// Converts the value into an [`Emit`].
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::structure(
            TaggedNewtypeEmitter {
                value: self,
                content: None,
                started: false,
                done: false,
            },
            state,
        )
    }
}

/// Serializes a variant of an internally tagged enum whose content is
/// owned (like [`TaggedNewtype`] which borrows it).
///
/// This is used for the content of variants with adapters, which is a
/// tuple of references to the fields.  It's serialized by forwarding to it
/// (see [`Emit::Forward`]) as the content cannot be borrowed otherwise.
pub struct TaggedContent<'a, S> {
    tag: &'static str,
    name: SerializeHandle<'a>,
    inner: S,
}

impl<'a, S: Serialize + Send + 'a> TaggedContent<'a, S> {
    /// Creates the tagged content.
    ///
    /// `name` is the value of the tag.
    pub fn new(tag: &'static str, name: SerializeHandle<'a>, inner: S) -> Self {
        TaggedContent { tag, name, inner }
    }

    /// Converts the value into an [`Emit`] that forwards to it.
    pub fn into_emit(self, state: &mut State) -> Emit<'a> {
        Emit::Forward(SerializeHandle::arena(self, state))
    }
}

impl<S: Serialize> Serialize for TaggedContent<'_, S> {
    fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
        Ok(TaggedNewtype::new(
            value.tag,
            SerializeHandle::from(value.name.get()),
            SerializeRef::new(&value.inner),
        )
        .into_emit(state))
    }
}

struct TaggedNewtypeEmitter<'a> {
    value: TaggedNewtype<'a>,
    // the fields of the inner value, once the tag was emitted
    content: Option<FlattenedStruct<'a>>,
    started: bool,
    done: bool,
}

impl<'a> StructEmitter for TaggedNewtypeEmitter<'a> {
    fn next(
        &mut self,
        state: &mut State,
    ) -> Result<Option<(Cow<'_, str>, SerializeHandle<'_>)>, Error> {
        if !self.started {
            self.started = true;
            return Ok(Some((
                Cow::Borrowed(self.value.tag),
                SerializeHandle::from(self.value.name.get()),
            )));
        }
        if self.done {
            return Ok(None);
        }
        if self.content.is_none() {
            let content = FlattenedStruct::new(self.value.inner, state)?;
            // unit structs are the tag alone, which is deserialized as null.
            // Other null content (like `None`) is not: it's not deserialized
            // from the tag alone (`()` is handled by the derive).
            if content.is_null() {
                let inner = self.value.inner;
                if !is_unit_struct(|d| inner.describe(d)) {
                    return Err(Error::new(
                        ErrorKind::UnsupportedType,
                        "newtype variants of internally tagged enums must contain structs, \
                         maps or unit structs",
                    ));
                }
                self.done = true;
                self.value.inner.finish(state)?;
                return Ok(None);
            }
            self.content = Some(content);
        }
        let tag = self.value.tag;
        match self.content.as_mut().unwrap().next(state)? {
            // the tag would be given twice (for instance by an internally
            // tagged enum with the same tag) and most parsers use the last.
            Some((name, _)) if name == tag => Err(Error::new(
                ErrorKind::InvalidValue,
                format!(
                    "the content of the variant has a field `{}` like the tag",
                    tag
                ),
            )),
            Some(item) => Ok(Some(item)),
            None => {
                self.done = true;
                self.value.inner.finish(state)?;
                Ok(None)
            }
        }
    }
}

/// Creates the error for a variant that is skipped when serializing.
#[cold]
pub fn skipped_variant(type_name: &str, variant: &str) -> Error {
    Error::new(
        ErrorKind::UnsupportedType,
        format!(
            "the variant `{}` of {} cannot be serialized",
            variant, type_name
        ),
    )
}

/// The name of a variant of a unit enum (see [`UnitVariants`]).
pub enum UnitName {
    Str(&'static str),
    U64(u64),
    I64(i64),
    Bool(bool),
    /// The variant cannot be serialized, this is its name in Rust.
    Skipped(&'static str),
}

/// The variants of a unit enum of the derive (an enum with only unit
/// variants).
///
/// The derive generates this as a constant together with a function that
/// returns the index of a variant, everything else exists once for all
/// unit enums.
pub struct UnitVariants {
    /// The name of the enum.
    pub type_name: &'static str,
    /// The names of the variants as they are described.
    pub names: &'static [&'static str],
    /// The names of the variants as they are serialized.
    pub atoms: &'static [UnitName],
}

/// Serializes the variant of a unit enum by index.
#[inline]
pub fn serialize_unit(variants: &UnitVariants, index: usize) -> Result<Emit<'static>, Error> {
    Ok(Emit::Atom(match variants.atoms[index] {
        UnitName::Str(name) => Atom::Str(Text::borrowed(name)),
        UnitName::U64(value) => Atom::U64(value),
        UnitName::I64(value) => Atom::I64(value),
        UnitName::Bool(value) => Atom::Bool(value),
        UnitName::Skipped(variant) => return Err(skipped_variant(variants.type_name, variant)),
    }))
}

/// Begins the serialization of the variant of a unit enum by index.
pub fn begin_unit(variants: &UnitVariants, index: usize) -> Result<Begin<'static>, Error> {
    Ok(Begin::emit(
        serialize_unit(variants, index)?,
        ContainerShape::new(),
        false,
    ))
}

/// Describes the variant of a unit enum by index.
pub fn describe_unit(d: &mut dyn Describe, variants: &UnitVariants, index: usize) {
    d.variant(&Variant::new(
        variants.type_name,
        variants.names[index],
        VariantKind::Unit,
        VariantRepr::External,
    ));
}