simd_json/value/lazy/
array.rs

1use std::borrow::Cow;
2
3use super::Value;
4use crate::{borrowed, tape};
5
6#[derive(Clone)]
7/// Wrapper around the tape that allows interacting with it via a `Array`-like API.
8pub enum Array<'borrow, 'tape, 'input> {
9    /// Tape variant
10    Tape(tape::Array<'tape, 'input>),
11    /// Value variant
12    Value(&'borrow borrowed::Array<'input>),
13}
14
15/// Iterator over the values in an array
16pub enum Iter<'borrow, 'tape, 'input> {
17    /// Tape variant
18    Tape(tape::array::Iter<'tape, 'input>),
19    /// Value variant
20    Value(std::slice::Iter<'borrow, borrowed::Value<'input>>),
21}
22
23impl<'borrow, 'tape, 'input> Iterator for Iter<'borrow, 'tape, 'input> {
24    type Item = Value<'borrow, 'tape, 'input>;
25
26    fn next(&mut self) -> Option<Self::Item> {
27        match self {
28            Iter::Tape(t) => t.next().map(Value::Tape),
29            Iter::Value(v) => v.next().map(Cow::Borrowed).map(Value::Value),
30        }
31    }
32}
33
34// value_trait::Array for
35impl<'borrow, 'tape, 'input> Array<'borrow, 'tape, 'input> {
36    /// Gets a ref to a value based on n index, returns `None` if the
37    /// current Value isn't an Array or doesn't contain the index
38    /// it was asked for.
39    #[must_use]
40    pub fn get<'a>(&'a self, idx: usize) -> Option<Value<'a, 'tape, 'input>> {
41        match self {
42            Array::Tape(t) => t.get(idx).map(Value::Tape),
43            Array::Value(v) => v.get(idx).map(Cow::Borrowed).map(Value::Value),
44        }
45    }
46    /// Iterates over the values paris
47    #[allow(clippy::pedantic)] // we want into_iter_without_iter but that lint doesn't exist in older clippy
48    #[must_use]
49    pub fn iter<'i>(&'i self) -> Iter<'i, 'tape, 'input> {
50        match self {
51            Array::Tape(t) => Iter::Tape(t.iter()),
52            Array::Value(v) => Iter::Value(v.iter()),
53        }
54    }
55
56    /// Number of key/value pairs
57    #[must_use]
58    pub fn len(&self) -> usize {
59        match self {
60            Array::Tape(t) => t.len(),
61            Array::Value(v) => v.len(),
62        }
63    }
64    /// Returns if the array is empty
65    #[must_use]
66    pub fn is_empty(&self) -> bool {
67        self.len() == 0
68    }
69}
70
71#[cfg(test)]
72mod test {
73    use crate::to_tape;
74    use value_trait::base::ValueAsScalar;
75
76    #[test]
77    fn get_ints() -> crate::Result<()> {
78        let mut input = b"[1,2,3,4]".to_vec();
79        let t = to_tape(input.as_mut_slice())?;
80        let v = t.as_value();
81        let a = v.as_array().expect("is an array");
82        assert_eq!(a.get(0).and_then(|v| v.as_u64()), Some(1));
83        assert_eq!(a.get(1).and_then(|v| v.as_u64()), Some(2));
84        assert_eq!(a.get(2).and_then(|v| v.as_u64()), Some(3));
85        assert_eq!(a.get(3).and_then(|v| v.as_u64()), Some(4));
86        assert_eq!(a.get(4), None);
87        Ok(())
88    }
89
90    #[test]
91    fn get_nested() -> crate::Result<()> {
92        let mut input = b"[1,[2,3],4]".to_vec();
93        let t = to_tape(input.as_mut_slice())?;
94        let v = t.as_value();
95        let a = v.as_array().expect("is an array");
96        assert_eq!(a.get(0).and_then(|v| v.as_u64()), Some(1));
97        let a1 = a.get(1).expect("has first element");
98        let a2 = a1.as_array().expect("is an array");
99        assert_eq!(a2.get(0).and_then(|v| v.as_u64()), Some(2));
100        assert_eq!(a2.get(1).and_then(|v| v.as_u64()), Some(3));
101        assert_eq!(a.get(2).and_then(|v| v.as_u64()), Some(4));
102        assert_eq!(a.get(3), None);
103        Ok(())
104    }
105
106    #[test]
107    fn iter() -> crate::Result<()> {
108        let mut input = b"[1,2,3,4]".to_vec();
109        let t = to_tape(input.as_mut_slice())?;
110        let v = t.as_value();
111        let a = v.as_array().expect("is an array");
112        let v = a
113            .iter()
114            .map(|v| v.as_u8().expect("integer"))
115            .collect::<Vec<_>>();
116
117        assert_eq!(v, vec![1, 2, 3, 4]);
118
119        Ok(())
120    }
121    #[test]
122    fn iter_container() -> crate::Result<()> {
123        let mut input = b"[1,[2,3],4]".to_vec();
124        let t = to_tape(input.as_mut_slice())?;
125        let v = t.as_value();
126        let a = v.as_array().expect("is an array");
127        let v = a.iter().map(|v| v.as_u8()).collect::<Vec<_>>();
128
129        assert_eq!(v, vec![Some(1), None, Some(4)]);
130
131        Ok(())
132    }
133}