musli_wire/
de.rs

1use core::fmt;
2use core::mem::take;
3
4#[cfg(feature = "alloc")]
5use alloc::vec::Vec;
6
7use musli::de::{
8    Decode, DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder,
9    SizeHint, Skip, ValueVisitor, VariantDecoder,
10};
11use musli::hint::{MapHint, SequenceHint};
12use musli::Context;
13use musli_storage::de::StorageDecoder;
14use musli_utils::int::continuation as c;
15use musli_utils::reader::Limit;
16use musli_utils::{Options, Reader};
17
18use crate::tag::{Kind, Tag};
19
20/// A very simple decoder.
21pub struct WireDecoder<'a, R, const OPT: Options, C: ?Sized> {
22    cx: &'a C,
23    reader: R,
24}
25
26impl<'a, 'de, R, const OPT: Options, C> WireDecoder<'a, R, OPT, C>
27where
28    R: Reader<'de>,
29    C: ?Sized + Context,
30{
31    /// Construct a new fixed width message encoder.
32    #[inline]
33    pub(crate) fn new(cx: &'a C, reader: R) -> Self {
34        Self { cx, reader }
35    }
36}
37
38impl<'a, 'de, R, const OPT: Options, C> WireDecoder<'a, Limit<R>, OPT, C>
39where
40    C: ?Sized + Context,
41    R: Reader<'de>,
42{
43    #[inline]
44    fn end(mut self) -> Result<(), C::Error> {
45        if self.reader.remaining() > 0 {
46            self.reader.skip(self.cx, self.reader.remaining())?;
47        }
48
49        Ok(())
50    }
51}
52
53impl<'a, 'de, R, const OPT: Options, C> WireDecoder<'a, R, OPT, C>
54where
55    R: Reader<'de>,
56    C: ?Sized + Context,
57{
58    /// Skip over any sequences of values.
59    pub(crate) fn skip_any(mut self) -> Result<(), C::Error> {
60        let mut remaining = 1;
61
62        while remaining > 0 {
63            remaining -= 1;
64
65            let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
66
67            match tag.kind() {
68                Kind::Prefix => {
69                    let len = if let Some(len) = tag.data() {
70                        len as usize
71                    } else {
72                        musli_utils::int::decode_usize::<_, _, OPT>(
73                            self.cx,
74                            self.reader.borrow_mut(),
75                        )?
76                    };
77
78                    self.reader.skip(self.cx, len)?;
79                }
80                Kind::Sequence => {
81                    let len = if let Some(len) = tag.data() {
82                        len as usize
83                    } else {
84                        musli_utils::int::decode_usize::<_, _, OPT>(
85                            self.cx,
86                            self.reader.borrow_mut(),
87                        )?
88                    };
89
90                    remaining += len;
91                }
92                Kind::Continuation => {
93                    if tag.data().is_none() {
94                        let _ = c::decode::<_, _, u128>(self.cx, self.reader.borrow_mut())?;
95                    }
96                }
97                kind => {
98                    return Err(self
99                        .cx
100                        .message(format_args!("Cannot skip over kind {kind:?}")));
101                }
102            }
103        }
104
105        Ok(())
106    }
107
108    #[inline]
109    fn decode_sequence_len(&mut self) -> Result<usize, C::Error> {
110        let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
111
112        match tag.kind() {
113            Kind::Sequence => Ok(if let Some(len) = tag.data() {
114                len as usize
115            } else {
116                musli_utils::int::decode_usize::<_, _, OPT>(self.cx, self.reader.borrow_mut())?
117            }),
118            _ => Err(self.cx.message(Expected {
119                expected: Kind::Sequence,
120                actual: tag,
121            })),
122        }
123    }
124
125    // Standard function for decoding a pair sequence.
126    #[inline]
127    fn shared_decode_pair_sequence(
128        mut self,
129    ) -> Result<RemainingWireDecoder<'a, R, OPT, C>, C::Error> {
130        let len = self.decode_sequence_len()?;
131        Ok(RemainingWireDecoder::new(self.cx, self.reader, len / 2))
132    }
133
134    // Standard function for decoding a pair sequence.
135    #[inline]
136    fn shared_decode_sequence(mut self) -> Result<RemainingWireDecoder<'a, R, OPT, C>, C::Error> {
137        let len = self.decode_sequence_len()?;
138        Ok(RemainingWireDecoder::new(self.cx, self.reader, len))
139    }
140
141    /// Decode the length of a prefix.
142    #[inline]
143    fn decode_len(&mut self, start: C::Mark) -> Result<usize, C::Error> {
144        let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
145
146        match tag.kind() {
147            Kind::Prefix => Ok(if let Some(len) = tag.data() {
148                len as usize
149            } else {
150                musli_utils::int::decode_usize::<_, _, OPT>(self.cx, self.reader.borrow_mut())?
151            }),
152            kind => Err(self
153                .cx
154                .marked_message(start, format_args!("Expected prefix, but got {kind:?}"))),
155        }
156    }
157}
158
159/// A length-prefixed decode wrapper.
160///
161/// This simplifies implementing decoders that do not have any special handling
162/// for length-prefixed types.
163#[doc(hidden)]
164pub struct RemainingWireDecoder<'a, R, const OPT: Options, C: ?Sized> {
165    cx: &'a C,
166    reader: R,
167    remaining: usize,
168}
169
170impl<'a, 'de, R, const OPT: Options, C> RemainingWireDecoder<'a, R, OPT, C>
171where
172    C: ?Sized + Context,
173    R: Reader<'de>,
174{
175    #[inline]
176    fn new(cx: &'a C, reader: R, remaining: usize) -> Self {
177        Self {
178            cx,
179            reader,
180            remaining,
181        }
182    }
183
184    #[inline]
185    fn skip_sequence_remaining(mut self) -> Result<(), C::Error> {
186        loop {
187            let Some(value) = SequenceDecoder::try_decode_next(&mut self)? else {
188                break;
189            };
190
191            value.skip()?;
192        }
193
194        Ok(())
195    }
196
197    #[inline]
198    fn skip_remaining_entries(mut self) -> Result<(), C::Error> {
199        loop {
200            let Some(value) = self.decode_entry_key()? else {
201                break;
202            };
203
204            value.skip()?;
205            self.decode_entry_value()?.skip()?;
206        }
207
208        Ok(())
209    }
210}
211
212#[musli::decoder]
213impl<'a, 'de, R, const OPT: Options, C> Decoder<'de> for WireDecoder<'a, R, OPT, C>
214where
215    C: ?Sized + Context,
216    R: Reader<'de>,
217{
218    type Cx = C;
219    type Error = C::Error;
220    type Mode = C::Mode;
221    type WithContext<'this, U> = WireDecoder<'this, R, OPT, U> where U: 'this + Context;
222    type DecodePack = WireDecoder<'a, Limit<R>, OPT, C>;
223    type DecodeSome = Self;
224    type DecodeSequence = RemainingWireDecoder<'a, R, OPT, C>;
225    type DecodeSequenceHint = RemainingWireDecoder<'a, R, OPT, C>;
226    type DecodeMap = RemainingWireDecoder<'a, R, OPT, C>;
227    type DecodeMapHint = RemainingWireDecoder<'a, R, OPT, C>;
228    type DecodeMapEntries = RemainingWireDecoder<'a, R, OPT, C>;
229    type DecodeVariant = Self;
230
231    #[inline]
232    fn cx(&self) -> &C {
233        self.cx
234    }
235
236    #[inline]
237    fn with_context<U>(self, cx: &U) -> Result<Self::WithContext<'_, U>, C::Error>
238    where
239        U: Context,
240    {
241        Ok(WireDecoder::new(cx, self.reader))
242    }
243
244    #[inline]
245    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246        write!(f, "type supported by the wire decoder")
247    }
248
249    #[inline]
250    fn decode<T>(self) -> Result<T, C::Error>
251    where
252        T: Decode<'de, Self::Mode>,
253    {
254        self.cx.decode(self)
255    }
256
257    #[inline]
258    fn decode_unsized<T, F, O>(self, f: F) -> Result<O, Self::Error>
259    where
260        T: ?Sized + DecodeUnsized<'de, Self::Mode>,
261        F: FnOnce(&T) -> Result<O, Self::Error>,
262    {
263        self.cx.decode_unsized(self, f)
264    }
265
266    #[inline]
267    fn skip(self) -> Result<(), C::Error> {
268        self.skip_any()
269    }
270
271    #[inline]
272    fn try_skip(self) -> Result<Skip, C::Error> {
273        self.skip()?;
274        Ok(Skip::Skipped)
275    }
276
277    #[inline]
278    fn decode_unit(self) -> Result<(), C::Error> {
279        self.skip()
280    }
281
282    #[inline]
283    fn decode_pack<F, O>(mut self, f: F) -> Result<O, C::Error>
284    where
285        F: FnOnce(&mut Self::DecodePack) -> Result<O, C::Error>,
286    {
287        let mark = self.cx.mark();
288        let len = self.decode_len(mark)?;
289        let mut decoder = WireDecoder::new(self.cx, self.reader.limit(len));
290        let output = f(&mut decoder)?;
291        decoder.end()?;
292        Ok(output)
293    }
294
295    #[inline]
296    fn decode_array<const N: usize>(mut self) -> Result<[u8; N], C::Error> {
297        let mark = self.cx.mark();
298        let len = self.decode_len(mark)?;
299
300        if len != N {
301            return Err(self.cx.marked_message(
302                mark,
303                BadLength {
304                    actual: len,
305                    expected: N,
306                },
307            ));
308        }
309
310        self.reader.read_array(self.cx)
311    }
312
313    #[inline]
314    fn decode_bytes<V>(mut self, visitor: V) -> Result<V::Ok, C::Error>
315    where
316        V: ValueVisitor<'de, C, [u8]>,
317    {
318        let mark = self.cx.mark();
319        let len = self.decode_len(mark)?;
320        self.reader.read_bytes(self.cx, len, visitor)
321    }
322
323    #[inline]
324    fn decode_string<V>(self, visitor: V) -> Result<V::Ok, C::Error>
325    where
326        V: ValueVisitor<'de, C, str>,
327    {
328        struct Visitor<V>(V);
329
330        impl<'de, C, V> ValueVisitor<'de, C, [u8]> for Visitor<V>
331        where
332            C: ?Sized + Context,
333            V: ValueVisitor<'de, C, str>,
334        {
335            type Ok = V::Ok;
336
337            #[inline]
338            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339                self.0.expecting(f)
340            }
341
342            #[cfg(feature = "alloc")]
343            #[inline]
344            fn visit_owned(self, cx: &C, bytes: Vec<u8>) -> Result<Self::Ok, C::Error> {
345                let string = crate::str::from_utf8_owned(bytes).map_err(cx.map())?;
346                self.0.visit_owned(cx, string)
347            }
348
349            #[inline]
350            fn visit_borrowed(self, cx: &C, bytes: &'de [u8]) -> Result<Self::Ok, C::Error> {
351                let string = crate::str::from_utf8(bytes).map_err(cx.map())?;
352                self.0.visit_borrowed(cx, string)
353            }
354
355            #[inline]
356            fn visit_ref(self, cx: &C, bytes: &[u8]) -> Result<Self::Ok, C::Error> {
357                let string = crate::str::from_utf8(bytes).map_err(cx.map())?;
358                self.0.visit_ref(cx, string)
359            }
360        }
361
362        self.decode_bytes(Visitor(visitor))
363    }
364
365    #[inline]
366    fn decode_bool(mut self) -> Result<bool, C::Error> {
367        const FALSE: Tag = Tag::new(Kind::Continuation, 0);
368        const TRUE: Tag = Tag::new(Kind::Continuation, 1);
369
370        let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
371
372        match tag {
373            FALSE => Ok(false),
374            TRUE => Ok(true),
375            tag => Err(self.cx.message(BadBoolean { actual: tag })),
376        }
377    }
378
379    #[inline]
380    fn decode_char(self) -> Result<char, C::Error> {
381        let cx = self.cx;
382        let num = self.decode_u32()?;
383
384        match char::from_u32(num) {
385            Some(d) => Ok(d),
386            None => Err(cx.message(BadCharacter(num))),
387        }
388    }
389
390    #[inline]
391    fn decode_u8(self) -> Result<u8, C::Error> {
392        crate::wire_int::decode_unsigned::<_, _, _, OPT>(self.cx, self.reader)
393    }
394
395    #[inline]
396    fn decode_u16(self) -> Result<u16, C::Error> {
397        crate::wire_int::decode_unsigned::<_, _, _, OPT>(self.cx, self.reader)
398    }
399
400    #[inline]
401    fn decode_u32(self) -> Result<u32, C::Error> {
402        crate::wire_int::decode_unsigned::<_, _, _, OPT>(self.cx, self.reader)
403    }
404
405    #[inline]
406    fn decode_u64(self) -> Result<u64, C::Error> {
407        crate::wire_int::decode_unsigned::<_, _, _, OPT>(self.cx, self.reader)
408    }
409
410    #[inline]
411    fn decode_u128(self) -> Result<u128, C::Error> {
412        crate::wire_int::decode_unsigned::<_, _, _, OPT>(self.cx, self.reader)
413    }
414
415    #[inline]
416    fn decode_i8(self) -> Result<i8, C::Error> {
417        Ok(self.decode_u8()? as i8)
418    }
419
420    #[inline]
421    fn decode_i16(self) -> Result<i16, C::Error> {
422        crate::wire_int::decode_signed::<_, _, _, OPT>(self.cx, self.reader)
423    }
424
425    #[inline]
426    fn decode_i32(self) -> Result<i32, C::Error> {
427        crate::wire_int::decode_signed::<_, _, _, OPT>(self.cx, self.reader)
428    }
429
430    #[inline]
431    fn decode_i64(self) -> Result<i64, C::Error> {
432        crate::wire_int::decode_signed::<_, _, _, OPT>(self.cx, self.reader)
433    }
434
435    #[inline]
436    fn decode_i128(self) -> Result<i128, C::Error> {
437        crate::wire_int::decode_signed::<_, _, _, OPT>(self.cx, self.reader)
438    }
439
440    #[inline]
441    fn decode_usize(self) -> Result<usize, C::Error> {
442        crate::wire_int::decode_length::<_, _, OPT>(self.cx, self.reader)
443    }
444
445    #[inline]
446    fn decode_isize(self) -> Result<isize, C::Error> {
447        Ok(self.decode_usize()? as isize)
448    }
449
450    /// Decode a 32-bit floating point value by reading the 32-bit in-memory
451    /// IEEE 754 encoding byte-by-byte.
452    #[inline]
453    fn decode_f32(self) -> Result<f32, C::Error> {
454        let bits = self.decode_u32()?;
455        Ok(f32::from_bits(bits))
456    }
457
458    /// Decode a 64-bit floating point value by reading the 64-bit in-memory
459    /// IEEE 754 encoding byte-by-byte.
460    #[inline]
461    fn decode_f64(self) -> Result<f64, C::Error> {
462        let bits = self.decode_u64()?;
463        Ok(f64::from_bits(bits))
464    }
465
466    #[inline]
467    fn decode_option(mut self) -> Result<Option<Self::DecodeSome>, C::Error> {
468        // Options are encoded as empty or sequences with a single element.
469        const NONE: Tag = Tag::new(Kind::Sequence, 0);
470        const SOME: Tag = Tag::new(Kind::Sequence, 1);
471
472        let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
473
474        match tag {
475            NONE => Ok(None),
476            SOME => Ok(Some(self)),
477            tag => Err(self.cx.message(ExpectedOption { tag })),
478        }
479    }
480
481    #[inline]
482    fn decode_sequence<F, O>(self, f: F) -> Result<O, <Self::Cx as Context>::Error>
483    where
484        F: FnOnce(&mut Self::DecodeSequence) -> Result<O, <Self::Cx as Context>::Error>,
485    {
486        let mut decoder = self.shared_decode_sequence()?;
487        let output = f(&mut decoder)?;
488        decoder.skip_sequence_remaining()?;
489        Ok(output)
490    }
491
492    #[inline]
493    fn decode_sequence_hint<F, O>(self, _: &SequenceHint, f: F) -> Result<O, C::Error>
494    where
495        F: FnOnce(&mut Self::DecodeSequenceHint) -> Result<O, C::Error>,
496    {
497        self.decode_sequence(f)
498    }
499
500    #[inline]
501    fn decode_map<F, O>(self, f: F) -> Result<O, C::Error>
502    where
503        F: FnOnce(&mut Self::DecodeMap) -> Result<O, C::Error>,
504    {
505        let mut decoder = self.shared_decode_pair_sequence()?;
506        let output = f(&mut decoder)?;
507        decoder.skip_remaining_entries()?;
508        Ok(output)
509    }
510
511    #[inline]
512    fn decode_map_hint<F, O>(self, _: &MapHint, f: F) -> Result<O, C::Error>
513    where
514        F: FnOnce(&mut Self::DecodeMapHint) -> Result<O, C::Error>,
515    {
516        self.decode_map(f)
517    }
518
519    #[inline]
520    fn decode_map_entries(self) -> Result<Self::DecodeMapEntries, C::Error> {
521        self.shared_decode_pair_sequence()
522    }
523
524    #[inline]
525    fn decode_variant<F, O>(mut self, f: F) -> Result<O, C::Error>
526    where
527        F: FnOnce(&mut Self::DecodeVariant) -> Result<O, C::Error>,
528    {
529        let tag = Tag::from_byte(self.reader.read_byte(self.cx)?);
530
531        if tag != Tag::new(Kind::Sequence, 2) {
532            return Err(self.cx.message(Expected {
533                expected: Kind::Sequence,
534                actual: tag,
535            }));
536        }
537
538        f(&mut self)
539    }
540}
541
542impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for WireDecoder<'a, Limit<R>, OPT, C>
543where
544    C: ?Sized + Context,
545    R: Reader<'de>,
546{
547    type Cx = C;
548    type DecodeNext<'this> = StorageDecoder<'a, <Limit<R> as Reader<'de>>::Mut<'this>, OPT, C> where Self: 'this;
549
550    #[inline]
551    fn try_decode_next(&mut self) -> Result<Option<Self::DecodeNext<'_>>, C::Error> {
552        Ok(Some(self.decode_next()?))
553    }
554
555    #[inline]
556    fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, C::Error> {
557        Ok(StorageDecoder::new(self.cx, self.reader.borrow_mut()))
558    }
559}
560
561impl<'a, 'de, R, const OPT: Options, C> SequenceDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C>
562where
563    C: ?Sized + Context,
564    R: Reader<'de>,
565{
566    type Cx = C;
567    type DecodeNext<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C> where Self: 'this;
568
569    #[inline]
570    fn size_hint(&self) -> SizeHint {
571        SizeHint::Exact(self.remaining)
572    }
573
574    #[inline]
575    fn try_decode_next(&mut self) -> Result<Option<Self::DecodeNext<'_>>, C::Error> {
576        if self.remaining == 0 {
577            return Ok(None);
578        }
579
580        self.remaining -= 1;
581        Ok(Some(WireDecoder::new(self.cx, self.reader.borrow_mut())))
582    }
583
584    #[inline]
585    fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, C::Error> {
586        if self.remaining == 0 {
587            return Err(self
588                .cx
589                .message(format_args!("No more tuple elements to decode")));
590        }
591
592        self.remaining -= 1;
593        Ok(WireDecoder::new(self.cx, self.reader.borrow_mut()))
594    }
595}
596
597impl<'a, 'de, R, const OPT: Options, C> VariantDecoder<'de> for WireDecoder<'a, R, OPT, C>
598where
599    C: ?Sized + Context,
600    R: Reader<'de>,
601{
602    type Cx = C;
603    type DecodeTag<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C> where Self: 'this;
604    type DecodeValue<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C> where Self: 'this;
605
606    #[inline]
607    fn decode_tag(&mut self) -> Result<Self::DecodeTag<'_>, C::Error> {
608        Ok(WireDecoder::new(self.cx, self.reader.borrow_mut()))
609    }
610
611    #[inline]
612    fn decode_value(&mut self) -> Result<Self::DecodeValue<'_>, C::Error> {
613        Ok(WireDecoder::new(self.cx, self.reader.borrow_mut()))
614    }
615}
616
617impl<'a, 'de, R, const OPT: Options, C> MapDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C>
618where
619    C: ?Sized + Context,
620    R: Reader<'de>,
621{
622    type Cx = C;
623    type DecodeEntry<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C>
624    where
625        Self: 'this;
626    type DecodeRemainingEntries<'this> = RemainingWireDecoder<'a, R::Mut<'this>, OPT, C> where Self: 'this;
627
628    #[inline]
629    fn size_hint(&self) -> SizeHint {
630        SizeHint::Exact(self.remaining)
631    }
632
633    #[inline]
634    fn decode_entry(&mut self) -> Result<Option<Self::DecodeEntry<'_>>, C::Error> {
635        if self.remaining == 0 {
636            return Ok(None);
637        }
638
639        self.remaining -= 1;
640        Ok(Some(WireDecoder::new(self.cx, self.reader.borrow_mut())))
641    }
642
643    #[inline]
644    fn decode_remaining_entries(&mut self) -> Result<Self::DecodeRemainingEntries<'_>, C::Error> {
645        Ok(RemainingWireDecoder::new(
646            self.cx,
647            self.reader.borrow_mut(),
648            take(&mut self.remaining),
649        ))
650    }
651}
652
653impl<'a, 'de, R, const OPT: Options, C> EntryDecoder<'de> for WireDecoder<'a, R, OPT, C>
654where
655    C: ?Sized + Context,
656    R: Reader<'de>,
657{
658    type Cx = C;
659    type DecodeKey<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C> where Self: 'this;
660    type DecodeValue = Self;
661
662    #[inline]
663    fn decode_key(&mut self) -> Result<Self::DecodeKey<'_>, C::Error> {
664        Ok(WireDecoder::new(self.cx, self.reader.borrow_mut()))
665    }
666
667    #[inline]
668    fn decode_value(self) -> Result<Self::DecodeValue, C::Error> {
669        Ok(self)
670    }
671}
672
673impl<'a, 'de, R, const OPT: Options, C> EntriesDecoder<'de> for RemainingWireDecoder<'a, R, OPT, C>
674where
675    C: ?Sized + Context,
676    R: Reader<'de>,
677{
678    type Cx = C;
679    type DecodeEntryKey<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C>
680    where
681        Self: 'this;
682    type DecodeEntryValue<'this> = WireDecoder<'a, R::Mut<'this>, OPT, C>
683    where
684        Self: 'this;
685
686    #[inline]
687    fn decode_entry_key(&mut self) -> Result<Option<Self::DecodeEntryKey<'_>>, C::Error> {
688        if self.remaining == 0 {
689            return Ok(None);
690        }
691
692        self.remaining -= 1;
693        Ok(Some(WireDecoder::new(self.cx, self.reader.borrow_mut())))
694    }
695
696    #[inline]
697    fn decode_entry_value(&mut self) -> Result<Self::DecodeEntryValue<'_>, C::Error> {
698        Ok(WireDecoder::new(self.cx, self.reader.borrow_mut()))
699    }
700
701    #[inline]
702    fn end_entries(self) -> Result<(), C::Error> {
703        self.skip_remaining_entries()?;
704        Ok(())
705    }
706}
707
708struct Expected {
709    expected: Kind,
710    actual: Tag,
711}
712
713impl fmt::Display for Expected {
714    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
715        let Self { expected, actual } = *self;
716
717        write!(f, "Expected {expected:?} but was {actual:?}")
718    }
719}
720
721struct BadBoolean {
722    actual: Tag,
723}
724
725impl fmt::Display for BadBoolean {
726    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
727        let Self { actual } = *self;
728        write!(f, "Bad boolean tag {actual:?}")
729    }
730}
731
732struct BadCharacter(u32);
733
734impl fmt::Display for BadCharacter {
735    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
736        write!(f, "Bad character number 0x{:02x}", self.0)
737    }
738}
739
740struct ExpectedOption {
741    tag: Tag,
742}
743
744impl fmt::Display for ExpectedOption {
745    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
746        let Self { tag } = *self;
747
748        write!(f, "Expected zero-to-single sequence, was {tag:?}",)
749    }
750}
751
752struct BadLength {
753    actual: usize,
754    expected: usize,
755}
756
757impl fmt::Display for BadLength {
758    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
759        let Self { actual, expected } = *self;
760
761        write!(f, "Bad length, got {actual} but expect {expected}")
762    }
763}