Skip to main content

deser_value/
ser.rs

1use deser_core::Text;
2use deser_core::ser::{Emit, MapEmitter, SeqEmitter, Serialize, SerializeHandle};
3use deser_core::{Atom, ContainerShape, Error, ErrorKind, State};
4
5use crate::map::Map;
6use crate::seq::Seq;
7use crate::value::{Kind, Value};
8
9impl Serialize for Value {
10    fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
11        if let Some(ref meta) = value.meta
12            && !meta.event_data().is_empty()
13        {
14            state.attach_event_data(meta.event_data());
15        }
16        Kind::serialize(&value.kind, state)
17    }
18
19    fn container_shape(value: &Self) -> ContainerShape {
20        Kind::container_shape(&value.kind)
21    }
22
23    fn is_optional(value: &Self) -> bool {
24        matches!(value.kind, Kind::Null)
25    }
26}
27
28impl Serialize for Kind {
29    fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
30        Ok(match this {
31            Kind::Null => Emit::Atom(Atom::Null),
32            Kind::Bool(value) => Emit::Atom(Atom::Bool(*value)),
33            Kind::U64(value) => Emit::Atom(Atom::U64(*value)),
34            Kind::I64(value) => Emit::Atom(Atom::I64(*value)),
35            Kind::F32(value) => Emit::Atom(Atom::F32(*value)),
36            Kind::F64(value) => Emit::Atom(Atom::F64(*value)),
37            Kind::Char(value) => Emit::Atom(Atom::Char(*value)),
38            Kind::Str(value) => Emit::Atom(Atom::Str(Text::borrowed(value))),
39            Kind::Lexical(value) => Emit::Atom(Atom::Lexical(Text::borrowed(value))),
40            Kind::Bytes(value) => Emit::Atom(Atom::Bytes(value.as_borrowed())),
41            Kind::Ext(value) => Emit::Atom(Atom::Ext(value.as_borrowed())),
42            Kind::Implicit(value) => Emit::Atom(Atom::Implicit(value.as_borrowed())),
43            Kind::Seq(seq) => return Seq::serialize(seq, state),
44            Kind::Map(map) => return Map::serialize(map, state),
45        })
46    }
47
48    fn container_shape(value: &Self) -> ContainerShape {
49        match value {
50            Kind::Seq(seq) => Seq::container_shape(seq),
51            Kind::Map(map) => Map::container_shape(map),
52            _ => ContainerShape::new(),
53        }
54    }
55
56    fn is_optional(value: &Self) -> bool {
57        matches!(value, Kind::Null)
58    }
59}
60
61impl Serialize for Seq {
62    fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
63        Ok(Emit::seq(SeqIter(value.items.iter()), state))
64    }
65
66    fn container_shape(value: &Self) -> ContainerShape {
67        {
68            let mut shape = ContainerShape::with_len(value.len());
69            shape.set_order(value.order());
70            shape.set_ambiguous_empty(value.is_ambiguous_empty());
71            shape
72        }
73    }
74}
75
76struct SeqIter<'a>(std::slice::Iter<'a, Value>);
77
78impl SeqEmitter for SeqIter<'_> {
79    fn next(&mut self, _state: &mut State) -> Result<Option<SerializeHandle<'_>>, Error> {
80        Ok(self.0.next().map(SerializeHandle::to))
81    }
82}
83
84impl Serialize for Map {
85    fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
86        Ok(Emit::map(
87            MapIter {
88                iter: this.inner.entries.iter(),
89                value: None,
90            },
91            state,
92        ))
93    }
94
95    fn container_shape(value: &Self) -> ContainerShape {
96        {
97            let mut shape = ContainerShape::with_len(value.len());
98            shape.set_order(value.order());
99            shape.set_multimap(value.is_multimap());
100            shape.set_ambiguous_empty(value.is_ambiguous_empty());
101            shape
102        }
103    }
104}
105
106struct MapIter<'a> {
107    iter: indexmap::map::Iter<'a, Value, Value>,
108    value: Option<&'a Value>,
109}
110
111impl MapEmitter for MapIter<'_> {
112    fn next_key(&mut self, _state: &mut State) -> Result<Option<SerializeHandle<'_>>, Error> {
113        Ok(self.iter.next().map(|(key, value)| {
114            self.value = Some(value);
115            SerializeHandle::to(key)
116        }))
117    }
118
119    fn next_value(&mut self, _state: &mut State) -> Result<SerializeHandle<'_>, Error> {
120        match self.value.take() {
121            Some(value) => Ok(SerializeHandle::to(value)),
122            None => Err(Error::new(
123                ErrorKind::InvalidState,
124                "next_value called before next_key",
125            )),
126        }
127    }
128}