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
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 crate::prelude::*;
199
200    #[test]
201    #[should_panic = "Not supported"]
202    #[allow(unused_variables, clippy::no_effect)]
203    fn object_index() {
204        let v = StaticNode::Null;
205        v["test"];
206    }
207
208    #[test]
209    #[should_panic = "Not supported"]
210    fn mut_object_index() {
211        let mut v = StaticNode::Null;
212        v["test"] = ();
213    }
214
215    #[test]
216    #[should_panic = "Not supported"]
217    #[allow(unused_variables, clippy::no_effect)]
218    fn array_index() {
219        let v = StaticNode::Null;
220        v[0];
221    }
222
223    #[test]
224    #[should_panic = "Not supported"]
225    fn mut_array_index() {
226        let mut v = StaticNode::Null;
227        v[0] = ();
228    }
229
230    #[test]
231    fn conversion_str() {
232        let v = StaticNode::Null;
233        assert!(!v.is_str());
234    }
235    #[cfg(feature = "128bit")]
236    #[test]
237    fn conversions_i128() {
238        let v = Value::from(i128::MAX);
239        assert!(v.is_i128());
240        assert!(v.is_u128());
241        assert!(!v.is_i64());
242        assert!(!v.is_u64());
243        assert!(!v.is_i32());
244        assert!(!v.is_u32());
245        assert!(!v.is_i16());
246        assert!(!v.is_u16());
247        assert!(!v.is_i8());
248        assert!(!v.is_u8());
249        assert!(!v.is_f64());
250        assert!(!v.is_f32());
251        assert!(v.is_f64_castable());
252        let v = Value::from(i128::MIN);
253        assert!(v.is_i128());
254        assert!(!v.is_u128());
255        assert!(!v.is_i64());
256        assert!(!v.is_u64());
257        assert!(!v.is_i32());
258        assert!(!v.is_u32());
259        assert!(!v.is_i16());
260        assert!(!v.is_u16());
261        assert!(!v.is_i8());
262        assert!(!v.is_u8());
263        assert!(!v.is_f64());
264        assert!(!v.is_f32());
265        assert!(v.is_f64_castable());
266    }
267
268    #[test]
269    fn conversions_i64() {
270        let v = Value::from(i64::MAX);
271        assert!(v.is_i128());
272        assert!(v.is_u128());
273        assert!(v.is_i64());
274        assert!(v.is_u64());
275        assert!(!v.is_i32());
276        assert!(!v.is_u32());
277        assert!(!v.is_i16());
278        assert!(!v.is_u16());
279        assert!(!v.is_i8());
280        assert!(!v.is_u8());
281        assert!(!v.is_f64());
282        assert!(!v.is_f32());
283        assert!(v.is_f64_castable());
284        let v = Value::from(i64::MIN);
285        assert!(v.is_i128());
286        assert!(!v.is_u128());
287        assert!(v.is_i64());
288        assert!(!v.is_u64());
289        assert!(!v.is_i32());
290        assert!(!v.is_u32());
291        assert!(!v.is_i16());
292        assert!(!v.is_u16());
293        assert!(!v.is_i8());
294        assert!(!v.is_u8());
295        assert!(!v.is_f64());
296        assert!(!v.is_f32());
297        assert!(v.is_f64_castable());
298    }
299
300    #[test]
301    fn conversions_i32() {
302        let v = Value::from(i32::MAX);
303        assert!(v.is_i128());
304        assert!(v.is_u128());
305        assert!(v.is_i64());
306        assert!(v.is_u64());
307        assert!(v.is_i32());
308        assert!(v.is_u32());
309        assert!(!v.is_i16());
310        assert!(!v.is_u16());
311        assert!(!v.is_i8());
312        assert!(!v.is_u8());
313        assert!(!v.is_f64());
314        assert!(!v.is_f32());
315        assert!(v.is_f64_castable());
316        let v = Value::from(i32::MIN);
317        assert!(v.is_i128());
318        assert!(!v.is_u128());
319        assert!(v.is_i64());
320        assert!(!v.is_u64());
321        assert!(v.is_i32());
322        assert!(!v.is_u32());
323        assert!(!v.is_i16());
324        assert!(!v.is_u16());
325        assert!(!v.is_i8());
326        assert!(!v.is_u8());
327        assert!(!v.is_f64());
328        assert!(!v.is_f32());
329        assert!(v.is_f64_castable());
330    }
331
332    #[test]
333    fn conversions_i16() {
334        let v = Value::from(i16::MAX);
335        assert!(v.is_i128());
336        assert!(v.is_u128());
337        assert!(v.is_i64());
338        assert!(v.is_u64());
339        assert!(v.is_i32());
340        assert!(v.is_u32());
341        assert!(v.is_i16());
342        assert!(v.is_u16());
343        assert!(!v.is_i8());
344        assert!(!v.is_u8());
345        assert!(!v.is_f64());
346        assert!(!v.is_f32());
347        assert!(v.is_f64_castable());
348        let v = Value::from(i16::MIN);
349        assert!(v.is_i128());
350        assert!(!v.is_u128());
351        assert!(v.is_i64());
352        assert!(!v.is_u64());
353        assert!(v.is_i32());
354        assert!(!v.is_u32());
355        assert!(v.is_i16());
356        assert!(!v.is_u16());
357        assert!(!v.is_i8());
358        assert!(!v.is_u8());
359        assert!(!v.is_f64());
360        assert!(!v.is_f32());
361        assert!(v.is_f64_castable());
362        assert!(v.is_f64_castable());
363    }
364
365    #[test]
366    fn conversions_i8() {
367        let v = Value::from(i8::MAX);
368        assert!(v.is_i128());
369        assert!(v.is_u128());
370        assert!(v.is_i64());
371        assert!(v.is_u64());
372        assert!(v.is_i32());
373        assert!(v.is_u32());
374        assert!(v.is_i16());
375        assert!(v.is_u16());
376        assert!(v.is_i8());
377        assert!(v.is_u8());
378        assert!(!v.is_f64());
379        assert!(!v.is_f32());
380        assert!(v.is_f64_castable());
381        let v = Value::from(i8::MIN);
382        assert!(v.is_i128());
383        assert!(!v.is_u128());
384        assert!(v.is_i64());
385        assert!(!v.is_u64());
386        assert!(v.is_i32());
387        assert!(!v.is_u32());
388        assert!(v.is_i16());
389        assert!(!v.is_u16());
390        assert!(v.is_i8());
391        assert!(!v.is_u8());
392        assert!(!v.is_f64());
393        assert!(!v.is_f32());
394        assert!(v.is_f64_castable());
395    }
396
397    #[test]
398    fn conversions_usize() {
399        let v = Value::from(usize::MIN as u64);
400        assert!(v.is_i128());
401        assert!(v.is_u128());
402        assert!(v.is_i64());
403        assert!(v.is_u64());
404        assert!(v.is_usize());
405        assert!(v.is_i32());
406        assert!(v.is_u32());
407        assert!(v.is_i16());
408        assert!(v.is_u16());
409        assert!(v.is_i8());
410        assert!(v.is_u8());
411        assert!(!v.is_f64());
412        assert!(!v.is_f32());
413        assert!(!v.is_f64());
414        assert!(!v.is_f32());
415        assert!(v.is_f64_castable());
416    }
417
418    #[cfg(feature = "128bit")]
419    #[test]
420    fn conversions_u128() {
421        let v = Value::from(u128::MIN);
422        assert!(v.is_i128());
423        assert!(v.is_u128());
424        assert!(v.is_i64());
425        assert!(v.is_u64());
426        assert!(v.is_i32());
427        assert!(v.is_u32());
428        assert!(v.is_i16());
429        assert!(v.is_u16());
430        assert!(v.is_i8());
431        assert!(v.is_u8());
432        assert!(!v.is_f64());
433        assert!(!v.is_f32());
434        assert!(v.is_f64_castable());
435    }
436
437    #[test]
438    fn conversions_u64() {
439        let v = Value::from(u64::MIN);
440        assert!(v.is_i128());
441        assert!(v.is_u128());
442        assert!(v.is_i64());
443        assert!(v.is_u64());
444        assert!(v.is_i32());
445        assert!(v.is_u32());
446        assert!(v.is_i16());
447        assert!(v.is_u16());
448        assert!(v.is_i8());
449        assert!(v.is_u8());
450        assert!(!v.is_f64());
451        assert!(!v.is_f32());
452        assert!(v.is_f64_castable());
453    }
454
455    #[test]
456    fn conversions_u32() {
457        let v = Value::from(u32::MAX);
458        assert!(v.is_i128());
459        assert!(v.is_u128());
460        assert!(v.is_i64());
461        assert!(v.is_u64());
462        assert!(!v.is_i32());
463        assert!(v.is_u32());
464        assert!(!v.is_i16());
465        assert!(!v.is_u16());
466        assert!(!v.is_i8());
467        assert!(!v.is_u8());
468        assert!(!v.is_f64());
469        assert!(!v.is_f32());
470        assert!(v.is_f64_castable());
471    }
472
473    #[test]
474    fn conversions_u16() {
475        let v = Value::from(u16::MAX);
476        assert!(v.is_i128());
477        assert!(v.is_u128());
478        assert!(v.is_i64());
479        assert!(v.is_u64());
480        assert!(v.is_i32());
481        assert!(v.is_u32());
482        assert!(!v.is_i16());
483        assert!(v.is_u16());
484        assert!(!v.is_i8());
485        assert!(!v.is_u8());
486        assert!(!v.is_f64());
487        assert!(!v.is_f32());
488        assert!(v.is_f64_castable());
489    }
490
491    #[test]
492    fn conversions_u8() {
493        let v = Value::from(u8::MAX);
494        assert!(v.is_i128());
495        assert!(v.is_u128());
496        assert!(v.is_i64());
497        assert!(v.is_u64());
498        assert!(v.is_i32());
499        assert!(v.is_u32());
500        assert!(v.is_i16());
501        assert!(v.is_u16());
502        assert!(!v.is_i8());
503        assert!(v.is_u8());
504        assert!(!v.is_f64());
505        assert!(!v.is_f32());
506        assert!(v.is_f64_castable());
507    }
508
509    #[test]
510    fn conversions_f64() {
511        let v = Value::from(f64::MAX);
512        assert!(!v.is_i64());
513        assert!(!v.is_u64());
514        assert!(v.is_f64());
515        assert!(!v.is_f32());
516        assert!(v.is_f64_castable());
517        let v = Value::from(f64::MIN);
518        assert!(!v.is_i64());
519        assert!(!v.is_u64());
520        assert!(v.is_f64());
521        assert!(!v.is_f32());
522        assert!(v.is_f64_castable());
523        let v = Value::from(());
524        assert!(!v.is_f64_castable());
525    }
526
527    #[test]
528    fn conversions_f32() {
529        let v = Value::from(f32::MAX);
530        assert!(!v.is_i64());
531        assert!(!v.is_u64());
532        assert!(v.is_f64());
533        assert!(v.is_f32());
534        assert!(v.is_f64_castable());
535        let v = Value::from(f32::MIN);
536        assert!(!v.is_i64());
537        assert!(!v.is_u64());
538        assert!(v.is_f64());
539        assert!(v.is_f32());
540        assert!(v.is_f64_castable());
541    }
542
543    #[test]
544    fn conversions_bool() {
545        let v = Value::from(true);
546        assert!(v.is_bool());
547        assert_eq!(v.value_type(), ValueType::Bool);
548        let v = Value::from(());
549        assert!(!v.is_bool());
550    }
551
552    #[test]
553    fn conversions_float() {
554        let v = Value::from(42.0);
555        assert!(v.is_f64());
556        assert_eq!(v.value_type(), ValueType::F64);
557        let v = Value::from(());
558        assert!(!v.is_f64());
559    }
560
561    #[test]
562    fn conversions_int() {
563        let v = Value::from(-42);
564        assert!(v.is_i64());
565        assert_eq!(v.value_type(), ValueType::I64);
566        #[cfg(feature = "128bit")]
567        {
568            let v = Value::from(-42_i128);
569            assert!(v.is_i64());
570            assert!(v.is_i128());
571            assert_eq!(v.value_type(), ValueType::I128);
572        }
573        let v = Value::from(());
574        assert!(!v.is_i64());
575        assert!(!v.is_i128());
576    }
577
578    #[test]
579    fn conversions_uint() {
580        let v = Value::from(42_u64);
581        assert!(v.is_u64());
582        assert_eq!(v.value_type(), ValueType::U64);
583        #[cfg(feature = "128bit")]
584        {
585            let v = Value::from(42_u128);
586            assert!(v.is_u64());
587            assert!(v.is_u128());
588            assert_eq!(v.value_type(), ValueType::U128);
589        }
590        let v = Value::from(());
591        assert!(!v.is_u64());
592        assert!(!v.is_u128());
593    }
594
595    #[test]
596    fn conversions_null() {
597        let v = Value::from(());
598        assert!(v.is_null());
599        assert_eq!(v.value_type(), ValueType::Null);
600        let v = Value::from(1);
601        assert!(!v.is_null());
602    }
603
604    #[test]
605    fn default() {
606        assert_eq!(Value::default(), Value::Null);
607    }
608
609    #[test]
610    fn mixed_int_cmp() {
611        assert_eq!(Value::from(1_u64), Value::from(1_i64));
612        assert_eq!(Value::from(1_i64), Value::from(1_u64));
613    }
614
615    #[test]
616    #[cfg(feature = "128bit")]
617    fn mixed_int_cmp_128() {
618        assert_eq!(Value::from(1_u64), Value::from(1_u128));
619        assert_eq!(Value::from(1_u64), Value::from(1_i128));
620        assert_eq!(Value::from(1_i64), Value::from(1_u128));
621        assert_eq!(Value::from(1_i64), Value::from(1_i128));
622
623        assert_eq!(Value::from(1_u128), Value::from(1_u128));
624        assert_eq!(Value::from(1_u128), Value::from(1_i128));
625        assert_eq!(Value::from(1_u128), Value::from(1_u64));
626        assert_eq!(Value::from(1_u128), Value::from(1_i64));
627
628        assert_eq!(Value::from(1_i128), Value::from(1_u128));
629        assert_eq!(Value::from(1_i128), Value::from(1_i128));
630        assert_eq!(Value::from(1_i128), Value::from(1_u64));
631        assert_eq!(Value::from(1_i128), Value::from(1_i64));
632    }
633
634    #[test]
635    fn test_union_cmp() {
636        let v: Value = ().into();
637        assert_eq!(v, ());
638    }
639    #[test]
640    #[allow(clippy::bool_assert_comparison)]
641    fn test_bool_cmp() {
642        let v: Value = true.into();
643        assert_eq!(v, true);
644        let v: Value = false.into();
645        assert_eq!(v, false);
646    }
647}