1use alloc::vec::Vec;
3
4use deser_core::de::{self, DeserializeDriver};
5use deser_core::ext::{Raw, RawFormat, RawFormatId, RawFormatInfo};
6use deser_core::ser::SerializeRef;
7use deser_core::{Atom, Bytes, Error};
8
9use crate::de::Deserializer;
10use crate::parser::Cursor;
11
12pub struct Cbor;
14
15pub type RawCbor<'a> = Raw<'a, Cbor>;
23
24pub(crate) static ID: RawFormatId = RawFormatId::new("cbor", false);
30
31static FORMAT: RawFormatInfo = {
33 let mut info = RawFormatInfo::new(&ID, replay, encode, fallback);
34 info.set_data(&SCANNER);
35 info
36};
37
38pub(crate) struct Scanner(pub(crate) ScanFn);
43
44type ScanFn = for<'i> fn(&mut Cursor<'i>, &mut Vec<u8>) -> Result<(), Error>;
46
47static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
49
50impl RawFormat for Cbor {
51 #[inline(always)]
52 fn info() -> &'static RawFormatInfo {
53 &FORMAT
54 }
55}
56
57fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
59 de::Deserializer::drive(&mut Deserializer::from_slice(input), driver)
60}
61
62fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
64 crate::to_vec(&value)
65}
66
67fn fallback(input: &[u8]) -> Atom<'_> {
72 match input {
73 [0xf6 | 0xf7] => Atom::Null,
74 [0xf4] => Atom::Bool(false),
75 [0xf5] => Atom::Bool(true),
76 _ => Atom::Bytes(Bytes::borrowed(input)),
77 }
78}