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
//! Serialization

use std::io::Write;

use serde::ser::{self, Impossible};
use serde::Serialize;
use serde_schema::SchemaSerialize;

use internal::ser::{FieldValueSerializer, SerializationCtx, SerializeVariantValue};
use internal::utils::Bow;

use error::Error;
pub use schema::{Schema, TypeId};

mod output;
pub use self::output::{Output, OutputBuffer, OutputPart, OutputWrite};

mod serialize_struct;
pub use self::serialize_struct::SerializeStruct;
mod serialize_seq;
pub use self::serialize_seq::SerializeSeq;
mod serialize_tuple;
pub use self::serialize_tuple::SerializeTuple;
mod serialize_map;
pub use self::serialize_map::SerializeMap;
mod serialize_struct_variant;
pub use self::serialize_struct_variant::SerializeStructVariant;

/// Serializes a single value.
pub struct Serializer<'t, O> {
    ctx: SerializationCtx<Bow<'t, Schema>>,
    type_id: TypeId,
    out: O,
}

/// Serializes a stream of values.
pub struct StreamSerializer<O> {
    schema: Schema,
    out: O,
}

impl StreamSerializer<OutputBuffer> {
    /// Create a new stream serializer that writes into a buffer.
    pub fn new_with_buffer() -> Self {
        let buffer = OutputBuffer::new();
        StreamSerializer::new(buffer)
    }
}

impl<W: Write> StreamSerializer<OutputWrite<W>> {
    /// Create a new stream serializer with the provided `Write` output.
    pub fn new_with_write(w: W) -> Self {
        StreamSerializer::new(OutputWrite::new(w))
    }
}

impl<O> StreamSerializer<O> {
    fn new(out: O) -> StreamSerializer<O> {
        let schema = Schema::new();
        StreamSerializer { schema, out }
    }

    pub fn schema_mut(&mut self) -> &mut Schema {
        &mut self.schema
    }

    pub fn serializer<'a>(&'a mut self, id: TypeId) -> Result<Serializer<'a, &'a mut O>, Error> {
        let ctx = SerializationCtx::with_schema(Bow::Borrowed(&mut self.schema));
        Ok(Serializer {
            type_id: id,
            ctx,
            out: &mut self.out,
        })
    }

    /// Serialize a value onto the stream.
    pub fn serialize<T>(&mut self, value: &T) -> Result<(), Error>
    where
        T: SchemaSerialize,
        O: Output,
    {
        let type_id = T::schema_register(&mut self.schema)?;
        self.serialize_with_type_id(type_id, value)
    }

    pub fn serialize_with_type_id<T>(&mut self, type_id: TypeId, value: &T) -> Result<(), Error>
    where
        T: Serialize,
        O: Output,
    {
        value.serialize(self.serializer(type_id)?)
    }

    pub fn get_ref(&self) -> &O {
        &self.out
    }

    pub fn get_mut(&mut self) -> &mut O {
        &mut self.out
    }

    pub fn into_inner(self) -> O {
        self.out
    }
}

impl<'t, O: Output> ser::Serializer for Serializer<'t, O> {
    type Ok = ();
    type Error = Error;

    type SerializeSeq = SerializeSeq<'t, O>;
    type SerializeTuple = SerializeTuple<'t, O>;
    type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>;
    type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>;
    type SerializeMap = SerializeMap<'t, O>;
    type SerializeStruct = SerializeStruct<'t, O>;
    type SerializeStructVariant = SerializeStructVariant<'t, O>;

    fn serialize_bool(mut self, v: bool) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_bool(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(v as i64)
    }

    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(v as i64)
    }

    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(v as i64)
    }

    fn serialize_i64(mut self, v: i64) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_i64(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(v as u64)
    }

    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(v as u64)
    }

    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(v as u64)
    }

    fn serialize_u64(mut self, v: u64) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_u64(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
        self.serialize_f64(v as f64)
    }

    fn serialize_f64(mut self, v: f64) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_f64(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
        self.serialize_i64(v as i64)
    }

    fn serialize_str(mut self, v: &str) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_str(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_bytes(mut self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_bytes(v)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_none(mut self) -> Result<Self::Ok, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_none()?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _name: &'static str,
        _value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_newtype_variant<T: ?Sized>(
        mut self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize,
    {
        self.ctx.value.write_int(self.type_id.0);
        let mut ok = {
            let ser = FieldValueSerializer {
                ctx: self.ctx,
                type_id: self.type_id,
            };
            ser.serialize_newtype_variant(name, variant_index, variant, value)?
        };
        ok.ctx.flush(self.out)
    }

    fn serialize_seq(mut self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        SerializeSeq::new(len, self.type_id, self.ctx, self.out)
    }

    fn serialize_tuple(mut self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        SerializeTuple::homogeneous(self.type_id, self.ctx, self.out)
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        Err(ser::Error::custom("not implemented yet"))
    }

    fn serialize_map(mut self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        self.ctx.value.write_uint(0);
        SerializeMap::new(len, self.type_id, self.ctx, self.out)
    }

    fn serialize_struct(
        mut self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        Ok(SerializeStruct::new(self.type_id, self.ctx, self.out)?)
    }

    fn serialize_struct_variant(
        mut self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        self.ctx.value.write_int(self.type_id.0);
        let inner =
            SerializeVariantValue::new(self.ctx, self.type_id, variant_index)?.serialize_struct()?;
        SerializeStructVariant::new(inner, self.out)
    }
}