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