zparse 2.0.5

High-performance JSON/TOML/YAML/XML parser with zero-allocation support
Documentation
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! DOM types for parsed JSON/TOML/YAML/XML values

use indexmap::IndexMap;
use indexmap::map::{IntoIter, Iter, Keys, Values};
use std::ops::Index;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};

/// A JSON/TOML/YAML/XML value
#[derive(Debug, Clone, PartialEq, Default)]
pub enum Value {
    /// Null value
    #[default]
    Null,
    /// Boolean value
    Bool(bool),
    /// Numeric value (f64)
    Number(f64),
    /// String value
    String(String),
    /// Array of values
    Array(Array),
    /// Object (key-value pairs with order preservation)
    Object(Object),
    /// TOML datetime value
    Datetime(TomlDatetime),
}

/// TOML datetime values
#[derive(Debug, Clone, PartialEq)]
pub enum TomlDatetime {
    OffsetDateTime(OffsetDateTime),
    LocalDateTime(PrimitiveDateTime),
    LocalDate(Date),
    LocalTime(Time),
}

impl Value {
    /// Returns true if this value is null
    pub fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// Returns true if this value is a boolean
    pub fn is_bool(&self) -> bool {
        matches!(self, Self::Bool(_))
    }

    /// Returns true if this value is a number
    pub fn is_number(&self) -> bool {
        matches!(self, Self::Number(_))
    }

    /// Returns true if this value is a string
    pub fn is_string(&self) -> bool {
        matches!(self, Self::String(_))
    }

    /// Returns true if this value is an array
    pub fn is_array(&self) -> bool {
        matches!(self, Self::Array(_))
    }

    /// Returns true if this value is an object
    pub fn is_object(&self) -> bool {
        matches!(self, Self::Object(_))
    }

    /// Returns true if this value is a TOML datetime
    pub fn is_datetime(&self) -> bool {
        matches!(self, Self::Datetime(_))
    }

    /// Returns the boolean value if this is a boolean, None otherwise
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Returns the numeric value if this is a number, None otherwise
    pub fn as_number(&self) -> Option<f64> {
        match self {
            Self::Number(n) => Some(*n),
            _ => None,
        }
    }

    /// Returns the string value if this is a string, None otherwise
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Self::String(s) => Some(s),
            _ => None,
        }
    }

    /// Returns the array if this is an array, None otherwise
    pub fn as_array(&self) -> Option<&Array> {
        match self {
            Self::Array(a) => Some(a),
            _ => None,
        }
    }

    /// Returns the object if this is an object, None otherwise
    pub fn as_object(&self) -> Option<&Object> {
        match self {
            Self::Object(o) => Some(o),
            _ => None,
        }
    }

    /// Returns the TOML datetime if this is a datetime, None otherwise
    pub fn as_datetime(&self) -> Option<&TomlDatetime> {
        match self {
            Self::Datetime(dt) => Some(dt),
            _ => None,
        }
    }

    /// Returns a mutable reference to the array if this is an array, None otherwise
    pub fn as_array_mut(&mut self) -> Option<&mut Array> {
        match self {
            Self::Array(a) => Some(a),
            _ => None,
        }
    }

    /// Returns a mutable reference to the object if this is an object, None otherwise
    pub fn as_object_mut(&mut self) -> Option<&mut Object> {
        match self {
            Self::Object(o) => Some(o),
            _ => None,
        }
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<f64> for Value {
    fn from(value: f64) -> Self {
        Self::Number(value)
    }
}

impl From<TomlDatetime> for Value {
    fn from(value: TomlDatetime) -> Self {
        Self::Datetime(value)
    }
}

impl From<i32> for Value {
    fn from(value: i32) -> Self {
        Self::Number(f64::from(value))
    }
}

impl From<i64> for Value {
    fn from(value: i64) -> Self {
        #[allow(clippy::as_conversions)]
        // JSON numbers are represented as f64; precision loss is acceptable here.
        let number = value as f64;
        Self::Number(number)
    }
}

impl From<u32> for Value {
    fn from(value: u32) -> Self {
        Self::Number(f64::from(value))
    }
}

impl From<u64> for Value {
    fn from(value: u64) -> Self {
        #[allow(clippy::as_conversions)]
        // JSON numbers are represented as f64; precision loss is acceptable here.
        let number = value as f64;
        Self::Number(number)
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Self::String(value.to_owned())
    }
}

impl From<Array> for Value {
    fn from(value: Array) -> Self {
        Self::Array(value)
    }
}

impl From<Object> for Value {
    fn from(value: Object) -> Self {
        Self::Object(value)
    }
}

#[allow(clippy::use_self)]
impl From<Vec<Value>> for Value {
    fn from(values: Vec<Value>) -> Self {
        Self::Array(Array(values))
    }
}

#[allow(clippy::use_self)]
impl From<IndexMap<String, Value>> for Value {
    fn from(map: IndexMap<String, Value>) -> Self {
        Self::Object(Object(map))
    }
}

/// An order-preserving object (map of string keys to values)
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Object(pub(crate) IndexMap<String, Value>);

impl Object {
    /// Creates a new empty object
    pub fn new() -> Self {
        Self(IndexMap::new())
    }

    /// Creates a new object with the given capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self(IndexMap::with_capacity(capacity))
    }

    /// Returns the number of key-value pairs in the object
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the object contains no key-value pairs
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns a reference to the value corresponding to the key
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.0.get(key)
    }

    /// Returns a mutable reference to the value corresponding to the key
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        self.0.get_mut(key)
    }

    /// Inserts a key-value pair into the object
    /// Returns the previous value if the key already existed
    pub fn insert(&mut self, key: impl Into<String>, value: impl Into<Value>) -> Option<Value> {
        self.0.insert(key.into(), value.into())
    }

    /// Removes a key from the object, returning the value if the key was present
    pub fn remove(&mut self, key: &str) -> Option<Value> {
        self.0.swap_remove(key)
    }

    /// Returns true if the object contains the specified key
    pub fn contains_key(&self, key: &str) -> bool {
        self.0.contains_key(key)
    }

    /// Returns an iterator over the keys
    pub fn keys(&self) -> Keys<'_, String, Value> {
        self.0.keys()
    }

    /// Returns an iterator over the values
    pub fn values(&self) -> Values<'_, String, Value> {
        self.0.values()
    }

    /// Returns an iterator over key-value pairs
    pub fn iter(&self) -> Iter<'_, String, Value> {
        self.0.iter()
    }

    /// Returns an iterator that allows modifying each value
    pub fn iter_mut(&mut self) -> indexmap::map::IterMut<'_, String, Value> {
        self.0.iter_mut()
    }

    /// Clears the object, removing all key-value pairs
    pub fn clear(&mut self) {
        self.0.clear();
    }
}

impl Index<&str> for Object {
    type Output = Value;

    #[allow(clippy::indexing_slicing)]
    fn index(&self, key: &str) -> &Self::Output {
        &self.0[key]
    }
}

impl Index<String> for Object {
    type Output = Value;

    #[allow(clippy::indexing_slicing)]
    fn index(&self, key: String) -> &Self::Output {
        &self.0[&key]
    }
}

impl<'a> IntoIterator for &'a Object {
    type Item = (&'a String, &'a Value);
    type IntoIter = Iter<'a, String, Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl IntoIterator for Object {
    type Item = (String, Value);
    type IntoIter = IntoIter<String, Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl From<IndexMap<String, Value>> for Object {
    fn from(map: IndexMap<String, Value>) -> Self {
        Self(map)
    }
}

impl FromIterator<(String, Value)> for Object {
    fn from_iter<I: IntoIterator<Item = (String, Value)>>(iter: I) -> Self {
        Self(IndexMap::from_iter(iter))
    }
}

/// An array of values
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Array(pub(crate) Vec<Value>);

impl Array {
    /// Creates a new empty array
    pub fn new() -> Self {
        Self(Vec::new())
    }

    /// Creates a new array with the given capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self(Vec::with_capacity(capacity))
    }

    /// Returns the number of elements in the array
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the array contains no elements
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns a reference to the element at the given index
    pub fn get(&self, index: usize) -> Option<&Value> {
        self.0.get(index)
    }

    /// Returns a mutable reference to the element at the given index
    pub fn get_mut(&mut self, index: usize) -> Option<&mut Value> {
        self.0.get_mut(index)
    }

    /// Appends an element to the end of the array
    pub fn push(&mut self, value: impl Into<Value>) {
        self.0.push(value.into());
    }

    /// Removes the last element from the array and returns it
    pub fn pop(&mut self) -> Option<Value> {
        self.0.pop()
    }

    /// Returns an iterator over the array
    pub fn iter(&self) -> std::slice::Iter<'_, Value> {
        self.0.iter()
    }

    /// Returns an iterator that allows modifying each value
    pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Value> {
        self.0.iter_mut()
    }

    /// Clears the array, removing all values
    pub fn clear(&mut self) {
        self.0.clear();
    }

    /// Inserts an element at the given index
    pub fn insert(&mut self, index: usize, value: impl Into<Value>) {
        self.0.insert(index, value.into());
    }

    /// Removes and returns the element at the given index
    pub fn remove(&mut self, index: usize) -> Value {
        self.0.remove(index)
    }
}

impl Index<usize> for Array {
    type Output = Value;

    #[allow(clippy::indexing_slicing)]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl<'a> IntoIterator for &'a Array {
    type Item = &'a Value;
    type IntoIter = std::slice::Iter<'a, Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl IntoIterator for Array {
    type Item = Value;
    type IntoIter = std::vec::IntoIter<Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl From<Vec<Value>> for Array {
    fn from(values: Vec<Value>) -> Self {
        Self(values)
    }
}

impl FromIterator<Value> for Array {
    fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
        Self(Vec::from_iter(iter))
    }
}

impl IntoIterator for Value {
    type Item = Self;
    type IntoIter = std::vec::IntoIter<Self>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Self::Array(arr) => arr.into_iter(),
            _ => Vec::new().into_iter(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_value_is_methods() {
        assert!(Value::Null.is_null());
        assert!(!Value::Null.is_bool());
        assert!(!Value::Null.is_number());
        assert!(!Value::Null.is_string());
        assert!(!Value::Null.is_array());
        assert!(!Value::Null.is_object());

        assert!(Value::Bool(true).is_bool());
        assert!(Value::Number(42.0).is_number());
        assert!(Value::String("hello".to_string()).is_string());
        assert!(Value::Array(Array::new()).is_array());
        assert!(Value::Object(Object::new()).is_object());
    }

    #[test]
    fn test_value_as_methods() {
        assert_eq!(Value::Bool(true).as_bool(), Some(true));
        assert_eq!(Value::Bool(false).as_bool(), Some(false));
        assert_eq!(Value::Null.as_bool(), None);

        assert_eq!(Value::Number(42.0).as_number(), Some(42.0));
        assert_eq!(Value::Null.as_number(), None);

        assert_eq!(
            Value::String("hello".to_string()).as_string(),
            Some("hello")
        );
        assert_eq!(Value::Null.as_string(), None);

        assert!(Value::Array(Array::new()).as_array().is_some());
        assert_eq!(Value::Null.as_array(), None);

        assert!(Value::Object(Object::new()).as_object().is_some());
        assert_eq!(Value::Null.as_object(), None);
    }

    #[test]
    fn test_value_from_impls() {
        let v: Value = true.into();
        assert!(matches!(v, Value::Bool(true)));

        let v: Value = 42.0.into();
        assert!(matches!(v, Value::Number(42.0)));

        let v: Value = 42i32.into();
        assert!(matches!(v, Value::Number(42.0)));

        let v: Value = "hello".into();
        assert!(matches!(v, Value::String(s) if s == "hello"));

        let v: Value = Array::new().into();
        assert!(matches!(v, Value::Array(_)));

        let v: Value = Object::new().into();
        assert!(matches!(v, Value::Object(_)));

        let v: Value = vec![Value::Null, Value::Bool(true)].into();
        assert!(matches!(v, Value::Array(arr) if arr.len() == 2));
    }

    #[test]
    fn test_object_basics() {
        let mut obj = Object::new();
        assert!(obj.is_empty());
        assert_eq!(obj.len(), 0);

        obj.insert("key1", Value::String("value1".to_string()));
        assert!(!obj.is_empty());
        assert_eq!(obj.len(), 1);
        assert!(obj.contains_key("key1"));
        assert!(!obj.contains_key("key2"));

        assert_eq!(obj.get("key1"), Some(&Value::String("value1".to_string())));
        assert_eq!(obj.get("key2"), None);

        obj.insert("key2", 42i32);
        assert_eq!(obj.len(), 2);

        let removed = obj.remove("key1");
        assert!(removed.is_some());
        assert_eq!(obj.len(), 1);
    }

    #[test]
    fn test_object_index() {
        let mut obj = Object::new();
        obj.insert("name", "Alice");
        obj.insert("age", 30i32);

        assert_eq!(obj.get("name"), Some(&Value::String("Alice".to_string())));
        assert_eq!(obj.get("age"), Some(&Value::Number(30.0)));

        let key = "name".to_string();
        assert_eq!(obj.get(&key), Some(&Value::String("Alice".to_string())));
    }

    #[test]
    fn test_object_order_preservation() {
        let mut obj = Object::new();
        obj.insert("first", 1i32);
        obj.insert("second", 2i32);
        obj.insert("third", 3i32);

        let keys: Vec<_> = obj.keys().collect();
        assert_eq!(keys, vec!["first", "second", "third"]);

        let values: Vec<_> = obj.values().collect();
        assert_eq!(
            values,
            vec![
                &Value::Number(1.0),
                &Value::Number(2.0),
                &Value::Number(3.0)
            ]
        );
    }

    #[test]
    fn test_object_iter() {
        let mut obj = Object::new();
        obj.insert("a", 1i32);
        obj.insert("b", 2i32);

        let mut count = 0;
        for (k, v) in &obj {
            count += 1;
            assert!(matches!(v, Value::Number(1.0) | Value::Number(2.0)));
            assert!(k == "a" || k == "b");
        }
        assert_eq!(count, 2);

        let obj2: Object = obj.into_iter().collect();
        assert_eq!(obj2.len(), 2);
    }

    #[test]
    fn test_array_basics() {
        let mut arr = Array::new();
        assert!(arr.is_empty());
        assert_eq!(arr.len(), 0);

        arr.push(Value::Null);
        assert!(!arr.is_empty());
        assert_eq!(arr.len(), 1);

        arr.push(42i32);
        assert_eq!(arr.len(), 2);

        assert_eq!(arr.get(0), Some(&Value::Null));
        assert_eq!(arr.get(1), Some(&Value::Number(42.0)));
        assert_eq!(arr.get(2), None);

        let popped = arr.pop();
        assert_eq!(popped, Some(Value::Number(42.0)));
        assert_eq!(arr.len(), 1);
    }

    #[test]
    fn test_array_index() {
        let mut arr = Array::new();
        arr.push("hello");
        arr.push(42i32);

        assert_eq!(arr.get(0), Some(&Value::String("hello".to_string())));
        assert_eq!(arr.get(1), Some(&Value::Number(42.0)));
    }

    #[test]
    fn test_array_iter() {
        let mut arr = Array::new();
        arr.push(1i32);
        arr.push(2i32);
        arr.push(3i32);

        let mut sum = 0.0;
        for v in &arr {
            if let Value::Number(n) = v {
                sum += n;
            }
        }
        assert_eq!(sum, 6.0);

        let arr2: Array = arr.into_iter().collect();
        assert_eq!(arr2.len(), 3);
    }

    #[test]
    fn test_value_into_iterator() {
        let arr = Array(vec![Value::Null, Value::Bool(true)]);
        let value = Value::Array(arr);

        let collected: Vec<_> = value.into_iter().collect();
        assert_eq!(collected.len(), 2);
    }

    #[test]
    fn test_non_array_value_into_iterator() {
        let value = Value::Null;
        let collected: Vec<_> = value.into_iter().collect();
        assert!(collected.is_empty());
    }
}