Skip to main content

simd_json/value/
tape.rs

1/// A tape of a parsed json, all values are extracted and validated and
2/// can be used without further computation.
3use value_trait::{StaticNode, TryTypeError, ValueType, base::TypedValue as _};
4
5/// Tape `Array` and its iterator
6pub mod array;
7mod cmp;
8/// Tape `Object` and its iterators
9pub mod object;
10mod trait_impls;
11#[derive(Debug)]
12/// `Tape`
13pub struct Tape<'input>(pub Vec<Node<'input>>);
14pub use array::Array;
15pub use object::Object;
16impl<'input> Tape<'input> {
17    /// Turns the tape into a `Value` that can be used like a `value_trait::Value`
18    #[must_use]
19    pub fn as_value(&self) -> Value<'_, 'input> {
20        // Skip initial zero
21        Value(&self.0)
22    }
23    /// Creates an empty tape with a null element in it
24    #[must_use]
25    pub fn null() -> Self {
26        Self(vec![Node::Static(StaticNode::Null)])
27    }
28
29    /// Clears the tape and returns it with a new lifetime to allow re-using the already
30    /// allocated buffer.
31    #[must_use]
32    pub fn reset<'new>(mut self) -> Tape<'new> {
33        self.0.clear();
34        // SAFETY: At this point the tape is empty, so no data in there has a lifetime associated with it,
35        // so we can safely change the lifetime of the tape to 'new
36        unsafe { std::mem::transmute(self) }
37    }
38
39    /// Deserializes the tape into a type that implements `serde::Deserialize`
40    /// # Errors
41    /// Returns an error if the deserialization fails
42    #[cfg(feature = "serde")]
43    pub fn deserialize<T>(self) -> crate::Result<T>
44    where
45        T: serde::Deserialize<'input>,
46    {
47        use crate::Deserializer;
48
49        let mut deserializer = Deserializer::from_tape(self);
50
51        T::deserialize(&mut deserializer)
52    }
53}
54
55/// Wrapper around the tape that allows interaction via a `Value`-like API.
56#[derive(Clone, Copy, Debug)]
57#[repr(transparent)]
58pub struct Value<'tape, 'input>(pub(crate) &'tape [Node<'input>])
59where
60    'input: 'tape;
61
62impl Value<'static, 'static> {
63    const NULL_TAPE: [Node<'static>; 1] = [Node::Static(StaticNode::Null)];
64    /// A static null value
65    pub const NULL: Value<'static, 'static> = Value(&Self::NULL_TAPE);
66    /// Creates tape value representing a null value
67    #[must_use]
68    pub const fn null() -> Self {
69        Self::NULL
70    }
71}
72
73#[allow(clippy::derive_partial_eq_without_eq)]
74/// Tape `Node`
75#[derive(Debug, Clone, Copy, PartialEq)]
76pub enum Node<'input> {
77    /// A string, located inside the input slice
78    String(&'input str),
79    /// An `Object` with the given `size` starts here.
80    /// the following values are keys and values, alternating
81    /// however values can be nested and have a length themselves.
82    Object {
83        /// The number of keys in the object
84        len: usize,
85        /// The total number of nodes in the object, including subelements.
86        count: usize,
87    },
88    /// An array with a given size starts here. The next `size`
89    /// elements belong to it - values can be nested and have a
90    /// `size` of their own.
91    Array {
92        /// The number of elements in the array
93        len: usize,
94        /// The total number of nodes in the array, including subelements.
95        count: usize,
96    },
97    /// A static value that is interned into the tape, it can
98    /// be directly taken and isn't nested.
99    Static(StaticNode),
100}
101
102impl<'input> Node<'input> {
103    fn as_str(&self) -> Option<&'input str> {
104        if let Node::String(s) = self {
105            Some(*s)
106        } else {
107            None
108        }
109    }
110    /// Returns the type of the node
111    #[must_use]
112    pub fn value_type(&self) -> ValueType {
113        match self {
114            Node::String(_) => ValueType::String,
115            Node::Object { .. } => ValueType::Object,
116            Node::Array { .. } => ValueType::Array,
117            Node::Static(v) => v.value_type(),
118        }
119    }
120
121    // returns the count of elements in an array
122    fn array_count(&self) -> Result<usize, TryTypeError> {
123        if let Node::Array { count, .. } = self {
124            Ok(*count)
125        } else {
126            Err(TryTypeError {
127                expected: ValueType::Array,
128                got: self.value_type(),
129            })
130        }
131    }
132
133    // // returns the length of an array
134    // fn array_len(&self) -> Result<usize, TryTypeError> {
135    //     if let Node::Array { len, .. } = self {
136    //         Ok(*len)
137    //     } else {
138    //         Err(TryTypeError {
139    //             expected: ValueType::Array,
140    //             got: self.value_type(),
141    //         })
142    //     }
143    // }
144
145    // returns the count of nodes in an object
146    fn object_count(&self) -> Result<usize, TryTypeError> {
147        if let Node::Object { count, .. } = self {
148            Ok(*count)
149        } else {
150            Err(TryTypeError {
151                expected: ValueType::Object,
152                got: self.value_type(),
153            })
154        }
155    }
156
157    // returns the count of elements in an array
158    fn object_len(&self) -> Result<usize, TryTypeError> {
159        if let Node::Object { len, .. } = self {
160            Ok(*len)
161        } else {
162            Err(TryTypeError {
163                expected: ValueType::Object,
164                got: self.value_type(),
165            })
166        }
167    }
168
169    // returns the count of elements in this node, including the node itself (n for nested, 1 for the rest)
170    fn count(&self) -> usize {
171        match self {
172            // We add 1 as we need to include the header itself
173            Node::Object { count, .. } | Node::Array { count, .. } => *count + 1,
174            _ => 1,
175        }
176    }
177    //     // Returns the lenght of nested elements
178    //     fn as_len(&self) -> Option<usize> {
179    //         match self {
180    //             Node::Object { len, .. } | Node::Array { len, .. } => Some(*len),
181    //             _ => None,
182    //         }
183    //     }
184
185    // fn as_len_and_count(&self) -> Option<(usize, usize)> {
186    //     match self {
187    //         Node::Object { len, count } | Node::Array { len, count } => Some((*len, *count)),
188    //         _ => None,
189    //     }
190    // }
191}
192
193#[cfg(test)]
194mod test {
195    #![allow(clippy::cognitive_complexity)]
196    use super::StaticNode as Value;
197    use crate::prelude::*;
198
199    #[test]
200    #[should_panic = "Not supported"]
201    #[allow(unused_variables, clippy::no_effect)]
202    fn object_index() {
203        let v = StaticNode::Null;
204        v["test"];
205    }
206
207    #[test]
208    #[should_panic = "Not supported"]
209    fn mut_object_index() {
210        let mut v = StaticNode::Null;
211        v["test"] = ();
212    }
213
214    #[test]
215    #[should_panic = "Not supported"]
216    #[allow(unused_variables, clippy::no_effect)]
217    fn array_index() {
218        let v = StaticNode::Null;
219        v[0];
220    }
221
222    #[test]
223    #[should_panic = "Not supported"]
224    fn mut_array_index() {
225        let mut v = StaticNode::Null;
226        v[0] = ();
227    }
228
229    #[test]
230    fn conversion_str() {
231        let v = StaticNode::Null;
232        assert!(!v.is_str());
233    }
234    #[cfg(feature = "128bit")]
235    #[test]
236    fn conversions_i128() {
237        let v = Value::from(i128::MAX);
238        assert!(v.is_i128());
239        assert!(v.is_u128());
240        assert!(!v.is_i64());
241        assert!(!v.is_u64());
242        assert!(!v.is_i32());
243        assert!(!v.is_u32());
244        assert!(!v.is_i16());
245        assert!(!v.is_u16());
246        assert!(!v.is_i8());
247        assert!(!v.is_u8());
248        assert!(!v.is_f64());
249        assert!(!v.is_f32());
250        assert!(v.is_f64_castable());
251        let v = Value::from(i128::MIN);
252        assert!(v.is_i128());
253        assert!(!v.is_u128());
254        assert!(!v.is_i64());
255        assert!(!v.is_u64());
256        assert!(!v.is_i32());
257        assert!(!v.is_u32());
258        assert!(!v.is_i16());
259        assert!(!v.is_u16());
260        assert!(!v.is_i8());
261        assert!(!v.is_u8());
262        assert!(!v.is_f64());
263        assert!(!v.is_f32());
264        assert!(v.is_f64_castable());
265    }
266
267    #[test]
268    fn conversions_i64() {
269        let v = Value::from(i64::MAX);
270        assert!(v.is_i128());
271        assert!(v.is_u128());
272        assert!(v.is_i64());
273        assert!(v.is_u64());
274        assert!(!v.is_i32());
275        assert!(!v.is_u32());
276        assert!(!v.is_i16());
277        assert!(!v.is_u16());
278        assert!(!v.is_i8());
279        assert!(!v.is_u8());
280        assert!(!v.is_f64());
281        assert!(!v.is_f32());
282        assert!(v.is_f64_castable());
283        let v = Value::from(i64::MIN);
284        assert!(v.is_i128());
285        assert!(!v.is_u128());
286        assert!(v.is_i64());
287        assert!(!v.is_u64());
288        assert!(!v.is_i32());
289        assert!(!v.is_u32());
290        assert!(!v.is_i16());
291        assert!(!v.is_u16());
292        assert!(!v.is_i8());
293        assert!(!v.is_u8());
294        assert!(!v.is_f64());
295        assert!(!v.is_f32());
296        assert!(v.is_f64_castable());
297    }
298
299    #[test]
300    fn conversions_i32() {
301        let v = Value::from(i32::MAX);
302        assert!(v.is_i128());
303        assert!(v.is_u128());
304        assert!(v.is_i64());
305        assert!(v.is_u64());
306        assert!(v.is_i32());
307        assert!(v.is_u32());
308        assert!(!v.is_i16());
309        assert!(!v.is_u16());
310        assert!(!v.is_i8());
311        assert!(!v.is_u8());
312        assert!(!v.is_f64());
313        assert!(!v.is_f32());
314        assert!(v.is_f64_castable());
315        let v = Value::from(i32::MIN);
316        assert!(v.is_i128());
317        assert!(!v.is_u128());
318        assert!(v.is_i64());
319        assert!(!v.is_u64());
320        assert!(v.is_i32());
321        assert!(!v.is_u32());
322        assert!(!v.is_i16());
323        assert!(!v.is_u16());
324        assert!(!v.is_i8());
325        assert!(!v.is_u8());
326        assert!(!v.is_f64());
327        assert!(!v.is_f32());
328        assert!(v.is_f64_castable());
329    }
330
331    #[test]
332    fn conversions_i16() {
333        let v = Value::from(i16::MAX);
334        assert!(v.is_i128());
335        assert!(v.is_u128());
336        assert!(v.is_i64());
337        assert!(v.is_u64());
338        assert!(v.is_i32());
339        assert!(v.is_u32());
340        assert!(v.is_i16());
341        assert!(v.is_u16());
342        assert!(!v.is_i8());
343        assert!(!v.is_u8());
344        assert!(!v.is_f64());
345        assert!(!v.is_f32());
346        assert!(v.is_f64_castable());
347        let v = Value::from(i16::MIN);
348        assert!(v.is_i128());
349        assert!(!v.is_u128());
350        assert!(v.is_i64());
351        assert!(!v.is_u64());
352        assert!(v.is_i32());
353        assert!(!v.is_u32());
354        assert!(v.is_i16());
355        assert!(!v.is_u16());
356        assert!(!v.is_i8());
357        assert!(!v.is_u8());
358        assert!(!v.is_f64());
359        assert!(!v.is_f32());
360        assert!(v.is_f64_castable());
361        assert!(v.is_f64_castable());
362    }
363
364    #[test]
365    fn conversions_i8() {
366        let v = Value::from(i8::MAX);
367        assert!(v.is_i128());
368        assert!(v.is_u128());
369        assert!(v.is_i64());
370        assert!(v.is_u64());
371        assert!(v.is_i32());
372        assert!(v.is_u32());
373        assert!(v.is_i16());
374        assert!(v.is_u16());
375        assert!(v.is_i8());
376        assert!(v.is_u8());
377        assert!(!v.is_f64());
378        assert!(!v.is_f32());
379        assert!(v.is_f64_castable());
380        let v = Value::from(i8::MIN);
381        assert!(v.is_i128());
382        assert!(!v.is_u128());
383        assert!(v.is_i64());
384        assert!(!v.is_u64());
385        assert!(v.is_i32());
386        assert!(!v.is_u32());
387        assert!(v.is_i16());
388        assert!(!v.is_u16());
389        assert!(v.is_i8());
390        assert!(!v.is_u8());
391        assert!(!v.is_f64());
392        assert!(!v.is_f32());
393        assert!(v.is_f64_castable());
394    }
395
396    #[test]
397    fn conversions_usize() {
398        let v = Value::from(usize::MIN as u64);
399        assert!(v.is_i128());
400        assert!(v.is_u128());
401        assert!(v.is_i64());
402        assert!(v.is_u64());
403        assert!(v.is_usize());
404        assert!(v.is_i32());
405        assert!(v.is_u32());
406        assert!(v.is_i16());
407        assert!(v.is_u16());
408        assert!(v.is_i8());
409        assert!(v.is_u8());
410        assert!(!v.is_f64());
411        assert!(!v.is_f32());
412        assert!(!v.is_f64());
413        assert!(!v.is_f32());
414        assert!(v.is_f64_castable());
415    }
416
417    #[cfg(feature = "128bit")]
418    #[test]
419    fn conversions_u128() {
420        let v = Value::from(u128::MIN);
421        assert!(v.is_i128());
422        assert!(v.is_u128());
423        assert!(v.is_i64());
424        assert!(v.is_u64());
425        assert!(v.is_i32());
426        assert!(v.is_u32());
427        assert!(v.is_i16());
428        assert!(v.is_u16());
429        assert!(v.is_i8());
430        assert!(v.is_u8());
431        assert!(!v.is_f64());
432        assert!(!v.is_f32());
433        assert!(v.is_f64_castable());
434    }
435
436    #[test]
437    fn conversions_u64() {
438        let v = Value::from(u64::MIN);
439        assert!(v.is_i128());
440        assert!(v.is_u128());
441        assert!(v.is_i64());
442        assert!(v.is_u64());
443        assert!(v.is_i32());
444        assert!(v.is_u32());
445        assert!(v.is_i16());
446        assert!(v.is_u16());
447        assert!(v.is_i8());
448        assert!(v.is_u8());
449        assert!(!v.is_f64());
450        assert!(!v.is_f32());
451        assert!(v.is_f64_castable());
452    }
453
454    #[test]
455    fn conversions_u32() {
456        let v = Value::from(u32::MAX);
457        assert!(v.is_i128());
458        assert!(v.is_u128());
459        assert!(v.is_i64());
460        assert!(v.is_u64());
461        assert!(!v.is_i32());
462        assert!(v.is_u32());
463        assert!(!v.is_i16());
464        assert!(!v.is_u16());
465        assert!(!v.is_i8());
466        assert!(!v.is_u8());
467        assert!(!v.is_f64());
468        assert!(!v.is_f32());
469        assert!(v.is_f64_castable());
470    }
471
472    #[test]
473    fn conversions_u16() {
474        let v = Value::from(u16::MAX);
475        assert!(v.is_i128());
476        assert!(v.is_u128());
477        assert!(v.is_i64());
478        assert!(v.is_u64());
479        assert!(v.is_i32());
480        assert!(v.is_u32());
481        assert!(!v.is_i16());
482        assert!(v.is_u16());
483        assert!(!v.is_i8());
484        assert!(!v.is_u8());
485        assert!(!v.is_f64());
486        assert!(!v.is_f32());
487        assert!(v.is_f64_castable());
488    }
489
490    #[test]
491    fn conversions_u8() {
492        let v = Value::from(u8::MAX);
493        assert!(v.is_i128());
494        assert!(v.is_u128());
495        assert!(v.is_i64());
496        assert!(v.is_u64());
497        assert!(v.is_i32());
498        assert!(v.is_u32());
499        assert!(v.is_i16());
500        assert!(v.is_u16());
501        assert!(!v.is_i8());
502        assert!(v.is_u8());
503        assert!(!v.is_f64());
504        assert!(!v.is_f32());
505        assert!(v.is_f64_castable());
506    }
507
508    #[test]
509    fn conversions_f64() {
510        let v = Value::from(f64::MAX);
511        assert!(!v.is_i64());
512        assert!(!v.is_u64());
513        assert!(v.is_f64());
514        assert!(!v.is_f32());
515        assert!(v.is_f64_castable());
516        let v = Value::from(f64::MIN);
517        assert!(!v.is_i64());
518        assert!(!v.is_u64());
519        assert!(v.is_f64());
520        assert!(!v.is_f32());
521        assert!(v.is_f64_castable());
522        let v = Value::from(());
523        assert!(!v.is_f64_castable());
524    }
525
526    #[test]
527    fn conversions_f32() {
528        let v = Value::from(f32::MAX);
529        assert!(!v.is_i64());
530        assert!(!v.is_u64());
531        assert!(v.is_f64());
532        assert!(v.is_f32());
533        assert!(v.is_f64_castable());
534        let v = Value::from(f32::MIN);
535        assert!(!v.is_i64());
536        assert!(!v.is_u64());
537        assert!(v.is_f64());
538        assert!(v.is_f32());
539        assert!(v.is_f64_castable());
540    }
541
542    #[test]
543    fn conversions_bool() {
544        let v = Value::from(true);
545        assert!(v.is_bool());
546        assert_eq!(v.value_type(), ValueType::Bool);
547        let v = Value::from(());
548        assert!(!v.is_bool());
549    }
550
551    #[test]
552    fn conversions_float() {
553        let v = Value::from(42.0);
554        assert!(v.is_f64());
555        assert_eq!(v.value_type(), ValueType::F64);
556        let v = Value::from(());
557        assert!(!v.is_f64());
558    }
559
560    #[test]
561    fn conversions_int() {
562        let v = Value::from(-42);
563        assert!(v.is_i64());
564        assert_eq!(v.value_type(), ValueType::I64);
565        #[cfg(feature = "128bit")]
566        {
567            let v = Value::from(-42_i128);
568            assert!(v.is_i64());
569            assert!(v.is_i128());
570            assert_eq!(v.value_type(), ValueType::I128);
571        }
572        let v = Value::from(());
573        assert!(!v.is_i64());
574        assert!(!v.is_i128());
575    }
576
577    #[test]
578    fn conversions_uint() {
579        let v = Value::from(42_u64);
580        assert!(v.is_u64());
581        assert_eq!(v.value_type(), ValueType::U64);
582        #[cfg(feature = "128bit")]
583        {
584            let v = Value::from(42_u128);
585            assert!(v.is_u64());
586            assert!(v.is_u128());
587            assert_eq!(v.value_type(), ValueType::U128);
588        }
589        let v = Value::from(());
590        assert!(!v.is_u64());
591        assert!(!v.is_u128());
592    }
593
594    #[test]
595    fn conversions_null() {
596        let v = Value::from(());
597        assert!(v.is_null());
598        assert_eq!(v.value_type(), ValueType::Null);
599        let v = Value::from(1);
600        assert!(!v.is_null());
601    }
602
603    #[test]
604    fn default() {
605        assert_eq!(Value::default(), Value::Null);
606    }
607
608    #[test]
609    fn mixed_int_cmp() {
610        assert_eq!(Value::from(1_u64), Value::from(1_i64));
611        assert_eq!(Value::from(1_i64), Value::from(1_u64));
612    }
613
614    #[test]
615    #[cfg(feature = "128bit")]
616    fn mixed_int_cmp_128() {
617        assert_eq!(Value::from(1_u64), Value::from(1_u128));
618        assert_eq!(Value::from(1_u64), Value::from(1_i128));
619        assert_eq!(Value::from(1_i64), Value::from(1_u128));
620        assert_eq!(Value::from(1_i64), Value::from(1_i128));
621
622        assert_eq!(Value::from(1_u128), Value::from(1_u128));
623        assert_eq!(Value::from(1_u128), Value::from(1_i128));
624        assert_eq!(Value::from(1_u128), Value::from(1_u64));
625        assert_eq!(Value::from(1_u128), Value::from(1_i64));
626
627        assert_eq!(Value::from(1_i128), Value::from(1_u128));
628        assert_eq!(Value::from(1_i128), Value::from(1_i128));
629        assert_eq!(Value::from(1_i128), Value::from(1_u64));
630        assert_eq!(Value::from(1_i128), Value::from(1_i64));
631    }
632
633    #[test]
634    fn test_union_cmp() {
635        let v: Value = ().into();
636        assert_eq!(v, ());
637    }
638    #[test]
639    #[allow(clippy::bool_assert_comparison)]
640    fn test_bool_cmp() {
641        let v: Value = true.into();
642        assert_eq!(v, true);
643        let v: Value = false.into();
644        assert_eq!(v, false);
645    }
646}