Skip to main content

deser_json5/
raw.rs

1// @generated from deser-template-json/src/raw.rs by
2// deser-template-json/generate.py.  Do not edit.
3//! Raw values of the dialect.
4//!
5use alloc::vec::Vec;
6use core::str;
7
8use deser_core::de::{self, DeserializeDriver};
9use deser_core::ext::{Raw, RawFormat, RawFormatId, RawFormatInfo, TextRawFormat};
10use deser_core::ser::SerializeRef;
11use deser_core::{Atom, Error, State, Text};
12
13use crate::de::Deserializer;
14use crate::parser::Cursor;
15
16/// The JSON5 format of [`RawJson5`] values.
17pub struct Json5;
18
19/// JSON5 text of a value.
20///
21/// The JSON5 of values deserialized from JSON5 is kept as it is.  Other
22/// values are encoded as JSON, which is valid JSON5.  See [`Raw`] for more
23/// information.
24pub type RawJson5<'a> = Raw<'a, Json5>;
25
26pub(crate) type Dialect = Json5;
27
28const NAME: &str = "json5";
29
30/// The identity of the format of the raw values of this dialect.
31///
32/// The parser and the serializer declare the raw values they pass on with
33/// it, the description (with the functions) is only referred to by the raw
34/// values.
35pub(crate) static ID: RawFormatId = RawFormatId::new(NAME, true);
36
37/// The description of the format of the raw values of this dialect.
38static FORMAT: RawFormatInfo = {
39    let mut info = RawFormatInfo::new(&ID, replay, encode, fallback);
40    info.set_data(&SCANNER);
41    info
42};
43
44/// Skips a raw value while validating it (see `parser::raw_value`).
45///
46/// The parser gets it from the description of the format, so that only
47/// programs with raw values of the dialect contain it.
48pub(crate) struct Scanner(pub(crate) ScanFn);
49
50/// Skips a value at the cursor (see `parser::skip_raw`).
51type ScanFn =
52    for<'i> fn(&mut Cursor<'i>, &mut Vec<u8>, bool, usize, &mut State) -> Result<bool, Error>;
53
54/// The scanner of the raw values of this dialect.
55static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
56
57impl RawFormat for Dialect {
58    #[inline(always)]
59    fn info() -> &'static RawFormatInfo {
60        &FORMAT
61    }
62}
63
64// SAFETY: the parser validates strings as UTF-8, everything else is ASCII,
65// and the encoder writes JSON text.
66unsafe impl TextRawFormat for Dialect {}
67
68/// Parses a raw value.
69fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
70    // SAFETY: the input of raw values of text formats is valid UTF-8
71    let input = unsafe { str::from_utf8_unchecked(input) };
72    de::Deserializer::drive(&mut Deserializer::from_str(input), driver)
73}
74
75/// Encodes a value as JSON, which all dialects can read.
76fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
77    crate::to_string(&value).map(|text| text.into_bytes())
78}
79
80/// Returns the fallback atom of a raw value.
81///
82/// Null and booleans are their value, everything else is its text.
83fn fallback(input: &[u8]) -> Atom<'_> {
84    match input {
85        b"null" => Atom::Null,
86        b"true" => Atom::Bool(true),
87        b"false" => Atom::Bool(false),
88        // SAFETY: the input of raw values of text formats is valid UTF-8
89        _ => Atom::Str(Text::borrowed(unsafe { str::from_utf8_unchecked(input) })),
90    }
91}