ptah/ser/
mod.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
mod impls;

#[cfg(feature = "heapless")]
mod heapless;

use crate::Writer;

/// Errors that can occur during serialization.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Error {
    WriterFullOfBytes,
    WriterFullOfHandles,
}

pub type Result<T> = core::result::Result<T, Error>;

pub trait Serialize {
    fn serialize<W>(&self, serializer: &mut Serializer<W>) -> Result<()>
    where
        W: Writer;
}

pub struct Serializer<W>
where
    W: Writer,
{
    pub(crate) writer: W,
}

impl<W> Serializer<W>
where
    W: Writer,
{
    pub fn new(writer: W) -> Serializer<W> {
        Serializer { writer }
    }

    pub fn serialize_bool(&mut self, value: bool) -> Result<()> {
        match value {
            false => self.writer.write(&[0x00]),
            true => self.writer.write(&[0x01]),
        }
    }

    pub fn serialize_u8(&mut self, value: u8) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_u16(&mut self, value: u16) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_u32(&mut self, value: u32) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_u64(&mut self, value: u64) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_u128(&mut self, value: u128) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_i8(&mut self, value: i8) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_i16(&mut self, value: i16) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_i32(&mut self, value: i32) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_i64(&mut self, value: i64) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_i128(&mut self, value: i128) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_f32(&mut self, value: f32) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_f64(&mut self, value: f64) -> Result<()> {
        self.writer.write(&value.to_le_bytes())
    }

    pub fn serialize_char(&mut self, value: char) -> Result<()> {
        self.writer.write(&(value as u32).to_le_bytes())
    }

    pub fn serialize_str(&mut self, value: &str) -> Result<()> {
        let bytes = value.as_bytes();
        let mut seq = self.serialize_seq(bytes.len() as u32)?;

        for byte in bytes {
            seq.serialize_element(byte)?;
        }
        Ok(())
    }

    pub fn serialize_none(&mut self) -> Result<()> {
        self.writer.write(&[crate::MARKER_NONE])
    }

    pub fn serialize_some<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.writer.write(&[crate::MARKER_SOME])?;
        value.serialize(self)
    }

    pub fn serialize_seq<'a>(&'a mut self, length: u32) -> Result<SeqSerializer<'a, W>> {
        self.serialize_u32(length)?;
        Ok(SeqSerializer(self))
    }

    pub fn serialize_map<'a>(&'a mut self, length: u32) -> Result<MapSerializer<'a, W>> {
        self.serialize_u32(length)?;
        Ok(MapSerializer(self))
    }

    /// Start serializing an enum - this encodes the tag that specifies which variant is being encoded. Data
    /// contained in the variant should be serialized following this.
    pub fn serialize_enum_variant(&mut self, variant_index: u32) -> Result<()> {
        self.serialize_u32(variant_index)
    }

    pub fn serialize_handle(&mut self, handle: crate::Handle) -> Result<()> {
        let slot = self.writer.push_handle(handle)?;
        self.serialize_u8(slot)
    }
}

pub struct SeqSerializer<'a, W>(&'a mut Serializer<W>)
where
    W: Writer;

impl<'a, W> SeqSerializer<'a, W>
where
    W: Writer,
{
    pub fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(self.0)
    }
}

pub struct MapSerializer<'a, W>(&'a mut Serializer<W>)
where
    W: Writer;

impl<'a, W> MapSerializer<'a, W>
where
    W: Writer,
{
    pub fn serialize_key<K>(&mut self, key: &K) -> Result<()>
    where
        K: ?Sized + Serialize,
    {
        key.serialize(self.0)
    }

    pub fn serialize_value<V>(&mut self, value: &V) -> Result<()>
    where
        V: ?Sized + Serialize,
    {
        value.serialize(self.0)
    }
}