1// SPDX-License-Identifier: Apache-2.0
2
3//! Serde deserialization support for CBOR
4
5mod error;
6
7pub use error::Error;
8
9use alloc::{string::String, vec::Vec};
10
11use ciborium_io::Read;
12use ciborium_ll::*;
13use serde::{de, de::Deserializer as _, forward_to_deserialize_any};
14
15trait Expected<E: de::Error> {
16 fn expected(self, kind: &'static str) -> E;
17}
18
19impl<E: de::Error> Expected<E> for Header {
20 #[inline]
21 fn expected(self, kind: &'static str) -> E {
22 de::Error::invalid_type(
23 match self {
24 Header::Positive(x) => de::Unexpected::Unsigned(x),
25 Header::Negative(x) => de::Unexpected::Signed(x as i64 ^ !0),
26 Header::Bytes(..) => de::Unexpected::Other("bytes"),
27 Header::Text(..) => de::Unexpected::Other("string"),
28
29 Header::Array(..) => de::Unexpected::Seq,
30 Header::Map(..) => de::Unexpected::Map,
31
32 Header::Tag(..) => de::Unexpected::Other("tag"),
33
34 Header::Simple(simple::FALSE) => de::Unexpected::Bool(false),
35 Header::Simple(simple::TRUE) => de::Unexpected::Bool(true),
36 Header::Simple(simple::NULL) => de::Unexpected::Other("null"),
37 Header::Simple(simple::UNDEFINED) => de::Unexpected::Other("undefined"),
38 Header::Simple(..) => de::Unexpected::Other("simple"),
39
40 Header::Float(x) => de::Unexpected::Float(x),
41 Header::Break => de::Unexpected::Other("break"),
42 },
43 &kind,
44 )
45 }
46}
47
48struct Deserializer<'b, R: Read> {
49 decoder: Decoder<R>,
50 scratch: &'b mut [u8],
51 recurse: usize,
52}
53
54impl<'a, R: Read> Deserializer<'a, R>
55where
56 R::Error: core::fmt::Debug,
57{
58 #[inline]
59 fn recurse<V, F: FnOnce(&mut Self) -> Result<V, Error<R::Error>>>(
60 &mut self,
61 func: F,
62 ) -> Result<V, Error<R::Error>> {
63 if self.recurse == 0 {
64 return Err(Error::RecursionLimitExceeded);
65 }
66
67 self.recurse -= 1;
68 let result = func(self);
69 self.recurse += 1;
70 result
71 }
72
73 #[inline]
74 fn integer(&mut self, mut header: Option<Header>) -> Result<(bool, u128), Error<R::Error>> {
75 loop {
76 let header = match header.take() {
77 Some(h) => h,
78 None => self.decoder.pull()?,
79 };
80
81 let neg = match header {
82 Header::Positive(x) => return Ok((false, x.into())),
83 Header::Negative(x) => return Ok((true, x.into())),
84 Header::Tag(tag::BIGPOS) => false,
85 Header::Tag(tag::BIGNEG) => true,
86 Header::Tag(..) => continue,
87 header => return Err(header.expected("integer")),
88 };
89
90 let mut buffer = [0u8; 16];
91 let mut value = [0u8; 16];
92 let mut index = 0usize;
93
94 return match self.decoder.pull()? {
95 Header::Bytes(len) => {
96 let mut segments = self.decoder.bytes(len);
97 while let Some(mut segment) = segments.pull()? {
98 while let Some(chunk) = segment.pull(&mut buffer)? {
99 for b in chunk {
100 match index {
101 16 => return Err(de::Error::custom("bigint too large")),
102 0 if *b == 0 => continue, // Skip leading zeros
103 _ => value[index] = *b,
104 }
105
106 index += 1;
107 }
108 }
109 }
110
111 value[..index].reverse();
112 Ok((neg, u128::from_le_bytes(value)))
113 }
114
115 h => Err(h.expected("bytes")),
116 };
117 }
118 }
119}
120
121impl<'de, 'a, 'b, R: Read> de::Deserializer<'de> for &'a mut Deserializer<'b, R>
122where
123 R::Error: core::fmt::Debug,
124{
125 type Error = Error<R::Error>;
126
127 #[inline]
128 fn deserialize_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
129 let header = self.decoder.pull()?;
130 self.decoder.push(header);
131
132 match header {
133 Header::Positive(..) => self.deserialize_u64(visitor),
134 Header::Negative(x) => match i64::try_from(x) {
135 Ok(..) => self.deserialize_i64(visitor),
136 Err(..) => self.deserialize_i128(visitor),
137 },
138
139 Header::Bytes(len) => match len {
140 Some(len) if len <= self.scratch.len() => self.deserialize_bytes(visitor),
141 _ => self.deserialize_byte_buf(visitor),
142 },
143
144 Header::Text(len) => match len {
145 Some(len) if len <= self.scratch.len() => self.deserialize_str(visitor),
146 _ => self.deserialize_string(visitor),
147 },
148
149 Header::Array(..) => self.deserialize_seq(visitor),
150 Header::Map(..) => self.deserialize_map(visitor),
151
152 Header::Tag(tag) => {
153 let _: Header = self.decoder.pull()?;
154
155 // Peek at the next item.
156 let header = self.decoder.pull()?;
157 self.decoder.push(header);
158
159 // If it is bytes, capture the length.
160 let len = match header {
161 Header::Bytes(x) => x,
162 _ => None,
163 };
164
165 match (tag, len) {
166 (tag::BIGPOS, Some(len)) | (tag::BIGNEG, Some(len)) if len <= 16 => {
167 let result = match self.integer(Some(Header::Tag(tag)))? {
168 (false, raw) => return visitor.visit_u128(raw),
169 (true, raw) => i128::try_from(raw).map(|x| x ^ !0),
170 };
171
172 match result {
173 Ok(x) => visitor.visit_i128(x),
174 Err(..) => Err(de::Error::custom("integer too large")),
175 }
176 }
177
178 _ => self.recurse(|me| {
179 let access = crate::tag::TagAccess::new(me, Some(tag));
180 visitor.visit_enum(access)
181 }),
182 }
183 }
184
185 Header::Float(..) => self.deserialize_f64(visitor),
186
187 Header::Simple(simple::FALSE) => self.deserialize_bool(visitor),
188 Header::Simple(simple::TRUE) => self.deserialize_bool(visitor),
189 Header::Simple(simple::NULL) => self.deserialize_option(visitor),
190 Header::Simple(simple::UNDEFINED) => self.deserialize_option(visitor),
191 h @ Header::Simple(..) => Err(h.expected("known simple value")),
192
193 h @ Header::Break => Err(h.expected("non-break")),
194 }
195 }
196
197 #[inline]
198 fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
199 loop {
200 let offset = self.decoder.offset();
201
202 return match self.decoder.pull()? {
203 Header::Tag(..) => continue,
204 Header::Simple(simple::FALSE) => visitor.visit_bool(false),
205 Header::Simple(simple::TRUE) => visitor.visit_bool(true),
206 _ => Err(Error::semantic(offset, "expected bool")),
207 };
208 }
209 }
210
211 #[inline]
212 fn deserialize_f32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
213 self.deserialize_f64(visitor)
214 }
215
216 #[inline]
217 fn deserialize_f64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
218 loop {
219 return match self.decoder.pull()? {
220 Header::Tag(..) => continue,
221 Header::Float(x) => visitor.visit_f64(x),
222 h => Err(h.expected("float")),
223 };
224 }
225 }
226
227 fn deserialize_i8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
228 self.deserialize_i64(visitor)
229 }
230
231 fn deserialize_i16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
232 self.deserialize_i64(visitor)
233 }
234
235 fn deserialize_i32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
236 self.deserialize_i64(visitor)
237 }
238
239 fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
240 let result = match self.integer(None)? {
241 (false, raw) => i64::try_from(raw),
242 (true, raw) => i64::try_from(raw).map(|x| x ^ !0),
243 };
244
245 match result {
246 Ok(x) => visitor.visit_i64(x),
247 Err(..) => Err(de::Error::custom("integer too large")),
248 }
249 }
250
251 fn deserialize_i128<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
252 let result = match self.integer(None)? {
253 (false, raw) => i128::try_from(raw),
254 (true, raw) => i128::try_from(raw).map(|x| x ^ !0),
255 };
256
257 match result {
258 Ok(x) => visitor.visit_i128(x),
259 Err(..) => Err(de::Error::custom("integer too large")),
260 }
261 }
262
263 fn deserialize_u8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
264 self.deserialize_u64(visitor)
265 }
266
267 fn deserialize_u16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
268 self.deserialize_u64(visitor)
269 }
270
271 fn deserialize_u32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
272 self.deserialize_u64(visitor)
273 }
274
275 fn deserialize_u64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
276 let result = match self.integer(None)? {
277 (false, raw) => u64::try_from(raw),
278 (true, ..) => return Err(de::Error::custom("unexpected negative integer")),
279 };
280
281 match result {
282 Ok(x) => visitor.visit_u64(x),
283 Err(..) => Err(de::Error::custom("integer too large")),
284 }
285 }
286
287 fn deserialize_u128<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
288 match self.integer(None)? {
289 (false, raw) => visitor.visit_u128(raw),
290 (true, ..) => Err(de::Error::custom("unexpected negative integer")),
291 }
292 }
293
294 fn deserialize_char<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
295 loop {
296 let offset = self.decoder.offset();
297 let header = self.decoder.pull()?;
298
299 return match header {
300 Header::Tag(..) => continue,
301
302 Header::Text(Some(len)) if len <= 4 => {
303 let mut buf = [0u8; 4];
304 self.decoder.read_exact(&mut buf[..len])?;
305
306 match core::str::from_utf8(&buf[..len]) {
307 Ok(s) => match s.chars().count() {
308 1 => visitor.visit_char(s.chars().next().unwrap()),
309 _ => Err(header.expected("char")),
310 },
311 Err(..) => Err(Error::Syntax(offset)),
312 }
313 }
314
315 _ => Err(header.expected("char")),
316 };
317 }
318 }
319
320 fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
321 loop {
322 let offset = self.decoder.offset();
323
324 return match self.decoder.pull()? {
325 Header::Tag(..) => continue,
326
327 Header::Text(Some(len)) if len <= self.scratch.len() => {
328 self.decoder.read_exact(&mut self.scratch[..len])?;
329
330 match core::str::from_utf8(&self.scratch[..len]) {
331 Ok(s) => visitor.visit_str(s),
332 Err(..) => Err(Error::Syntax(offset)),
333 }
334 }
335
336 header => Err(header.expected("str")),
337 };
338 }
339 }
340
341 fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
342 loop {
343 return match self.decoder.pull()? {
344 Header::Tag(..) => continue,
345
346 Header::Text(len) => {
347 let mut buffer = String::new();
348
349 let mut segments = self.decoder.text(len);
350 while let Some(mut segment) = segments.pull()? {
351 while let Some(chunk) = segment.pull(self.scratch)? {
352 buffer.push_str(chunk);
353 }
354 }
355
356 visitor.visit_string(buffer)
357 }
358
359 header => Err(header.expected("string")),
360 };
361 }
362 }
363
364 fn deserialize_bytes<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
365 loop {
366 return match self.decoder.pull()? {
367 Header::Tag(..) => continue,
368
369 Header::Bytes(Some(len)) if len <= self.scratch.len() => {
370 self.decoder.read_exact(&mut self.scratch[..len])?;
371 visitor.visit_bytes(&self.scratch[..len])
372 }
373
374 Header::Array(len) => self.recurse(|me| {
375 let access = Access(me, len);
376 visitor.visit_seq(access)
377 }),
378
379 header => Err(header.expected("bytes")),
380 };
381 }
382 }
383
384 fn deserialize_byte_buf<V: de::Visitor<'de>>(
385 self,
386 visitor: V,
387 ) -> Result<V::Value, Self::Error> {
388 loop {
389 return match self.decoder.pull()? {
390 Header::Tag(..) => continue,
391
392 Header::Bytes(len) => {
393 let mut buffer = Vec::new();
394
395 let mut segments = self.decoder.bytes(len);
396 while let Some(mut segment) = segments.pull()? {
397 while let Some(chunk) = segment.pull(self.scratch)? {
398 buffer.extend_from_slice(chunk);
399 }
400 }
401
402 visitor.visit_byte_buf(buffer)
403 }
404
405 Header::Array(len) => self.recurse(|me| {
406 let access = Access(me, len);
407 visitor.visit_seq(access)
408 }),
409
410 header => Err(header.expected("byte buffer")),
411 };
412 }
413 }
414
415 fn deserialize_seq<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
416 loop {
417 return match self.decoder.pull()? {
418 Header::Tag(..) => continue,
419
420 Header::Array(len) => self.recurse(|me| {
421 let access = Access(me, len);
422 visitor.visit_seq(access)
423 }),
424
425 Header::Bytes(len) => {
426 let mut buffer = Vec::new();
427
428 let mut segments = self.decoder.bytes(len);
429 while let Some(mut segment) = segments.pull()? {
430 while let Some(chunk) = segment.pull(self.scratch)? {
431 buffer.extend_from_slice(chunk);
432 }
433 }
434
435 visitor.visit_seq(BytesAccess::<R>(0, buffer, core::marker::PhantomData))
436 }
437
438 header => Err(header.expected("array")),
439 };
440 }
441 }
442
443 fn deserialize_map<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
444 loop {
445 return match self.decoder.pull()? {
446 Header::Tag(..) => continue,
447
448 Header::Map(len) => self.recurse(|me| {
449 let access = Access(me, len);
450 visitor.visit_map(access)
451 }),
452
453 header => Err(header.expected("map")),
454 };
455 }
456 }
457
458 fn deserialize_struct<V: de::Visitor<'de>>(
459 self,
460 _name: &'static str,
461 _fields: &'static [&'static str],
462 visitor: V,
463 ) -> Result<V::Value, Self::Error> {
464 self.deserialize_map(visitor)
465 }
466
467 fn deserialize_tuple<V: de::Visitor<'de>>(
468 self,
469 _len: usize,
470 visitor: V,
471 ) -> Result<V::Value, Self::Error> {
472 self.deserialize_seq(visitor)
473 }
474
475 fn deserialize_tuple_struct<V: de::Visitor<'de>>(
476 self,
477 _name: &'static str,
478 _len: usize,
479 visitor: V,
480 ) -> Result<V::Value, Self::Error> {
481 self.deserialize_seq(visitor)
482 }
483
484 fn deserialize_identifier<V: de::Visitor<'de>>(
485 self,
486 visitor: V,
487 ) -> Result<V::Value, Self::Error> {
488 loop {
489 let offset = self.decoder.offset();
490
491 return match self.decoder.pull()? {
492 Header::Tag(..) => continue,
493
494 Header::Text(Some(len)) if len <= self.scratch.len() => {
495 self.decoder.read_exact(&mut self.scratch[..len])?;
496
497 match core::str::from_utf8(&self.scratch[..len]) {
498 Ok(s) => visitor.visit_str(s),
499 Err(..) => Err(Error::Syntax(offset)),
500 }
501 }
502 Header::Bytes(Some(len)) if len <= self.scratch.len() => {
503 self.decoder.read_exact(&mut self.scratch[..len])?;
504 visitor.visit_bytes(&self.scratch[..len])
505 }
506
507 header => Err(header.expected("str or bytes")),
508 };
509 }
510 }
511
512 fn deserialize_ignored_any<V: de::Visitor<'de>>(
513 self,
514 visitor: V,
515 ) -> Result<V::Value, Self::Error> {
516 self.deserialize_any(visitor)
517 }
518
519 #[inline]
520 fn deserialize_option<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
521 match self.decoder.pull()? {
522 Header::Simple(simple::UNDEFINED) => visitor.visit_none(),
523 Header::Simple(simple::NULL) => visitor.visit_none(),
524 header => {
525 self.decoder.push(header);
526 visitor.visit_some(self)
527 }
528 }
529 }
530
531 #[inline]
532 fn deserialize_unit<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
533 loop {
534 return match self.decoder.pull()? {
535 Header::Simple(simple::UNDEFINED) => visitor.visit_unit(),
536 Header::Simple(simple::NULL) => visitor.visit_unit(),
537 Header::Tag(..) => continue,
538 header => Err(header.expected("unit")),
539 };
540 }
541 }
542
543 #[inline]
544 fn deserialize_unit_struct<V: de::Visitor<'de>>(
545 self,
546 _name: &'static str,
547 visitor: V,
548 ) -> Result<V::Value, Self::Error> {
549 self.deserialize_unit(visitor)
550 }
551
552 #[inline]
553 fn deserialize_newtype_struct<V: de::Visitor<'de>>(
554 self,
555 _name: &'static str,
556 visitor: V,
557 ) -> Result<V::Value, Self::Error> {
558 visitor.visit_newtype_struct(self)
559 }
560
561 #[inline]
562 fn deserialize_enum<V: de::Visitor<'de>>(
563 self,
564 name: &'static str,
565 _variants: &'static [&'static str],
566 visitor: V,
567 ) -> Result<V::Value, Self::Error> {
568 if name == "@@TAG@@" {
569 let tag = match self.decoder.pull()? {
570 Header::Tag(x) => Some(x),
571 header => {
572 self.decoder.push(header);
573 None
574 }
575 };
576
577 return self.recurse(|me| {
578 let access = crate::tag::TagAccess::new(me, tag);
579 visitor.visit_enum(access)
580 });
581 }
582
583 loop {
584 match self.decoder.pull()? {
585 Header::Tag(..) => continue,
586 Header::Map(Some(1)) => (),
587 header @ Header::Text(..) => self.decoder.push(header),
588 header => return Err(header.expected("enum")),
589 }
590
591 return self.recurse(|me| {
592 let access = Access(me, Some(0));
593 visitor.visit_enum(access)
594 });
595 }
596 }
597
598 #[inline]
599 fn is_human_readable(&self) -> bool {
600 false
601 }
602}
603
604struct Access<'a, 'b, R: Read>(&'a mut Deserializer<'b, R>, Option<usize>);
605
606impl<'de, 'a, 'b, R: Read> de::SeqAccess<'de> for Access<'a, 'b, R>
607where
608 R::Error: core::fmt::Debug,
609{
610 type Error = Error<R::Error>;
611
612 #[inline]
613 fn next_element_seed<U: de::DeserializeSeed<'de>>(
614 &mut self,
615 seed: U,
616 ) -> Result<Option<U::Value>, Self::Error> {
617 match self.1 {
618 Some(0) => return Ok(None),
619 Some(x) => self.1 = Some(x - 1),
620 None => match self.0.decoder.pull()? {
621 Header::Break => return Ok(None),
622 header => self.0.decoder.push(header),
623 },
624 }
625
626 seed.deserialize(&mut *self.0).map(Some)
627 }
628
629 #[inline]
630 fn size_hint(&self) -> Option<usize> {
631 self.1
632 }
633}
634
635impl<'de, 'a, 'b, R: Read> de::MapAccess<'de> for Access<'a, 'b, R>
636where
637 R::Error: core::fmt::Debug,
638{
639 type Error = Error<R::Error>;
640
641 #[inline]
642 fn next_key_seed<K: de::DeserializeSeed<'de>>(
643 &mut self,
644 seed: K,
645 ) -> Result<Option<K::Value>, Self::Error> {
646 match self.1 {
647 Some(0) => return Ok(None),
648 Some(x) => self.1 = Some(x - 1),
649 None => match self.0.decoder.pull()? {
650 Header::Break => return Ok(None),
651 header => self.0.decoder.push(header),
652 },
653 }
654
655 seed.deserialize(&mut *self.0).map(Some)
656 }
657
658 #[inline]
659 fn next_value_seed<V: de::DeserializeSeed<'de>>(
660 &mut self,
661 seed: V,
662 ) -> Result<V::Value, Self::Error> {
663 seed.deserialize(&mut *self.0)
664 }
665
666 #[inline]
667 fn size_hint(&self) -> Option<usize> {
668 self.1
669 }
670}
671
672impl<'de, 'a, 'b, R: Read> de::EnumAccess<'de> for Access<'a, 'b, R>
673where
674 R::Error: core::fmt::Debug,
675{
676 type Error = Error<R::Error>;
677 type Variant = Self;
678
679 #[inline]
680 fn variant_seed<V: de::DeserializeSeed<'de>>(
681 self,
682 seed: V,
683 ) -> Result<(V::Value, Self::Variant), Self::Error> {
684 let variant = seed.deserialize(&mut *self.0)?;
685 Ok((variant, self))
686 }
687}
688
689impl<'de, 'a, 'b, R: Read> de::VariantAccess<'de> for Access<'a, 'b, R>
690where
691 R::Error: core::fmt::Debug,
692{
693 type Error = Error<R::Error>;
694
695 #[inline]
696 fn unit_variant(self) -> Result<(), Self::Error> {
697 Ok(())
698 }
699
700 #[inline]
701 fn newtype_variant_seed<U: de::DeserializeSeed<'de>>(
702 self,
703 seed: U,
704 ) -> Result<U::Value, Self::Error> {
705 seed.deserialize(&mut *self.0)
706 }
707
708 #[inline]
709 fn tuple_variant<V: de::Visitor<'de>>(
710 self,
711 _len: usize,
712 visitor: V,
713 ) -> Result<V::Value, Self::Error> {
714 self.0.deserialize_any(visitor)
715 }
716
717 #[inline]
718 fn struct_variant<V: de::Visitor<'de>>(
719 self,
720 _fields: &'static [&'static str],
721 visitor: V,
722 ) -> Result<V::Value, Self::Error> {
723 self.0.deserialize_any(visitor)
724 }
725}
726
727struct BytesAccess<R: Read>(usize, Vec<u8>, core::marker::PhantomData<R>);
728
729impl<'de, R: Read> de::SeqAccess<'de> for BytesAccess<R>
730where
731 R::Error: core::fmt::Debug,
732{
733 type Error = Error<R::Error>;
734
735 #[inline]
736 fn next_element_seed<U: de::DeserializeSeed<'de>>(
737 &mut self,
738 seed: U,
739 ) -> Result<Option<U::Value>, Self::Error> {
740 use de::IntoDeserializer;
741
742 if self.0 < self.1.len() {
743 let byte = self.1[self.0];
744 self.0 += 1;
745 seed.deserialize(byte.into_deserializer()).map(Some)
746 } else {
747 Ok(None)
748 }
749 }
750
751 #[inline]
752 fn size_hint(&self) -> Option<usize> {
753 Some(self.1.len() - self.0)
754 }
755}
756
757struct TagAccess<'a, 'b, R: Read>(&'a mut Deserializer<'b, R>, usize);
758
759impl<'de, 'a, 'b, R: Read> de::Deserializer<'de> for &mut TagAccess<'a, 'b, R>
760where
761 R::Error: core::fmt::Debug,
762{
763 type Error = Error<R::Error>;
764
765 #[inline]
766 fn deserialize_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
767 let offset = self.0.decoder.offset();
768
769 match self.0.decoder.pull()? {
770 Header::Tag(x) => visitor.visit_u64(x),
771 _ => Err(Error::semantic(offset, "expected tag")),
772 }
773 }
774
775 forward_to_deserialize_any! {
776 i8 i16 i32 i64 i128
777 u8 u16 u32 u64 u128
778 bool f32 f64
779 char str string
780 bytes byte_buf
781 seq map
782 struct tuple tuple_struct
783 identifier ignored_any
784 option unit unit_struct newtype_struct enum
785 }
786}
787
788impl<'de, 'a, 'b, R: Read> de::SeqAccess<'de> for TagAccess<'a, 'b, R>
789where
790 R::Error: core::fmt::Debug,
791{
792 type Error = Error<R::Error>;
793
794 #[inline]
795 fn next_element_seed<U: de::DeserializeSeed<'de>>(
796 &mut self,
797 seed: U,
798 ) -> Result<Option<U::Value>, Self::Error> {
799 self.1 += 1;
800
801 match self.1 {
802 1 => seed.deserialize(self).map(Some),
803 2 => seed.deserialize(&mut *self.0).map(Some),
804 _ => Ok(None),
805 }
806 }
807
808 #[inline]
809 fn size_hint(&self) -> Option<usize> {
810 Some(match self.1 {
811 0 => 2,
812 1 => 1,
813 _ => 0,
814 })
815 }
816}
817
818/// Deserializes as CBOR from a type with [`impl ciborium_io::Read`](ciborium_io::Read)
819#[inline]
820pub fn from_reader<T: de::DeserializeOwned, R: Read>(reader: R) -> Result<T, Error<R::Error>>
821where
822 R::Error: core::fmt::Debug,
823{
824 let mut scratch = [0; 4096];
825
826 let mut reader = Deserializer {
827 decoder: reader.into(),
828 scratch: &mut scratch,
829 recurse: 256,
830 };
831
832 T::deserialize(&mut reader)
833}
834