1use std::fmt;
31
32use serde::de::{
33 self, DeserializeSeed, EnumAccess, MapAccess, SeqAccess, VariantAccess,
34 Visitor,
35};
36use serde::ser::{
37 self, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
38 SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
39};
40use serde::{Deserialize, Serialize};
41
42use crate::BindError;
43use crate::Result;
44
45pub struct SimpleSerializer {
53 output: Vec<u8>,
54}
55
56impl SimpleSerializer {
57 pub fn new() -> Self {
59 Self { output: Vec::new() }
60 }
61
62 pub fn with_capacity(capacity: usize) -> Self {
64 Self { output: Vec::with_capacity(capacity) }
65 }
66
67 pub fn into_bytes(self) -> Vec<u8> {
69 self.output
70 }
71}
72
73impl Default for SimpleSerializer {
74 fn default() -> Self {
75 Self::new()
76 }
77}
78
79pub fn to_bytes<T: Serialize>(value: &T) -> Result<Vec<u8>> {
81 let mut serializer = SimpleSerializer::new();
82 value.serialize(&mut serializer)?;
83 Ok(serializer.into_bytes())
84}
85
86pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> Result<T> {
88 let mut deserializer = SimpleDeserializer::new(bytes);
89 let value = T::deserialize(&mut deserializer)?;
90 if !deserializer.remaining().is_empty() {
91 return Err(BindError::InvalidData(format!(
92 "trailing {} bytes after deserialization",
93 deserializer.remaining().len()
94 )));
95 }
96 Ok(value)
97}
98
99#[derive(Debug)]
105pub struct SerError(BindError);
106
107impl fmt::Display for SerError {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 write!(f, "{}", self.0)
110 }
111}
112
113impl std::error::Error for SerError {}
114
115impl ser::Error for SerError {
116 fn custom<T: fmt::Display>(msg: T) -> Self {
117 SerError(BindError::InvalidData(msg.to_string()))
118 }
119}
120
121impl de::Error for SerError {
122 fn custom<T: fmt::Display>(msg: T) -> Self {
123 SerError(BindError::InvalidData(msg.to_string()))
124 }
125}
126
127impl From<BindError> for SerError {
128 fn from(e: BindError) -> Self {
129 SerError(e)
130 }
131}
132
133impl From<SerError> for BindError {
134 fn from(e: SerError) -> Self {
135 e.0
136 }
137}
138
139impl ser::Serializer for &mut SimpleSerializer {
142 type Ok = ();
143 type Error = SerError;
144
145 type SerializeSeq = Self;
146 type SerializeTuple = Self;
147 type SerializeTupleStruct = Self;
148 type SerializeTupleVariant = Self;
149 type SerializeMap = Self;
150 type SerializeStruct = Self;
151 type SerializeStructVariant = Self;
152
153 fn serialize_bool(self, v: bool) -> std::result::Result<(), SerError> {
154 self.output.push(if v { 1 } else { 0 });
155 Ok(())
156 }
157
158 fn serialize_i8(self, v: i8) -> std::result::Result<(), SerError> {
159 self.output.push(v as u8);
160 Ok(())
161 }
162
163 fn serialize_i16(self, v: i16) -> std::result::Result<(), SerError> {
164 self.output.extend_from_slice(&v.to_be_bytes());
165 Ok(())
166 }
167
168 fn serialize_i32(self, v: i32) -> std::result::Result<(), SerError> {
169 self.output.extend_from_slice(&v.to_be_bytes());
170 Ok(())
171 }
172
173 fn serialize_i64(self, v: i64) -> std::result::Result<(), SerError> {
174 self.output.extend_from_slice(&v.to_be_bytes());
175 Ok(())
176 }
177
178 fn serialize_i128(self, v: i128) -> std::result::Result<(), SerError> {
179 self.output.extend_from_slice(&v.to_be_bytes());
180 Ok(())
181 }
182
183 fn serialize_u8(self, v: u8) -> std::result::Result<(), SerError> {
184 self.output.push(v);
185 Ok(())
186 }
187
188 fn serialize_u16(self, v: u16) -> std::result::Result<(), SerError> {
189 self.output.extend_from_slice(&v.to_be_bytes());
190 Ok(())
191 }
192
193 fn serialize_u32(self, v: u32) -> std::result::Result<(), SerError> {
194 self.output.extend_from_slice(&v.to_be_bytes());
195 Ok(())
196 }
197
198 fn serialize_u64(self, v: u64) -> std::result::Result<(), SerError> {
199 self.output.extend_from_slice(&v.to_be_bytes());
200 Ok(())
201 }
202
203 fn serialize_u128(self, v: u128) -> std::result::Result<(), SerError> {
204 self.output.extend_from_slice(&v.to_be_bytes());
205 Ok(())
206 }
207
208 fn serialize_f32(self, v: f32) -> std::result::Result<(), SerError> {
209 self.output.extend_from_slice(&v.to_be_bytes());
210 Ok(())
211 }
212
213 fn serialize_f64(self, v: f64) -> std::result::Result<(), SerError> {
214 self.output.extend_from_slice(&v.to_be_bytes());
215 Ok(())
216 }
217
218 fn serialize_char(self, v: char) -> std::result::Result<(), SerError> {
219 self.output.extend_from_slice(&(v as u32).to_be_bytes());
220 Ok(())
221 }
222
223 fn serialize_str(self, v: &str) -> std::result::Result<(), SerError> {
224 let len_usize = v.len();
228 let len: u32 = u32::try_from(len_usize).map_err(|_| {
229 SerError(BindError::InvalidData(format!(
230 "serialize_str: string length {} exceeds u32 max ({})",
231 len_usize,
232 u32::MAX,
233 )))
234 })?;
235 self.output.extend_from_slice(&len.to_be_bytes());
236 self.output.extend_from_slice(v.as_bytes());
237 Ok(())
238 }
239
240 fn serialize_bytes(self, v: &[u8]) -> std::result::Result<(), SerError> {
241 let len_usize = v.len();
243 let len: u32 = u32::try_from(len_usize).map_err(|_| {
244 SerError(BindError::InvalidData(format!(
245 "serialize_bytes: payload length {} exceeds u32 max ({})",
246 len_usize,
247 u32::MAX,
248 )))
249 })?;
250 self.output.extend_from_slice(&len.to_be_bytes());
251 self.output.extend_from_slice(v);
252 Ok(())
253 }
254
255 fn serialize_none(self) -> std::result::Result<(), SerError> {
256 self.output.push(0);
257 Ok(())
258 }
259
260 fn serialize_some<T: ?Sized + Serialize>(
261 self,
262 value: &T,
263 ) -> std::result::Result<(), SerError> {
264 self.output.push(1);
265 value.serialize(self)
266 }
267
268 fn serialize_unit(self) -> std::result::Result<(), SerError> {
269 Ok(())
270 }
271
272 fn serialize_unit_struct(
273 self,
274 _name: &'static str,
275 ) -> std::result::Result<(), SerError> {
276 Ok(())
277 }
278
279 fn serialize_unit_variant(
280 self,
281 _name: &'static str,
282 variant_index: u32,
283 _variant: &'static str,
284 ) -> std::result::Result<(), SerError> {
285 self.output.extend_from_slice(&variant_index.to_be_bytes());
286 Ok(())
287 }
288
289 fn serialize_newtype_struct<T: ?Sized + Serialize>(
290 self,
291 _name: &'static str,
292 value: &T,
293 ) -> std::result::Result<(), SerError> {
294 value.serialize(self)
295 }
296
297 fn serialize_newtype_variant<T: ?Sized + Serialize>(
298 self,
299 _name: &'static str,
300 variant_index: u32,
301 _variant: &'static str,
302 value: &T,
303 ) -> std::result::Result<(), SerError> {
304 self.output.extend_from_slice(&variant_index.to_be_bytes());
305 value.serialize(self)
306 }
307
308 fn serialize_seq(
309 self,
310 len: Option<usize>,
311 ) -> std::result::Result<Self::SerializeSeq, SerError> {
312 let count = len.ok_or_else(|| {
313 SerError(BindError::UnsupportedType(
314 "sequences must have known length".to_string(),
315 ))
316 })? as u32;
317 self.output.extend_from_slice(&count.to_be_bytes());
318 Ok(self)
319 }
320
321 fn serialize_tuple(
322 self,
323 _len: usize,
324 ) -> std::result::Result<Self::SerializeTuple, SerError> {
325 Ok(self)
326 }
327
328 fn serialize_tuple_struct(
329 self,
330 _name: &'static str,
331 _len: usize,
332 ) -> std::result::Result<Self::SerializeTupleStruct, SerError> {
333 Ok(self)
334 }
335
336 fn serialize_tuple_variant(
337 self,
338 _name: &'static str,
339 variant_index: u32,
340 _variant: &'static str,
341 _len: usize,
342 ) -> std::result::Result<Self::SerializeTupleVariant, SerError> {
343 self.output.extend_from_slice(&variant_index.to_be_bytes());
344 Ok(self)
345 }
346
347 fn serialize_map(
348 self,
349 len: Option<usize>,
350 ) -> std::result::Result<Self::SerializeMap, SerError> {
351 let count = len.ok_or_else(|| {
352 SerError(BindError::UnsupportedType(
353 "maps must have known length".to_string(),
354 ))
355 })? as u32;
356 self.output.extend_from_slice(&count.to_be_bytes());
357 Ok(self)
358 }
359
360 fn serialize_struct(
361 self,
362 _name: &'static str,
363 _len: usize,
364 ) -> std::result::Result<Self::SerializeStruct, SerError> {
365 Ok(self)
366 }
367
368 fn serialize_struct_variant(
369 self,
370 _name: &'static str,
371 variant_index: u32,
372 _variant: &'static str,
373 _len: usize,
374 ) -> std::result::Result<Self::SerializeStructVariant, SerError> {
375 self.output.extend_from_slice(&variant_index.to_be_bytes());
376 Ok(self)
377 }
378}
379
380impl SerializeSeq for &mut SimpleSerializer {
381 type Ok = ();
382 type Error = SerError;
383
384 fn serialize_element<T: ?Sized + Serialize>(
385 &mut self,
386 value: &T,
387 ) -> std::result::Result<(), SerError> {
388 value.serialize(&mut **self)
389 }
390
391 fn end(self) -> std::result::Result<(), SerError> {
392 Ok(())
393 }
394}
395
396impl SerializeTuple for &mut SimpleSerializer {
397 type Ok = ();
398 type Error = SerError;
399
400 fn serialize_element<T: ?Sized + Serialize>(
401 &mut self,
402 value: &T,
403 ) -> std::result::Result<(), SerError> {
404 value.serialize(&mut **self)
405 }
406
407 fn end(self) -> std::result::Result<(), SerError> {
408 Ok(())
409 }
410}
411
412impl SerializeTupleStruct for &mut SimpleSerializer {
413 type Ok = ();
414 type Error = SerError;
415
416 fn serialize_field<T: ?Sized + Serialize>(
417 &mut self,
418 value: &T,
419 ) -> std::result::Result<(), SerError> {
420 value.serialize(&mut **self)
421 }
422
423 fn end(self) -> std::result::Result<(), SerError> {
424 Ok(())
425 }
426}
427
428impl SerializeTupleVariant for &mut SimpleSerializer {
429 type Ok = ();
430 type Error = SerError;
431
432 fn serialize_field<T: ?Sized + Serialize>(
433 &mut self,
434 value: &T,
435 ) -> std::result::Result<(), SerError> {
436 value.serialize(&mut **self)
437 }
438
439 fn end(self) -> std::result::Result<(), SerError> {
440 Ok(())
441 }
442}
443
444impl SerializeMap for &mut SimpleSerializer {
445 type Ok = ();
446 type Error = SerError;
447
448 fn serialize_key<T: ?Sized + Serialize>(
449 &mut self,
450 key: &T,
451 ) -> std::result::Result<(), SerError> {
452 key.serialize(&mut **self)
453 }
454
455 fn serialize_value<T: ?Sized + Serialize>(
456 &mut self,
457 value: &T,
458 ) -> std::result::Result<(), SerError> {
459 value.serialize(&mut **self)
460 }
461
462 fn end(self) -> std::result::Result<(), SerError> {
463 Ok(())
464 }
465}
466
467impl SerializeStruct for &mut SimpleSerializer {
468 type Ok = ();
469 type Error = SerError;
470
471 fn serialize_field<T: ?Sized + Serialize>(
472 &mut self,
473 _key: &'static str,
474 value: &T,
475 ) -> std::result::Result<(), SerError> {
476 value.serialize(&mut **self)
477 }
478
479 fn end(self) -> std::result::Result<(), SerError> {
480 Ok(())
481 }
482}
483
484impl SerializeStructVariant for &mut SimpleSerializer {
485 type Ok = ();
486 type Error = SerError;
487
488 fn serialize_field<T: ?Sized + Serialize>(
489 &mut self,
490 _key: &'static str,
491 value: &T,
492 ) -> std::result::Result<(), SerError> {
493 value.serialize(&mut **self)
494 }
495
496 fn end(self) -> std::result::Result<(), SerError> {
497 Ok(())
498 }
499}
500
501pub struct SimpleDeserializer<'de> {
510 input: &'de [u8],
511 pos: usize,
512}
513
514impl<'de> SimpleDeserializer<'de> {
515 pub fn new(input: &'de [u8]) -> Self {
517 Self { input, pos: 0 }
518 }
519
520 pub fn remaining(&self) -> &'de [u8] {
522 &self.input[self.pos..]
523 }
524
525 fn read_bytes(
526 &mut self,
527 n: usize,
528 ) -> std::result::Result<&'de [u8], SerError> {
529 if self.pos + n > self.input.len() {
530 return Err(SerError(BindError::BufferUnderflow {
531 needed: n,
532 available: self.input.len() - self.pos,
533 }));
534 }
535 let slice = &self.input[self.pos..self.pos + n];
536 self.pos += n;
537 Ok(slice)
538 }
539
540 fn read_u8(&mut self) -> std::result::Result<u8, SerError> {
541 let b = self.read_bytes(1)?;
542 Ok(b[0])
543 }
544
545 fn read_u16(&mut self) -> std::result::Result<u16, SerError> {
546 let b = self.read_bytes(2)?;
547 Ok(u16::from_be_bytes([b[0], b[1]]))
548 }
549
550 fn read_u32(&mut self) -> std::result::Result<u32, SerError> {
551 let b = self.read_bytes(4)?;
552 Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
553 }
554
555 fn read_u64(&mut self) -> std::result::Result<u64, SerError> {
556 let b = self.read_bytes(8)?;
557 Ok(u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]))
558 }
559
560 fn read_u128(&mut self) -> std::result::Result<u128, SerError> {
561 let b = self.read_bytes(16)?;
562 let mut arr = [0u8; 16];
563 arr.copy_from_slice(b);
564 Ok(u128::from_be_bytes(arr))
565 }
566}
567
568impl<'de> de::Deserializer<'de> for &mut SimpleDeserializer<'de> {
569 type Error = SerError;
570
571 fn deserialize_any<V: Visitor<'de>>(
572 self,
573 _visitor: V,
574 ) -> std::result::Result<V::Value, SerError> {
575 Err(SerError(BindError::UnsupportedType(
576 "deserialize_any is not supported; use a typed deserialize method"
577 .to_string(),
578 )))
579 }
580
581 fn deserialize_bool<V: Visitor<'de>>(
582 self,
583 visitor: V,
584 ) -> std::result::Result<V::Value, SerError> {
585 let b = self.read_u8()?;
586 visitor.visit_bool(b != 0)
587 }
588
589 fn deserialize_i8<V: Visitor<'de>>(
590 self,
591 visitor: V,
592 ) -> std::result::Result<V::Value, SerError> {
593 let b = self.read_u8()?;
594 visitor.visit_i8(b as i8)
595 }
596
597 fn deserialize_i16<V: Visitor<'de>>(
598 self,
599 visitor: V,
600 ) -> std::result::Result<V::Value, SerError> {
601 let v = self.read_u16()?;
602 visitor.visit_i16(v as i16)
603 }
604
605 fn deserialize_i32<V: Visitor<'de>>(
606 self,
607 visitor: V,
608 ) -> std::result::Result<V::Value, SerError> {
609 let v = self.read_u32()?;
610 visitor.visit_i32(v as i32)
611 }
612
613 fn deserialize_i64<V: Visitor<'de>>(
614 self,
615 visitor: V,
616 ) -> std::result::Result<V::Value, SerError> {
617 let v = self.read_u64()?;
618 visitor.visit_i64(v as i64)
619 }
620
621 fn deserialize_i128<V: Visitor<'de>>(
622 self,
623 visitor: V,
624 ) -> std::result::Result<V::Value, SerError> {
625 let v = self.read_u128()?;
626 visitor.visit_i128(v as i128)
627 }
628
629 fn deserialize_u8<V: Visitor<'de>>(
630 self,
631 visitor: V,
632 ) -> std::result::Result<V::Value, SerError> {
633 let b = self.read_u8()?;
634 visitor.visit_u8(b)
635 }
636
637 fn deserialize_u16<V: Visitor<'de>>(
638 self,
639 visitor: V,
640 ) -> std::result::Result<V::Value, SerError> {
641 let v = self.read_u16()?;
642 visitor.visit_u16(v)
643 }
644
645 fn deserialize_u32<V: Visitor<'de>>(
646 self,
647 visitor: V,
648 ) -> std::result::Result<V::Value, SerError> {
649 let v = self.read_u32()?;
650 visitor.visit_u32(v)
651 }
652
653 fn deserialize_u64<V: Visitor<'de>>(
654 self,
655 visitor: V,
656 ) -> std::result::Result<V::Value, SerError> {
657 let v = self.read_u64()?;
658 visitor.visit_u64(v)
659 }
660
661 fn deserialize_u128<V: Visitor<'de>>(
662 self,
663 visitor: V,
664 ) -> std::result::Result<V::Value, SerError> {
665 let v = self.read_u128()?;
666 visitor.visit_u128(v)
667 }
668
669 fn deserialize_f32<V: Visitor<'de>>(
670 self,
671 visitor: V,
672 ) -> std::result::Result<V::Value, SerError> {
673 let v = self.read_u32()?;
674 visitor.visit_f32(f32::from_bits(v))
675 }
676
677 fn deserialize_f64<V: Visitor<'de>>(
678 self,
679 visitor: V,
680 ) -> std::result::Result<V::Value, SerError> {
681 let v = self.read_u64()?;
682 visitor.visit_f64(f64::from_bits(v))
683 }
684
685 fn deserialize_char<V: Visitor<'de>>(
686 self,
687 visitor: V,
688 ) -> std::result::Result<V::Value, SerError> {
689 let v = self.read_u32()?;
690 let c = char::from_u32(v).ok_or_else(|| {
691 SerError(BindError::InvalidData(format!(
692 "invalid char code point: {}",
693 v
694 )))
695 })?;
696 visitor.visit_char(c)
697 }
698
699 fn deserialize_str<V: Visitor<'de>>(
700 self,
701 visitor: V,
702 ) -> std::result::Result<V::Value, SerError> {
703 let len = self.read_u32()? as usize;
704 let bytes = self.read_bytes(len)?;
705 let s = std::str::from_utf8(bytes)
706 .map_err(|e| SerError(BindError::StringEncoding(e.to_string())))?;
707 visitor.visit_borrowed_str(s)
708 }
709
710 fn deserialize_string<V: Visitor<'de>>(
711 self,
712 visitor: V,
713 ) -> std::result::Result<V::Value, SerError> {
714 self.deserialize_str(visitor)
715 }
716
717 fn deserialize_bytes<V: Visitor<'de>>(
718 self,
719 visitor: V,
720 ) -> std::result::Result<V::Value, SerError> {
721 let len = self.read_u32()? as usize;
722 let bytes = self.read_bytes(len)?;
723 visitor.visit_borrowed_bytes(bytes)
724 }
725
726 fn deserialize_byte_buf<V: Visitor<'de>>(
727 self,
728 visitor: V,
729 ) -> std::result::Result<V::Value, SerError> {
730 self.deserialize_bytes(visitor)
731 }
732
733 fn deserialize_option<V: Visitor<'de>>(
734 self,
735 visitor: V,
736 ) -> std::result::Result<V::Value, SerError> {
737 let tag = self.read_u8()?;
738 match tag {
739 0 => visitor.visit_none(),
740 1 => visitor.visit_some(self),
741 _ => Err(SerError(BindError::InvalidData(format!(
742 "invalid option tag: {}",
743 tag
744 )))),
745 }
746 }
747
748 fn deserialize_unit<V: Visitor<'de>>(
749 self,
750 visitor: V,
751 ) -> std::result::Result<V::Value, SerError> {
752 visitor.visit_unit()
753 }
754
755 fn deserialize_unit_struct<V: Visitor<'de>>(
756 self,
757 _name: &'static str,
758 visitor: V,
759 ) -> std::result::Result<V::Value, SerError> {
760 visitor.visit_unit()
761 }
762
763 fn deserialize_newtype_struct<V: Visitor<'de>>(
764 self,
765 _name: &'static str,
766 visitor: V,
767 ) -> std::result::Result<V::Value, SerError> {
768 visitor.visit_newtype_struct(self)
769 }
770
771 fn deserialize_seq<V: Visitor<'de>>(
772 self,
773 visitor: V,
774 ) -> std::result::Result<V::Value, SerError> {
775 let count = self.read_u32()? as usize;
776 visitor.visit_seq(CountedAccess { de: self, remaining: count })
777 }
778
779 fn deserialize_tuple<V: Visitor<'de>>(
780 self,
781 len: usize,
782 visitor: V,
783 ) -> std::result::Result<V::Value, SerError> {
784 visitor.visit_seq(CountedAccess { de: self, remaining: len })
785 }
786
787 fn deserialize_tuple_struct<V: Visitor<'de>>(
788 self,
789 _name: &'static str,
790 len: usize,
791 visitor: V,
792 ) -> std::result::Result<V::Value, SerError> {
793 visitor.visit_seq(CountedAccess { de: self, remaining: len })
794 }
795
796 fn deserialize_map<V: Visitor<'de>>(
797 self,
798 visitor: V,
799 ) -> std::result::Result<V::Value, SerError> {
800 let count = self.read_u32()? as usize;
801 visitor.visit_map(CountedAccess { de: self, remaining: count })
802 }
803
804 fn deserialize_struct<V: Visitor<'de>>(
805 self,
806 _name: &'static str,
807 fields: &'static [&'static str],
808 visitor: V,
809 ) -> std::result::Result<V::Value, SerError> {
810 visitor.visit_seq(CountedAccess { de: self, remaining: fields.len() })
811 }
812
813 fn deserialize_enum<V: Visitor<'de>>(
814 self,
815 _name: &'static str,
816 _variants: &'static [&'static str],
817 visitor: V,
818 ) -> std::result::Result<V::Value, SerError> {
819 visitor.visit_enum(EnumDeserializer { de: self })
820 }
821
822 fn deserialize_identifier<V: Visitor<'de>>(
823 self,
824 visitor: V,
825 ) -> std::result::Result<V::Value, SerError> {
826 let idx = self.read_u32()?;
827 visitor.visit_u32(idx)
828 }
829
830 fn deserialize_ignored_any<V: Visitor<'de>>(
831 self,
832 _visitor: V,
833 ) -> std::result::Result<V::Value, SerError> {
834 Err(SerError(BindError::UnsupportedType(
835 "deserialize_ignored_any is not supported".to_string(),
836 )))
837 }
838}
839
840struct CountedAccess<'a, 'de> {
843 de: &'a mut SimpleDeserializer<'de>,
844 remaining: usize,
845}
846
847impl<'de, 'a> SeqAccess<'de> for CountedAccess<'a, 'de> {
848 type Error = SerError;
849
850 fn next_element_seed<T: DeserializeSeed<'de>>(
851 &mut self,
852 seed: T,
853 ) -> std::result::Result<Option<T::Value>, SerError> {
854 if self.remaining == 0 {
855 return Ok(None);
856 }
857 self.remaining -= 1;
858 seed.deserialize(&mut *self.de).map(Some)
859 }
860}
861
862impl<'de, 'a> MapAccess<'de> for CountedAccess<'a, 'de> {
863 type Error = SerError;
864
865 fn next_key_seed<K: DeserializeSeed<'de>>(
866 &mut self,
867 seed: K,
868 ) -> std::result::Result<Option<K::Value>, SerError> {
869 if self.remaining == 0 {
870 return Ok(None);
871 }
872 self.remaining -= 1;
873 seed.deserialize(&mut *self.de).map(Some)
874 }
875
876 fn next_value_seed<V: DeserializeSeed<'de>>(
877 &mut self,
878 seed: V,
879 ) -> std::result::Result<V::Value, SerError> {
880 seed.deserialize(&mut *self.de)
881 }
882}
883
884struct EnumDeserializer<'a, 'de> {
887 de: &'a mut SimpleDeserializer<'de>,
888}
889
890impl<'de, 'a> EnumAccess<'de> for EnumDeserializer<'a, 'de> {
891 type Error = SerError;
892 type Variant = Self;
893
894 fn variant_seed<V: DeserializeSeed<'de>>(
895 self,
896 seed: V,
897 ) -> std::result::Result<(V::Value, Self::Variant), SerError> {
898 let idx = self.de.read_u32()?;
899 let val = seed.deserialize(u32_into_deserializer(idx))?;
900 Ok((val, self))
901 }
902}
903
904struct U32Deserializer(u32);
905
906fn u32_into_deserializer(v: u32) -> U32Deserializer {
907 U32Deserializer(v)
908}
909
910impl<'de> de::Deserializer<'de> for U32Deserializer {
911 type Error = SerError;
912
913 fn deserialize_any<V: Visitor<'de>>(
914 self,
915 visitor: V,
916 ) -> std::result::Result<V::Value, SerError> {
917 visitor.visit_u32(self.0)
918 }
919
920 serde::forward_to_deserialize_any! {
921 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
922 bytes byte_buf option unit unit_struct newtype_struct seq tuple
923 tuple_struct map struct enum identifier ignored_any
924 }
925}
926
927impl<'de, 'a> VariantAccess<'de> for EnumDeserializer<'a, 'de> {
928 type Error = SerError;
929
930 fn unit_variant(self) -> std::result::Result<(), SerError> {
931 Ok(())
932 }
933
934 fn newtype_variant_seed<T: DeserializeSeed<'de>>(
935 self,
936 seed: T,
937 ) -> std::result::Result<T::Value, SerError> {
938 seed.deserialize(self.de)
939 }
940
941 fn tuple_variant<V: Visitor<'de>>(
942 self,
943 len: usize,
944 visitor: V,
945 ) -> std::result::Result<V::Value, SerError> {
946 visitor.visit_seq(CountedAccess { de: self.de, remaining: len })
947 }
948
949 fn struct_variant<V: Visitor<'de>>(
950 self,
951 fields: &'static [&'static str],
952 visitor: V,
953 ) -> std::result::Result<V::Value, SerError> {
954 visitor
955 .visit_seq(CountedAccess { de: self.de, remaining: fields.len() })
956 }
957}
958
959#[cfg(test)]
964mod tests {
965 use super::*;
966 use serde::{Deserialize, Serialize};
967 use std::collections::HashMap;
968
969 fn round_trip<
970 T: Serialize + for<'de> Deserialize<'de> + PartialEq + fmt::Debug,
971 >(
972 val: &T,
973 ) {
974 let bytes = to_bytes(val).expect("serialize");
975 let decoded: T = from_bytes(&bytes).expect("deserialize");
976 assert_eq!(&decoded, val);
977 }
978
979 #[test]
980 fn test_bool_true() {
981 round_trip(&true);
982 }
983
984 #[test]
985 fn test_bool_false() {
986 round_trip(&false);
987 }
988
989 #[test]
990 fn test_u8() {
991 round_trip(&42u8);
992 round_trip(&0u8);
993 round_trip(&255u8);
994 }
995
996 #[test]
997 fn test_i8() {
998 round_trip(&-1i8);
999 round_trip(&127i8);
1000 round_trip(&-128i8);
1001 }
1002
1003 #[test]
1004 fn test_u16() {
1005 round_trip(&0u16);
1006 round_trip(&12345u16);
1007 round_trip(&u16::MAX);
1008 }
1009
1010 #[test]
1011 fn test_i16() {
1012 round_trip(&-1i16);
1013 round_trip(&i16::MIN);
1014 round_trip(&i16::MAX);
1015 }
1016
1017 #[test]
1018 fn test_u32() {
1019 round_trip(&0u32);
1020 round_trip(&1_000_000u32);
1021 round_trip(&u32::MAX);
1022 }
1023
1024 #[test]
1025 fn test_i32() {
1026 round_trip(&-42i32);
1027 round_trip(&i32::MIN);
1028 round_trip(&i32::MAX);
1029 }
1030
1031 #[test]
1032 fn test_u64() {
1033 round_trip(&0u64);
1034 round_trip(&u64::MAX);
1035 }
1036
1037 #[test]
1038 fn test_i64() {
1039 round_trip(&-99i64);
1040 round_trip(&i64::MIN);
1041 round_trip(&i64::MAX);
1042 }
1043
1044 #[test]
1045 fn test_u128() {
1046 round_trip(&0u128);
1047 round_trip(&u128::MAX);
1048 }
1049
1050 #[test]
1051 fn test_i128() {
1052 round_trip(&-1i128);
1053 round_trip(&i128::MAX);
1054 }
1055
1056 #[test]
1057 fn test_f32() {
1058 round_trip(&std::f32::consts::PI);
1059 round_trip(&0.0f32);
1060 round_trip(&f32::NEG_INFINITY);
1061 }
1062
1063 #[test]
1064 fn test_f64() {
1065 round_trip(&std::f64::consts::E);
1066 round_trip(&f64::MAX);
1067 round_trip(&f64::MIN);
1068 }
1069
1070 #[test]
1071 fn test_char() {
1072 round_trip(&'a');
1073 round_trip(&'\u{1F600}'); round_trip(&'\0');
1075 }
1076
1077 #[test]
1078 fn test_string_empty() {
1079 round_trip(&String::new());
1080 }
1081
1082 #[test]
1083 fn test_string_ascii() {
1084 round_trip(&"hello world".to_string());
1085 }
1086
1087 #[test]
1088 fn test_string_unicode() {
1089 round_trip(&"cafe\u{0301} \u{1F600}".to_string());
1090 }
1091
1092 #[test]
1093 fn test_option_none() {
1094 let v: Option<u32> = None;
1095 round_trip(&v);
1096 }
1097
1098 #[test]
1099 fn test_option_some() {
1100 round_trip(&Some(42u32));
1101 round_trip(&Some("hello".to_string()));
1102 }
1103
1104 #[test]
1105 fn test_vec_empty() {
1106 let v: Vec<u32> = vec![];
1107 round_trip(&v);
1108 }
1109
1110 #[test]
1111 fn test_vec_ints() {
1112 round_trip(&vec![1u32, 2, 3, 4, 5]);
1113 }
1114
1115 #[test]
1116 fn test_vec_strings() {
1117 round_trip(&vec!["a".to_string(), "bb".to_string(), "ccc".to_string()]);
1118 }
1119
1120 #[test]
1121 fn test_tuple() {
1122 round_trip(&(1u32, 2u64, true));
1123 }
1124
1125 #[test]
1126 fn test_nested_option() {
1127 let v: Option<Option<u32>> = Some(Some(42));
1128 round_trip(&v);
1129 let v2: Option<Option<u32>> = Some(None);
1130 round_trip(&v2);
1131 }
1132
1133 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1134 struct Simple {
1135 x: u32,
1136 y: String,
1137 }
1138
1139 #[test]
1140 fn test_struct_simple() {
1141 round_trip(&Simple { x: 42, y: "hello".to_string() });
1142 }
1143
1144 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1145 struct Nested {
1146 inner: Simple,
1147 flag: bool,
1148 }
1149
1150 #[test]
1151 fn test_struct_nested() {
1152 round_trip(&Nested {
1153 inner: Simple { x: 100, y: "nested".to_string() },
1154 flag: true,
1155 });
1156 }
1157
1158 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1159 struct WithOption {
1160 name: String,
1161 value: Option<u64>,
1162 }
1163
1164 #[test]
1165 fn test_struct_with_option() {
1166 round_trip(&WithOption { name: "test".to_string(), value: Some(999) });
1167 round_trip(&WithOption { name: "empty".to_string(), value: None });
1168 }
1169
1170 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1171 enum Color {
1172 Red,
1173 Green,
1174 Blue,
1175 }
1176
1177 #[test]
1178 fn test_enum_unit_variants() {
1179 round_trip(&Color::Red);
1180 round_trip(&Color::Green);
1181 round_trip(&Color::Blue);
1182 }
1183
1184 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1185 enum Shape {
1186 Circle(f64),
1187 Rectangle(f64, f64),
1188 Named { name: String, sides: u32 },
1189 }
1190
1191 #[test]
1192 fn test_enum_newtype_variant() {
1193 round_trip(&Shape::Circle(std::f64::consts::PI));
1194 }
1195
1196 #[test]
1197 fn test_enum_tuple_variant() {
1198 round_trip(&Shape::Rectangle(10.0, 20.0));
1199 }
1200
1201 #[test]
1202 fn test_enum_struct_variant() {
1203 round_trip(&Shape::Named { name: "pentagon".to_string(), sides: 5 });
1204 }
1205
1206 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1207 struct UnitStruct;
1208
1209 #[test]
1210 fn test_unit_struct() {
1211 round_trip(&UnitStruct);
1212 }
1213
1214 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1215 struct NewtypeStruct(u64);
1216
1217 #[test]
1218 fn test_newtype_struct() {
1219 round_trip(&NewtypeStruct(42));
1220 }
1221
1222 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1223 struct TupleStruct(u32, String, bool);
1224
1225 #[test]
1226 fn test_tuple_struct() {
1227 round_trip(&TupleStruct(1, "x".to_string(), false));
1228 }
1229
1230 #[test]
1231 fn test_vec_of_structs() {
1232 let v = vec![
1233 Simple { x: 1, y: "a".to_string() },
1234 Simple { x: 2, y: "b".to_string() },
1235 ];
1236 round_trip(&v);
1237 }
1238
1239 #[test]
1240 fn test_hashmap() {
1241 let mut map = HashMap::new();
1244 map.insert("key".to_string(), 42u32);
1245 let bytes = to_bytes(&map).expect("serialize");
1246 let decoded: HashMap<String, u32> =
1247 from_bytes(&bytes).expect("deserialize");
1248 assert_eq!(decoded, map);
1249 }
1250
1251 #[test]
1252 fn test_hashmap_multi() {
1253 let mut map = HashMap::new();
1254 map.insert(1u32, "one".to_string());
1255 map.insert(2u32, "two".to_string());
1256 map.insert(3u32, "three".to_string());
1257 let bytes = to_bytes(&map).expect("serialize");
1258 let decoded: HashMap<u32, String> =
1259 from_bytes(&bytes).expect("deserialize");
1260 assert_eq!(decoded, map);
1261 }
1262
1263 #[test]
1264 fn test_buffer_underflow() {
1265 let result: std::result::Result<u32, _> = from_bytes(&[0, 0]);
1266 assert!(result.is_err());
1267 }
1268
1269 #[test]
1270 fn test_trailing_bytes_error() {
1271 let mut padded = to_bytes(&42u32).expect("serialize");
1272 padded.push(0xFF);
1273 let result: std::result::Result<u32, _> = from_bytes(&padded);
1274 assert!(result.is_err());
1275 }
1276
1277 #[test]
1278 fn test_empty_bytes() {
1279 let result: std::result::Result<u32, _> = from_bytes(&[]);
1280 assert!(result.is_err());
1281 }
1282
1283 #[test]
1284 fn test_bytes_serialization() {
1285 round_trip(&vec![1u8, 2u8, 3u8]);
1288 }
1289
1290 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1291 struct Complex {
1292 id: u64,
1293 name: String,
1294 tags: Vec<String>,
1295 metadata: Option<Nested>,
1296 color: Color,
1297 }
1298
1299 #[test]
1300 fn test_complex_struct() {
1301 round_trip(&Complex {
1302 id: 12345,
1303 name: "complex test".to_string(),
1304 tags: vec!["a".to_string(), "b".to_string()],
1305 metadata: Some(Nested {
1306 inner: Simple { x: 7, y: "deep".to_string() },
1307 flag: false,
1308 }),
1309 color: Color::Blue,
1310 });
1311 }
1312
1313 #[test]
1314 fn test_complex_struct_none_metadata() {
1315 round_trip(&Complex {
1316 id: 0,
1317 name: String::new(),
1318 tags: vec![],
1319 metadata: None,
1320 color: Color::Red,
1321 });
1322 }
1323
1324 #[test]
1325 fn test_bool_encoding() {
1326 let bytes = to_bytes(&true).unwrap();
1327 assert_eq!(bytes, vec![1]);
1328 let bytes = to_bytes(&false).unwrap();
1329 assert_eq!(bytes, vec![0]);
1330 }
1331
1332 #[test]
1333 fn test_u32_encoding_big_endian() {
1334 let bytes = to_bytes(&0x01020304u32).unwrap();
1335 assert_eq!(bytes, vec![0x01, 0x02, 0x03, 0x04]);
1336 }
1337
1338 #[test]
1339 fn test_string_encoding_format() {
1340 let bytes = to_bytes(&"hi".to_string()).unwrap();
1341 assert_eq!(bytes, vec![0, 0, 0, 2, b'h', b'i']);
1343 }
1344
1345 #[test]
1346 fn test_option_encoding_format() {
1347 let bytes_none = to_bytes(&Option::<u8>::None).unwrap();
1348 assert_eq!(bytes_none, vec![0]);
1349
1350 let bytes_some = to_bytes(&Some(42u8)).unwrap();
1351 assert_eq!(bytes_some, vec![1, 42]);
1352 }
1353
1354 #[test]
1357 fn test_with_capacity() {
1358 let mut ser = SimpleSerializer::with_capacity(64);
1359 ser.output.push(0x42);
1360 let bytes = ser.into_bytes();
1361 assert_eq!(bytes, vec![0x42]);
1362 }
1363
1364 #[test]
1365 fn test_default_serializer() {
1366 let ser = SimpleSerializer::default();
1367 let bytes = ser.into_bytes();
1368 assert!(bytes.is_empty());
1369 }
1370
1371 #[test]
1374 fn test_f32_nan_round_trip() {
1375 let bytes = to_bytes(&f32::NAN).unwrap();
1377 assert_eq!(bytes.len(), 4);
1378 let bits = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
1379 assert!(f32::from_bits(bits).is_nan());
1380 }
1381
1382 #[test]
1383 fn test_f64_nan_round_trip() {
1384 let bytes = to_bytes(&f64::NAN).unwrap();
1385 assert_eq!(bytes.len(), 8);
1386 let bits = u64::from_be_bytes(bytes.try_into().unwrap());
1387 assert!(f64::from_bits(bits).is_nan());
1388 }
1389
1390 #[test]
1391 fn test_f32_infinity_round_trip() {
1392 round_trip(&f32::INFINITY);
1393 }
1394
1395 #[test]
1396 fn test_f64_infinity_round_trip() {
1397 round_trip(&f64::INFINITY);
1398 round_trip(&f64::NEG_INFINITY);
1399 }
1400
1401 #[test]
1402 fn test_f32_zero_round_trip() {
1403 round_trip(&0.0f32);
1404 round_trip(&-0.0f32);
1405 }
1406
1407 #[test]
1408 fn test_f64_zero_round_trip() {
1409 round_trip(&0.0f64);
1410 }
1411
1412 #[test]
1415 fn test_deserialize_any_returns_error() {
1416 use serde::de::Deserializer;
1417 use serde::de::Visitor;
1418 struct AnyVisitor;
1419 impl<'de> Visitor<'de> for AnyVisitor {
1420 type Value = ();
1421 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
1422 write!(f, "anything")
1423 }
1424 }
1425 let mut de = SimpleDeserializer::new(&[]);
1426 let result = de.deserialize_any(AnyVisitor);
1427 assert!(result.is_err());
1428 }
1429
1430 #[test]
1431 fn test_deserialize_ignored_any_returns_error() {
1432 use serde::de::Deserializer;
1433 use serde::de::IgnoredAny;
1434 let mut de = SimpleDeserializer::new(&[]);
1435 let result = de.deserialize_ignored_any(serde::de::IgnoredAny);
1436 assert!(result.is_err());
1437 let _ = result; let mut de2 = SimpleDeserializer::new(&[]);
1440 assert!(de2.deserialize_ignored_any(IgnoredAny).is_err());
1441 }
1442
1443 #[test]
1444 fn test_invalid_option_tag() {
1445 let bad_bytes = vec![2u8];
1447 let result: std::result::Result<Option<u8>, _> = from_bytes(&bad_bytes);
1448 assert!(result.is_err());
1449 }
1450
1451 #[test]
1452 fn test_invalid_char_code_point() {
1453 let bad_char_bytes = 0xD800u32.to_be_bytes();
1455 let result: std::result::Result<char, _> = from_bytes(&bad_char_bytes);
1456 assert!(result.is_err());
1457 }
1458
1459 #[test]
1460 fn test_serialize_seq_with_none_length_is_error() {
1461 let mut ser = SimpleSerializer::new();
1465 use serde::Serializer;
1466 let result = ser.serialize_seq(None);
1467 assert!(result.is_err());
1468 }
1469
1470 #[test]
1471 fn test_serialize_map_with_none_length_is_error() {
1472 let mut ser = SimpleSerializer::new();
1473 use serde::Serializer;
1474 let result = ser.serialize_map(None);
1475 assert!(result.is_err());
1476 }
1477
1478 #[test]
1481 fn test_i32_encoding_big_endian() {
1482 let bytes = to_bytes(&-1i32).unwrap();
1483 assert_eq!(bytes, vec![0xFF, 0xFF, 0xFF, 0xFF]);
1484 }
1485
1486 #[test]
1487 fn test_i64_encoding_big_endian() {
1488 let bytes = to_bytes(&1i64).unwrap();
1489 assert_eq!(bytes, vec![0, 0, 0, 0, 0, 0, 0, 1]);
1490 }
1491
1492 #[test]
1493 fn test_u128_encoding_length() {
1494 let bytes = to_bytes(&u128::MAX).unwrap();
1495 assert_eq!(bytes.len(), 16);
1496 assert!(bytes.iter().all(|&b| b == 0xFF));
1497 }
1498
1499 #[test]
1500 fn test_i128_encoding_big_endian() {
1501 let bytes = to_bytes(&0i128).unwrap();
1502 assert_eq!(bytes.len(), 16);
1503 assert!(bytes.iter().all(|&b| b == 0));
1504 }
1505
1506 #[test]
1509 fn test_char_encoding_format() {
1510 let bytes = to_bytes(&'A').unwrap();
1511 assert_eq!(bytes, vec![0, 0, 0, 0x41]); }
1513
1514 #[test]
1515 fn test_char_null_byte() {
1516 round_trip(&'\0');
1517 let bytes = to_bytes(&'\0').unwrap();
1518 assert_eq!(bytes, vec![0, 0, 0, 0]);
1519 }
1520
1521 #[test]
1525 fn test_serial_primitive_bindings_all_types() {
1526 round_trip(&"abc".to_string());
1527 round_trip(&true);
1528 round_trip(&false);
1529 round_trip(&42u8);
1530 round_trip(&123i16);
1531 round_trip(&123i32);
1532 round_trip(&123i64);
1533 round_trip(&(b'a' as u32)); let f32_bytes = to_bytes(&123.123f32).unwrap();
1536 let f32_got: f32 = from_bytes(&f32_bytes).unwrap();
1537 assert!((f32_got - 123.123f32).abs() < 1e-3);
1538 let f64_bytes = to_bytes(&123.123f64).unwrap();
1539 let f64_got: f64 = from_bytes(&f64_bytes).unwrap();
1540 assert!((f64_got - 123.123f64).abs() < 1e-9);
1541 }
1542
1543 #[test]
1545 fn test_serial_null_objects() {
1546 let none_u32: Option<u32> = None;
1547 let bytes = to_bytes(&none_u32).unwrap();
1548 assert!(!bytes.is_empty());
1549 let got: Option<u32> = from_bytes(&bytes).unwrap();
1550 assert_eq!(got, None);
1551
1552 let none_str: Option<String> = None;
1553 round_trip(&none_str);
1554 }
1555
1556 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1558 struct SerialRecord {
1559 id: u64,
1560 name: String,
1561 value: f64,
1562 tags: Vec<String>,
1563 parent: Option<Box<SerialSimple>>,
1564 }
1565 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1566 struct SerialSimple {
1567 x: i32,
1568 label: String,
1569 }
1570
1571 #[test]
1572 fn test_serial_complex_object_round_trip() {
1573 let record = SerialRecord {
1574 id: 99999,
1575 name: "test record".to_string(),
1576 value: std::f64::consts::PI,
1577 tags: vec![
1578 "alpha".to_string(),
1579 "beta".to_string(),
1580 "gamma".to_string(),
1581 ],
1582 parent: Some(Box::new(SerialSimple {
1583 x: -42,
1584 label: "parent".to_string(),
1585 })),
1586 };
1587 round_trip(&record);
1588 }
1589
1590 #[test]
1591 fn test_serial_complex_object_none_parent() {
1592 let record = SerialRecord {
1593 id: 0,
1594 name: String::new(),
1595 value: 0.0,
1596 tags: vec![],
1597 parent: None,
1598 };
1599 round_trip(&record);
1600 }
1601
1602 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1604 struct Level3 {
1605 z: u8,
1606 }
1607 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1608 struct Level2 {
1609 inner: Level3,
1610 s: String,
1611 }
1612 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1613 struct Level1 {
1614 mid: Level2,
1615 count: u32,
1616 }
1617
1618 #[test]
1619 fn test_serial_deeply_nested_struct() {
1620 let v = Level1 {
1621 mid: Level2 { inner: Level3 { z: 255 }, s: "deep".to_string() },
1622 count: 1_000_000,
1623 };
1624 round_trip(&v);
1625 }
1626
1627 #[test]
1629 fn test_serial_non_zero_sizes() {
1630 assert!(!to_bytes(&true).unwrap().is_empty());
1631 assert!(!to_bytes(&42u8).unwrap().is_empty());
1632 assert!(!to_bytes(&123i16).unwrap().is_empty());
1633 assert!(!to_bytes(&123i32).unwrap().is_empty());
1634 assert!(!to_bytes(&123i64).unwrap().is_empty());
1635 assert!(!to_bytes(&std::f32::consts::PI).unwrap().is_empty());
1636 assert!(!to_bytes(&std::f64::consts::PI).unwrap().is_empty());
1637 assert!(!to_bytes(&"hello".to_string()).unwrap().is_empty());
1638 }
1639
1640 #[test]
1643 fn test_deeply_nested_option() {
1644 let v: Option<Option<Option<u32>>> = Some(Some(Some(7)));
1645 round_trip(&v);
1646 let none3: Option<Option<Option<u32>>> = None;
1647 round_trip(&none3);
1648 }
1649
1650 #[test]
1651 fn test_vec_of_options() {
1652 let v: Vec<Option<u64>> = vec![Some(1), None, Some(3)];
1653 round_trip(&v);
1654 }
1655
1656 #[test]
1657 fn test_tuple_struct_round_trip() {
1658 round_trip(&TupleStruct(999, "tuple".to_string(), true));
1660 }
1661
1662 #[test]
1663 fn test_newtype_struct_round_trip() {
1664 round_trip(&NewtypeStruct(u64::MAX));
1665 round_trip(&NewtypeStruct(0));
1666 }
1667
1668 #[test]
1671 fn test_ser_error_display() {
1672 use serde::ser::Error as _;
1673 let e = SerError::custom("test error message");
1674 let s = format!("{}", e);
1675 assert!(s.contains("test error message"));
1676 }
1677
1678 #[test]
1679 fn test_ser_error_from_bind_error() {
1680 let bind_err = BindError::InvalidData("oops".to_string());
1681 let ser_err = SerError::from(bind_err);
1682 let bind_back = BindError::from(ser_err);
1683 let s = format!("{}", bind_back);
1684 assert!(s.contains("oops"));
1685 }
1686
1687 #[test]
1688 fn test_de_error_custom() {
1689 use serde::de::Error as _;
1690 let e = SerError::custom("de error");
1691 let s = format!("{}", e);
1692 assert!(s.contains("de error"));
1693 }
1694
1695 #[test]
1698 fn test_deserializer_remaining_after_partial_read() {
1699 let bytes = to_bytes(&42u32).unwrap(); let mut de = SimpleDeserializer::new(&bytes);
1701 de.read_bytes(2).unwrap();
1703 assert_eq!(de.remaining().len(), 2);
1704 }
1705
1706 #[test]
1707 fn test_deserializer_remaining_empty_at_end() {
1708 let bytes = to_bytes(&1u8).unwrap(); let v: u8 = from_bytes(&bytes).unwrap();
1710 assert_eq!(v, 1);
1711 }
1713
1714 #[test]
1717 fn test_unit_serialization() {
1718 let bytes = to_bytes(&()).unwrap();
1719 assert!(bytes.is_empty());
1720 let _: () = from_bytes(&bytes).unwrap();
1721 }
1722
1723 #[test]
1724 fn test_unit_struct_serialization() {
1725 round_trip(&UnitStruct);
1726 let bytes = to_bytes(&UnitStruct).unwrap();
1727 assert!(bytes.is_empty());
1728 }
1729
1730 #[derive(Debug, PartialEq, Serialize, Deserialize)]
1733 enum Wrapper {
1734 Val(u32),
1735 Pair(u16, u16),
1736 }
1737
1738 #[test]
1739 fn test_newtype_variant_round_trip() {
1740 round_trip(&Wrapper::Val(123));
1741 }
1742
1743 #[test]
1744 fn test_tuple_variant_round_trip() {
1745 round_trip(&Wrapper::Pair(10, 20));
1746 }
1747
1748 #[test]
1763 fn test_serialize_str_round_trip_at_short_lengths() {
1764 round_trip(&"hello".to_string());
1766 round_trip(&String::new());
1767 round_trip(&"a".repeat(1024));
1768 }
1769}