1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::iter::Cloned;
use core::marker::PhantomData;
use core::slice;

use crate::parser::{JsonKind, ParseConfig, ParseDelegate, Parser};
#[cfg(feature = "alloc")]
use crate::{value::Entry, Object, Value};
use crate::{Error, ErrorKind, JsonNumber, JsonString};

/// A parsed JSON payload.
///
/// This type is a read-only view into the JSON payload.
///
/// This structure is faster to parse than [`Value`], but it is more work to
/// interact with because the entire payload is stored in a single list
/// internally.
///
/// The list of [`Node`]s is sequentially ordered by the order in which they
/// appear in the document. This means that the first [`Node`] tells us what the
/// root type of the document is.
///
/// The nodes [`Node::Null`], [`Node::Boolean`], [`Node::String`], and
/// [`Node::Number`] all directly represent a [`Value`] with the same name.
///
/// [`Node::Object`] contains a length field, which lets us know how many
/// key-value pairs will appear after the object's node. Object keys are
/// guaranteed to be [`Node::String`]s, but object values can be any [`Node`]
/// variant. This means care must be taken to handle nested structures.
///
/// [`Node::Array`] also contains a length field, which lets us know how many
/// values follow the array's node. Because array values can be any type, care
/// must be taken to handle nested structures correctly.
///
/// [`DocumentIter`] has multiple methods to help efficiently deal with nested
/// data types:
///
/// - [`skip_next_value()`](DocumentIter::skip_next_value): Skips over the next
///   value, taking care to handle nested structures properly.
/// - [`next_value()`](DocumentIter::next_value): Returns a [`Value`] from the
///   iterator.
#[derive(Debug, Eq, PartialEq)]
pub struct GenericDocument<'a, Backing> {
    nodes: Nodes<'a, Backing>,
}

impl<'a, Backing> GenericDocument<'a, Backing>
where
    Backing: NodeCollection<'a>,
{
    /// Parses a JSON payload from `source`.
    ///
    /// This function verifies that `json` is valid UTF-8 while parsing the
    /// JSON.
    pub fn from_json_bytes(source: &'a [u8]) -> Result<Self, Error>
    where
        Backing: Default,
    {
        let mut nodes = Nodes::default();
        Parser::parse_json_bytes(source, &mut nodes).map_err(Error::into_infallable)?;
        Ok(Self { nodes })
    }

    /// Parses a JSON payload from `source`, with the settings from `config`.
    ///
    /// This function verifies that `json` is valid UTF-8 while parsing the
    /// JSON.
    pub fn from_json_bytes_with_config(source: &'a [u8], config: ParseConfig) -> Result<Self, Error>
    where
        Backing: Default,
    {
        let mut nodes = Nodes::default();
        Parser::parse_json_bytes_with_config(source, config, &mut nodes)
            .map_err(Error::into_infallable)?;
        Ok(Self { nodes })
    }

    /// Parses a JSON payload from `source`.
    ///
    /// Because the `str` type guarantees that `json` is valid UTF-8, no
    /// additional unicode checks are performed on unescaped unicode sequences.
    pub fn from_json(source: &'a str) -> Result<Self, Error>
    where
        Backing: Default,
    {
        let mut nodes = Nodes::default();
        Parser::parse_json(source, &mut nodes).map_err(Error::into_infallable)?;
        Ok(Self { nodes })
    }

    /// Parses a JSON payload from `source`, with the settings from `config`.
    ///
    /// Because the `str` type guarantees that `json` is valid UTF-8, no
    /// additional unicode checks are performed on unescaped unicode sequences.
    pub fn from_json_with_config(source: &'a str, config: ParseConfig) -> Result<Self, Error>
    where
        Backing: Default,
    {
        let mut nodes = Nodes::default();
        Parser::parse_json_with_config(source, config, &mut nodes)
            .map_err(Error::into_infallable)?;
        Ok(Self { nodes })
    }

    /// Returns an iterator over the nodes in this document.
    #[must_use]
    pub fn iter(&self) -> DocumentIter<'_, 'a> {
        self.into_iter()
    }
}

#[cfg(feature = "alloc")]
impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>
where
    Backing: NodeCollection<'a>,
{
    fn from(doc: GenericDocument<'a, Backing>) -> Self {
        let mut nodes = doc.nodes.collection.into_iter();
        let root = nodes.next().expect("empty document is invalid");
        hydrate_value_from_node(root, &mut nodes)
    }
}

#[cfg(feature = "alloc")]
fn hydrate_value_from_node<'a, I>(node: Node<'a>, remaining_nodes: &mut I) -> Value<'a>
where
    I: Iterator<Item = Node<'a>>,
{
    match node {
        Node::Null => Value::Null,
        Node::Boolean(value) => Value::Boolean(value),
        Node::String(value) => Value::String(value),
        Node::Number(number) => Value::Number(number),
        Node::Object { length: len } => {
            let mut obj = Object::with_capacity(len);
            for _ in 0..len {
                let node = remaining_nodes.next().expect("obbject missing value");
                let Node::String(key) = node else { unreachable!("object key must be string") };
                let node = remaining_nodes.next().expect("object missing value");
                obj.push(Entry {
                    key,
                    value: hydrate_value_from_node(node, remaining_nodes),
                });
            }
            Value::Object(obj)
        }
        Node::Array { length: len } => {
            let mut values = Vec::with_capacity(len);
            for _ in 0..len {
                let node = remaining_nodes.next().expect("array missing value");
                values.push(hydrate_value_from_node(node, remaining_nodes));
            }
            Value::Array(values)
        }
    }
}

impl<'doc, 'a, Backing> IntoIterator for &'doc GenericDocument<'a, Backing>
where
    Backing: NodeCollection<'a>,
{
    type IntoIter = DocumentIter<'doc, 'a>;
    type Item = Node<'a>;

    fn into_iter(self) -> Self::IntoIter {
        DocumentIter {
            nodes: self.nodes.collection.as_ref().iter().cloned(),
        }
    }
}

/// A single value in a [`Document`].
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Node<'a> {
    /// A null value.
    Null,
    /// A boolean value.
    Boolean(bool),
    /// A string value.
    String(JsonString<'a>),
    /// A numerical value.
    Number(JsonNumber<'a>),
    /// An object value with `len` key-value pairs following it.
    Object {
        /// The number of key-value pairs that this object contains.
        length: usize,
    },
    /// An array with `len` values following it.
    Array {
        /// The number of values that this array contains.
        length: usize,
    },
}

#[derive(Default, Debug, Eq, PartialEq)]
struct Nodes<'a, Backing> {
    collection: Backing,
    _phantom: PhantomData<&'a ()>,
}

impl<'a, Backing> Nodes<'a, Backing>
where
    Backing: NodeCollection<'a>,
{
    fn push_node(&mut self, node: Node<'a>) -> Result<usize, ErrorKind> {
        let index = self.collection.as_ref().len();
        self.collection.push(node)?;
        Ok(index)
    }

    pub fn push_null(&mut self) -> Result<usize, ErrorKind> {
        self.push_node(Node::Null)
    }

    pub fn push_bool(&mut self, boolean: bool) -> Result<usize, ErrorKind> {
        self.push_node(Node::Boolean(boolean))
    }

    pub fn push_string(&mut self, string: JsonString<'a>) -> Result<usize, ErrorKind> {
        self.push_node(Node::String(string))
    }

    pub fn push_number(&mut self, number: JsonNumber<'a>) -> Result<usize, ErrorKind> {
        self.push_node(Node::Number(number))
    }

    pub fn push_object(&mut self) -> Result<usize, ErrorKind> {
        self.push_node(Node::Object { length: 0 })
    }

    pub fn push_array(&mut self) -> Result<usize, ErrorKind> {
        self.push_node(Node::Array { length: 0 })
    }

    pub fn extend_object(&mut self, index: usize) {
        let Node::Object { length: len } = &mut self.collection.as_mut()[index] else { unreachable!("extended wrong type") };
        *len += 1;
    }

    pub fn extend_array(&mut self, index: usize) {
        let Node::Array { length: len } = &mut self.collection.as_mut()[index] else { unreachable!("extended wrong type") };
        *len += 1;
    }
}

impl<'a, 'b, Backing> ParseDelegate<'a> for &'b mut Nodes<'a, Backing>
where
    Backing: NodeCollection<'a>,
{
    type Array = usize;
    type Error = ErrorKind;
    type Key = ();
    type Object = usize;
    type Value = usize;

    #[inline]
    fn null(&mut self) -> Result<Self::Value, Self::Error> {
        self.push_null()
    }

    #[inline]
    fn boolean(&mut self, value: bool) -> Result<Self::Value, Self::Error> {
        self.push_bool(value)
    }

    #[inline]
    fn number(&mut self, value: JsonNumber<'a>) -> Result<Self::Value, Self::Error> {
        self.push_number(value)
    }

    #[inline]
    fn string(&mut self, value: JsonString<'a>) -> Result<Self::Value, Self::Error> {
        self.push_string(value)
    }

    #[inline]
    fn begin_object(&mut self) -> Result<Self::Object, Self::Error> {
        self.push_object()
    }

    #[inline]
    fn object_key(
        &mut self,
        object: &mut Self::Object,
        key: JsonString<'a>,
    ) -> Result<Self::Key, Self::Error> {
        self.extend_object(*object);
        self.push_string(key)?;
        Ok(())
    }

    #[inline]
    fn object_value(
        &mut self,
        _object: &mut Self::Object,
        _key: Self::Key,
        _value: Self::Value,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    #[inline]
    fn object_is_empty(&self, object: &Self::Object) -> bool {
        let Node::Object { length: len } = &self.collection.as_ref()[*object] else { unreachable!("invalid object") };
        *len == 0
    }

    #[inline]
    fn end_object(&mut self, object: Self::Object) -> Result<Self::Value, Self::Error> {
        Ok(object)
    }

    #[inline]
    fn begin_array(&mut self) -> Result<Self::Array, Self::Error> {
        self.push_array()
    }

    #[inline]
    fn array_value(
        &mut self,
        array: &mut Self::Array,
        _value: Self::Value,
    ) -> Result<(), Self::Error> {
        self.extend_array(*array);
        Ok(())
    }

    #[inline]
    fn array_is_empty(&self, array: &Self::Array) -> bool {
        let Node::Array { length: len } = &self.collection.as_ref()[*array] else { unreachable!("invalid array") };
        *len == 0
    }

    #[inline]
    fn end_array(&mut self, array: Self::Array) -> Result<Self::Value, Self::Error> {
        Ok(array)
    }

    #[inline]
    fn kind_of(&self, value: &Self::Value) -> JsonKind {
        match &self.collection.as_ref()[*value] {
            Node::Null => JsonKind::Null,
            Node::Boolean(_) => JsonKind::Boolean,
            Node::String(_) => JsonKind::String,
            Node::Number(_) => JsonKind::Number,
            Node::Object { .. } => JsonKind::Object,
            Node::Array { .. } => JsonKind::Array,
        }
    }
}

/// An iterator over the [`Node`]s in a [`Document`].
#[derive(Debug, Clone)]
pub struct DocumentIter<'doc, 'a> {
    nodes: Cloned<slice::Iter<'doc, Node<'a>>>,
}

impl<'doc, 'a> DocumentIter<'doc, 'a> {
    /// Reads a [`Value`] from the iterator, if any nodes remain.
    ///
    /// This function automatically reads nested objects and arrays.
    ///
    /// ```rust
    /// use justjson::doc::{Document, Node};
    /// use justjson::Value;
    ///
    /// let doc = Document::from_json(
    ///     r#"{
    ///         "a": [1, 2, 3, 4]
    ///     }"#,
    /// )
    /// .unwrap();
    ///
    /// let mut nodes = doc.iter();
    /// assert_eq!(nodes.next(), Some(Node::Object { length: 1 }));
    /// let Node::String(key) = nodes.next().unwrap() else { unreachable!() };
    /// assert_eq!(key, "a");
    /// let Value::Array(array) = nodes.next_value().unwrap() else { unreachable!() };
    /// assert_eq!(array.len(), 4);
    /// assert!(nodes.next().is_none());
    /// ```
    #[cfg(feature = "alloc")]
    pub fn next_value(&mut self) -> Option<Value<'a>> {
        let node = self.nodes.next()?;

        Some(hydrate_value_from_node(node, &mut self.nodes))
    }

    /// Skips a [`Value`], including any nested values.
    pub fn skip_next_value(&mut self) {
        if let Some(node) = self.nodes.next() {
            match node {
                Node::Null | Node::Boolean(_) | Node::String(_) | Node::Number(_) => {}
                Node::Object { length: len } => {
                    for _ in 0..len {
                        // Skip the key
                        self.skip_next_value();
                        // Skip the value
                        self.skip_next_value();
                    }
                }
                Node::Array { length: len } => {
                    for _ in 0..len {
                        // Skip the value
                        self.skip_next_value();
                    }
                }
            }
        }
    }
}

impl<'doc, 'a> Iterator for DocumentIter<'doc, 'a> {
    type Item = Node<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.nodes.next()
    }
}

#[test]
#[cfg(feature = "alloc")]
fn document_iteration() {
    let source = r#"{"a":1,"b":true,"c":"hello","d":[null],"e":{}}"#;
    let doc = Document::from_json(source).unwrap();
    assert_eq!(
        doc.iter().collect::<Vec<_>>(),
        alloc::vec![
            Node::Object { length: 5 },
            Node::String(JsonString::from_json("\"a\"").unwrap()),
            Node::Number(JsonNumber::from_json("1").unwrap()),
            Node::String(JsonString::from_json("\"b\"").unwrap()),
            Node::Boolean(true),
            Node::String(JsonString::from_json("\"c\"").unwrap()),
            Node::String(JsonString::from_json("\"hello\"").unwrap()),
            Node::String(JsonString::from_json("\"d\"").unwrap()),
            Node::Array { length: 1 },
            Node::Null,
            Node::String(JsonString::from_json("\"e\"").unwrap()),
            Node::Object { length: 0 },
        ]
    );
    let value = doc.iter().next_value().unwrap();
    assert_eq!(value, Value::from_json(source).unwrap());

    // Test skipping
    let mut iter = doc.iter();
    iter.skip_next_value();
    assert!(iter.next().is_none());
}

/// A collection for use with [`GenericDocument`].
pub trait NodeCollection<'a>:
    AsRef<[Node<'a>]> + AsMut<[Node<'a>]> + IntoIterator<Item = Node<'a>>
{
    /// Push `node` to the end of the collection.
    fn push(&mut self, node: Node<'a>) -> Result<(), ErrorKind>;
}

#[cfg(feature = "alloc")]
impl<'a> NodeCollection<'a> for Vec<Node<'a>> {
    #[inline]
    fn push(&mut self, node: Node<'a>) -> Result<(), ErrorKind> {
        self.push(node);
        Ok(())
    }
}

#[cfg(feature = "heapless")]
impl<'a, const N: usize> NodeCollection<'a> for heapless::Vec<Node<'a>, N> {
    #[inline]
    fn push(&mut self, node: Node<'a>) -> Result<(), ErrorKind> {
        self.push(node).map_err(|_| ErrorKind::PaylodTooLarge)
    }
}

/// A convenience alias for a [`GenericDocument`] that uses a `Vec` from
/// `std`/`alloc`.
#[cfg(feature = "alloc")]
pub type Document<'a> = GenericDocument<'a, Vec<Node<'a>>>;

/// A convenience alias for a [`GenericDocument`] that uses a `heapless::Vec`.
#[cfg(feature = "heapless")]
pub type HeaplessDocument<'a, const N: usize> = GenericDocument<'a, heapless::Vec<Node<'a>, N>>;