1use core::{fmt, marker};
2use serde::{
3 Serialize,
4 Serializer,
5 ser::{
6 SerializeSeq,
7 SerializeTuple,
8 SerializeTupleStruct,
9 SerializeTupleVariant,
10 SerializeMap,
11 SerializeStruct,
12 SerializeStructVariant
13 }
14};
15use byteorder::ByteOrder;
16use either::Either;
17use super::{
18 io::{
19 Write,
20 BinarySerializerDelegate
21 },
22 err::{
23 ErrorAdapter,
24 DisplayCollector
25 }
26};
27
28#[derive(Debug)]
29pub enum BinarySerializerError {
30 }
32
33impl fmt::Display for BinarySerializerError {
34 fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
35 unreachable!()
36 }
37}
38
39pub struct BinarySerializer<W, E, H, D>
40where
41 W: Write,
42 E: ByteOrder,
43 H: BinarySerializerDelegate,
44 D: DisplayCollector,
45{
46 write: W,
47 phantom_data: marker::PhantomData<(E, H, D)>,
48}
49
50impl<W, E, H, D> BinarySerializer<W, E, H, D>
51where
52 W: Write,
53 E: ByteOrder,
54 H: BinarySerializerDelegate,
55 D: DisplayCollector,
56{
57 pub fn new<WW: Into<W>>(write: WW) -> Self {
58 BinarySerializer {
59 write: write.into(),
60 phantom_data: marker::PhantomData,
61 }
62 }
63
64 pub fn consume(self) -> W {
65 self.write
66 }
67}
68
69impl<W, E, H, D> Serializer for BinarySerializer<W, E, H, D>
70where
71 W: Write,
72 E: ByteOrder,
73 H: BinarySerializerDelegate,
74 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
75{
76 type Ok = Self;
77 type Error = ErrorAdapter<Either<BinarySerializerError, W::Error>, D>;
78 type SerializeSeq = BinarySerializeSeq<W, E, H, D>;
79 type SerializeTuple = BinarySerializeTuple<W, E, H, D>;
80 type SerializeTupleStruct = BinarySerializeTupleStruct<W, E, H, D>;
81 type SerializeTupleVariant = BinarySerializeTupleVariant<W, E, H, D>;
82 type SerializeMap = BinarySerializeMap<W, E, H, D>;
83 type SerializeStruct = BinarySerializeStruct<W, E, H, D>;
84 type SerializeStructVariant = BinarySerializeStructVariant<W, E, H, D>;
85
86 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
87 self.serialize_i8(if v { 1 } else { 0 })
88 }
89
90 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
91 self.serialize_u8(v as _)
92 }
93
94 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
95 self.serialize_u16(v as _)
96 }
97
98 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
99 self.serialize_u32(v as _)
100 }
101
102 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
103 self.serialize_u64(v as _)
104 }
105
106 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
107 let mut mut_self = self;
108 mut_self
109 .write
110 .write(&[v])
111 .map_err(Either::Right)
112 .map_err(ErrorAdapter::Inner)
113 .map(|_| mut_self)
114 }
115
116 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
117 use core::mem;
118
119 let mut mut_self = self;
120 let mut buffer = [0; mem::size_of::<u16>()];
121 E::write_u16(&mut buffer, v);
122 mut_self
123 .write
124 .write(&buffer)
125 .map_err(Either::Right)
126 .map_err(ErrorAdapter::Inner)
127 .map(|_| mut_self)
128 }
129
130 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
131 use core::mem;
132
133 let mut mut_self = self;
134 let mut buffer = [0; mem::size_of::<u32>()];
135 E::write_u32(&mut buffer, v);
136 mut_self
137 .write
138 .write(&buffer)
139 .map_err(Either::Right)
140 .map_err(ErrorAdapter::Inner)
141 .map(|_| mut_self)
142 }
143
144 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
145 use core::mem;
146
147 let mut mut_self = self;
148 let mut buffer = [0; mem::size_of::<u64>()];
149 E::write_u64(&mut buffer, v);
150 mut_self
151 .write
152 .write(&buffer)
153 .map_err(Either::Right)
154 .map_err(ErrorAdapter::Inner)
155 .map(|_| mut_self)
156 }
157
158 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
159 use core::mem;
160
161 let mut mut_self = self;
162 let mut buffer = [0; mem::size_of::<f32>()];
163 E::write_f32(&mut buffer, v);
164 mut_self
165 .write
166 .write(&buffer)
167 .map_err(Either::Right)
168 .map_err(ErrorAdapter::Inner)
169 .map(|_| mut_self)
170 }
171
172 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
173 use core::mem;
174
175 let mut mut_self = self;
176 let mut buffer = [0; mem::size_of::<f64>()];
177 E::write_f64(&mut buffer, v);
178 mut_self
179 .write
180 .write(&buffer)
181 .map_err(Either::Right)
182 .map_err(ErrorAdapter::Inner)
183 .map(|_| mut_self)
184 }
185
186 fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
187 H::encode_char(v).serialize(self)
188 }
189
190 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
191 self.serialize_bytes(v.as_bytes())
192 }
193
194 fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
195 let seq = self.serialize_seq(Some(v.len()));
196 (0..v.len())
197 .fold(seq, |seq, index| {
198 seq.and_then(|mut s| s.serialize_element(&v[index]).map(|_| s))
199 })
200 .and_then(|s| s.end())
201 }
202
203 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
204 H::encode_variant(0)
205 .serialize(self)
206 }
207
208 fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
209 where
210 T: ?Sized + Serialize,
211 {
212 H::encode_variant(1)
213 .serialize(self)
214 .and_then(|s| value.serialize(s))
215 }
216
217 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
218 Ok(self)
219 }
220
221 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
222 let _ = name;
223 Ok(self)
224 }
225
226 fn serialize_unit_variant(
227 self,
228 name: &'static str,
229 variant_index: u32,
230 variant: &'static str,
231 ) -> Result<Self::Ok, Self::Error> {
232 let _ = name;
233 let _ = variant;
234 H::encode_variant(variant_index).serialize(self)
235 }
236
237 fn serialize_newtype_struct<T: ?Sized>(
238 self,
239 name: &'static str,
240 value: &T,
241 ) -> Result<Self::Ok, Self::Error>
242 where
243 T: Serialize,
244 {
245 let _ = name;
246 value.serialize(self)
247 }
248
249 fn serialize_newtype_variant<T: ?Sized>(
250 self,
251 name: &'static str,
252 variant_index: u32,
253 variant: &'static str,
254 value: &T,
255 ) -> Result<Self::Ok, Self::Error>
256 where
257 T: Serialize,
258 {
259 let _ = name;
260 let _ = variant;
261 H::encode_variant(variant_index)
262 .serialize(self)
263 .and_then(|s| value.serialize(s))
264 }
265
266 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
267 let maybe_self = match len {
268 Some(len) => H::encode_sequence_length(len).serialize(self),
269 None => Ok(self),
270 };
271 maybe_self.and_then(|x| Ok(BinarySerializeSeq { raw: Ok(x) }))
272 }
273
274 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
275 let _ = len;
276 let sequence = BinarySerializeSeq { raw: Ok(self) };
277 Ok(BinarySerializeTuple { sequence: sequence })
278 }
279
280 fn serialize_tuple_struct(
281 self,
282 name: &'static str,
283 len: usize,
284 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
285 let _ = name;
286 let _ = len;
287 let sequence = BinarySerializeSeq { raw: Ok(self) };
288 Ok(BinarySerializeTupleStruct { sequence: sequence })
289 }
290
291 fn serialize_tuple_variant(
292 self,
293 name: &'static str,
294 variant_index: u32,
295 variant: &'static str,
296 len: usize,
297 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
298 let _ = name;
299 let _ = variant;
300 let _ = len;
301 H::encode_variant(variant_index)
302 .serialize(self)
303 .and_then(|x| {
304 let sequence = BinarySerializeSeq { raw: Ok(x) };
305 Ok(BinarySerializeTupleVariant { sequence: sequence })
306 })
307 }
308
309 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
310 let maybe_self = match len {
311 Some(len) => H::encode_length(len).serialize(self),
312 None => Ok(self),
313 };
314 maybe_self.and_then(|x| {
315 let sequence = BinarySerializeSeq { raw: Ok(x) };
316 Ok(BinarySerializeMap { sequence: sequence })
317 })
318 }
319
320 fn serialize_struct(
321 self,
322 name: &'static str,
323 len: usize,
324 ) -> Result<Self::SerializeStruct, Self::Error> {
325 let _ = name;
326 let _ = len;
327 let sequence = BinarySerializeSeq { raw: Ok(self) };
328 Ok(BinarySerializeStruct { sequence: sequence })
329 }
330
331 fn serialize_struct_variant(
332 self,
333 name: &'static str,
334 variant_index: u32,
335 variant: &'static str,
336 len: usize,
337 ) -> Result<Self::SerializeStructVariant, Self::Error> {
338 let _ = name;
339 let _ = variant;
340 let _ = len;
341 H::encode_variant(variant_index)
342 .serialize(self)
343 .and_then(|x| {
344 let sequence = BinarySerializeSeq { raw: Ok(x) };
345 Ok(BinarySerializeStructVariant { sequence: sequence })
346 })
347 }
348
349 #[cfg(not(feature = "use_std"))]
350 fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
351 where
352 T: ?Sized + fmt::Display,
353 {
354 D::display(value).serialize(self)
355 }
356
357 fn is_human_readable(&self) -> bool {
358 false
359 }
360}
361
362pub struct BinarySerializeSeq<W, E, H, D>
363where
364 W: Write,
365 E: ByteOrder,
366 H: BinarySerializerDelegate,
367 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
368{
369 raw: Result<BinarySerializer<W, E, H, D>, Option<ErrorAdapter<Either<BinarySerializerError, W::Error>, D>>>,
370}
371
372impl<W, E, H, D> SerializeSeq for BinarySerializeSeq<W, E, H, D>
373where
374 W: Write,
375 E: ByteOrder,
376 H: BinarySerializerDelegate,
377 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
378{
379 type Ok = BinarySerializer<W, E, H, D>;
380 type Error = <Self::Ok as Serializer>::Error;
381
382 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
383 where
384 T: Serialize + ?Sized,
385 {
386 use core::mem;
387
388 let mut temp = Err(None);
389 mem::swap(&mut temp, &mut self.raw);
390 let mut temp = temp.and_then(|s| value.serialize(s).map_err(Some));
391 mem::swap(&mut temp, &mut self.raw);
392 self.raw.as_mut().map(|_| ()).map_err(|e| {
393 let mut temp = None;
394 mem::swap(&mut temp, e);
395 temp.unwrap()
396 })
397 }
398
399 fn end(self) -> Result<Self::Ok, Self::Error> {
400 self.raw.map_err(Option::unwrap)
401 }
402}
403
404pub struct BinarySerializeTuple<W, E, H, D>
405where
406 W: Write,
407 E: ByteOrder,
408 H: BinarySerializerDelegate,
409 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
410{
411 sequence: BinarySerializeSeq<W, E, H, D>,
412}
413
414impl<W, E, H, D> SerializeTuple for BinarySerializeTuple<W, E, H, D>
415where
416 W: Write,
417 E: ByteOrder,
418 H: BinarySerializerDelegate,
419 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
420{
421 type Ok = BinarySerializer<W, E, H, D>;
422 type Error = <Self::Ok as Serializer>::Error;
423
424 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
425 where
426 T: Serialize + ?Sized,
427 {
428 self.sequence.serialize_element(value)
429 }
430
431 fn end(self) -> Result<Self::Ok, Self::Error> {
432 self.sequence.end()
433 }
434}
435
436pub struct BinarySerializeTupleStruct<W, E, H, D>
437where
438 W: Write,
439 E: ByteOrder,
440 H: BinarySerializerDelegate,
441 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
442{
443 sequence: BinarySerializeSeq<W, E, H, D>,
444}
445
446impl<W, E, H, D> SerializeTupleStruct for BinarySerializeTupleStruct<W, E, H, D>
447where
448 W: Write,
449 E: ByteOrder,
450 H: BinarySerializerDelegate,
451 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
452{
453 type Ok = BinarySerializer<W, E, H, D>;
454 type Error = <Self::Ok as Serializer>::Error;
455
456 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
457 where
458 T: Serialize + ?Sized,
459 {
460 self.sequence.serialize_element(value)
461 }
462
463 fn end(self) -> Result<Self::Ok, Self::Error> {
464 self.sequence.end()
465 }
466}
467
468pub struct BinarySerializeTupleVariant<W, E, H, D>
469where
470 W: Write,
471 E: ByteOrder,
472 H: BinarySerializerDelegate,
473 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
474{
475 sequence: BinarySerializeSeq<W, E, H, D>,
476}
477
478impl<W, E, H, D> SerializeTupleVariant for BinarySerializeTupleVariant<W, E, H, D>
479where
480 W: Write,
481 E: ByteOrder,
482 H: BinarySerializerDelegate,
483 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
484{
485 type Ok = BinarySerializer<W, E, H, D>;
486 type Error = <Self::Ok as Serializer>::Error;
487
488 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
489 where
490 T: Serialize + ?Sized,
491 {
492 self.sequence.serialize_element(value)
493 }
494
495 fn end(self) -> Result<Self::Ok, Self::Error> {
496 self.sequence.end()
497 }
498}
499
500pub struct BinarySerializeMap<W, E, H, D>
501where
502 W: Write,
503 E: ByteOrder,
504 H: BinarySerializerDelegate,
505 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
506{
507 sequence: BinarySerializeSeq<W, E, H, D>,
508}
509
510impl<W, E, H, D> SerializeMap for BinarySerializeMap<W, E, H, D>
511where
512 W: Write,
513 E: ByteOrder,
514 H: BinarySerializerDelegate,
515 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
516{
517 type Ok = BinarySerializer<W, E, H, D>;
518 type Error = <Self::Ok as Serializer>::Error;
519
520 fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
521 where
522 T: Serialize + ?Sized,
523 {
524 self.sequence.serialize_element(key)
525 }
526
527 fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
528 where
529 T: Serialize + ?Sized,
530 {
531 self.sequence.serialize_element(value)
532 }
533
534 fn end(self) -> Result<Self::Ok, Self::Error> {
535 self.sequence.end()
536 }
537}
538
539pub struct BinarySerializeStruct<W, E, H, D>
540where
541 W: Write,
542 E: ByteOrder,
543 H: BinarySerializerDelegate,
544 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
545{
546 sequence: BinarySerializeSeq<W, E, H, D>,
547}
548
549impl<W, E, H, D> SerializeStruct for BinarySerializeStruct<W, E, H, D>
550where
551 W: Write,
552 E: ByteOrder,
553 H: BinarySerializerDelegate,
554 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
555{
556 type Ok = BinarySerializer<W, E, H, D>;
557 type Error = <Self::Ok as Serializer>::Error;
558
559 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
560 where
561 T: Serialize + ?Sized,
562 {
563 let _ = key;
564
565 self.sequence.serialize_element(value)
566 }
567
568 fn end(self) -> Result<Self::Ok, Self::Error> {
569 self.sequence.end()
570 }
571}
572
573pub struct BinarySerializeStructVariant<W, E, H, D>
574where
575 W: Write,
576 E: ByteOrder,
577 H: BinarySerializerDelegate,
578 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
579{
580 sequence: BinarySerializeSeq<W, E, H, D>,
581}
582
583impl<W, E, H, D> SerializeStructVariant for BinarySerializeStructVariant<W, E, H, D>
584where
585 W: Write,
586 E: ByteOrder,
587 H: BinarySerializerDelegate,
588 D: Serialize + DisplayCollector + fmt::Display + fmt::Debug,
589{
590 type Ok = BinarySerializer<W, E, H, D>;
591 type Error = <Self::Ok as Serializer>::Error;
592
593 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
594 where
595 T: Serialize + ?Sized,
596 {
597 let _ = key;
598
599 self.sequence.serialize_element(value)
600 }
601
602 fn end(self) -> Result<Self::Ok, Self::Error> {
603 self.sequence.end()
604 }
605}