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.set_check_limits(crate::parser::check_raw_limits);
45 info
46};
47
48/// Skips a raw value while validating it (see `parser::raw_value`).
49///
50/// The parser gets it from the description of the format, so that only
51/// programs with raw values of the dialect contain it.
52pub(crate) struct Scanner(pub(crate) ScanFn);
53
54/// Skips a value at the cursor (see `parser::skip_raw`).
55type ScanFn =
56 for<'i> fn(&mut Cursor<'i>, &mut Vec<u8>, bool, usize, &mut State) -> Result<bool, Error>;
57
58/// The scanner of the raw values of this dialect.
59static SCANNER: Scanner = Scanner(crate::parser::skip_raw);
60
61impl RawFormat for Dialect {
62 #[inline(always)]
63 fn info() -> &'static RawFormatInfo {
64 &FORMAT
65 }
66}
67
68// SAFETY: the parser validates strings as UTF-8, everything else is ASCII,
69// and the encoder writes JSON text.
70unsafe impl TextRawFormat for Dialect {}
71
72/// Parses a raw value.
73fn replay<'de>(input: &'de [u8], driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
74 // SAFETY: the input of raw values of text formats is valid UTF-8
75 let input = unsafe { str::from_utf8_unchecked(input) };
76 de::Deserializer::drive(&mut Deserializer::from_str(input), driver)
77}
78
79/// Encodes a value as JSON, which all dialects can read.
80fn encode(value: SerializeRef<'_>) -> Result<Vec<u8>, Error> {
81 crate::to_string(&value).map(|text| text.into_bytes())
82}
83
84/// Returns the fallback atom of a raw value.
85///
86/// Null and booleans are their value, everything else is its text.
87fn fallback(input: &[u8]) -> Atom<'_> {
88 match input {
89 b"null" => Atom::Null,
90 b"true" => Atom::Bool(true),
91 b"false" => Atom::Bool(false),
92 // SAFETY: the input of raw values of text formats is valid UTF-8
93 _ => Atom::Str(Text::borrowed(unsafe { str::from_utf8_unchecked(input) })),
94 }
95}