use alloc::vec::Vec;
use core::str;
use deser_core::de::{self, DeserializeDriver};
use deser_core::ext::{Raw, RawFormat, RawFormatId, RawFormatInfo, TextRawFormat};
use deser_core::ser::SerializeRef;
use deser_core::{Atom, Error, State, Text};
use crate::de::Deserializer;
use crate::parser::Cursor;
pub struct Json5;
pub type RawJson5<'a> = Raw<'a, Json5>;
pub(crate) type Dialect = Json5;
const NAME: &str = "json5";
pub(crate) static ID: RawFormatId = RawFormatId::new(NAME, true);
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>, bool, usize, &mut State) -> Result<bool, Error>;
static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
impl RawFormat for Dialect {
#[inline(always)]
fn info() -> &'static RawFormatInfo {
&FORMAT
}
}
unsafe impl TextRawFormat for Dialect {}
fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
let input = unsafe { str::from_utf8_unchecked(input) };
de::Deserializer::drive(&mut Deserializer::from_str(input), driver)
}
fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
crate::to_string(&value).map(|text| text.into_bytes())
}
fn fallback(input: &[u8]) -> Atom<'_> {
match input {
b"null" => Atom::Null,
b"true" => Atom::Bool(true),
b"false" => Atom::Bool(false),
_ => Atom::Str(Text::borrowed(unsafe { str::from_utf8_unchecked(input) })),
}
}