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