use alloc::vec::Vec;
use deser_core::de::{self, DeserializeDriver};
use deser_core::ext::{Raw, RawFormat, RawFormatId, RawFormatInfo};
use deser_core::ser::SerializeRef;
use deser_core::{Atom, Bytes, Error};
use crate::de::Deserializer;
use crate::parser::Cursor;
pub struct Cbor;
pub type RawCbor<'a> = Raw<'a, Cbor>;
pub(crate) static ID: RawFormatId = RawFormatId::new("cbor", false);
static FORMAT: RawFormatInfo = {
let mut info = RawFormatInfo::new(&ID, replay, encode, fallback);
info.set_data(&SCANNER);
info
};
pub(crate) struct Scanner(pub(crate) ScanFn);
type ScanFn = for<'i> fn(&mut Cursor<'i>, &mut Vec<u8>) -> Result<(), Error>;
static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
impl RawFormat for Cbor {
#[inline(always)]
fn info() -> &'static RawFormatInfo {
&FORMAT
}
}
fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
de::Deserializer::drive(&mut Deserializer::from_slice(input), driver)
}
fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
crate::to_vec(&value)
}
fn fallback(input: &[u8]) -> Atom<'_> {
match input {
[0xf6 | 0xf7] => Atom::Null,
[0xf4] => Atom::Bool(false),
[0xf5] => Atom::Bool(true),
_ => Atom::Bytes(Bytes::borrowed(input)),
}
}