simd_json/value/tape/
array.rs1use super::Value;
2use crate::Node;
3
4#[derive(Clone, Copy)]
5pub struct Array<'tape, 'input>(pub(super) &'tape [Node<'input>]);
7
8pub struct Iter<'tape, 'input>(&'tape [Node<'input>]);
9
10impl<'tape, 'input> Iterator for Iter<'tape, 'input> {
11 type Item = Value<'tape, 'input>;
12
13 fn next(&mut self) -> Option<Self::Item> {
14 let (head, tail) = self.0.split_at(self.0.first()?.count());
15 self.0 = tail;
16 Some(Value(head))
17 }
18}
19
20impl<'tape, 'input> Array<'tape, 'input>
22where
23 'input: 'tape,
24{
25 #[must_use]
29 pub fn get(&self, mut idx: usize) -> Option<Value<'tape, 'input>> {
30 let mut offset = 1;
31 while idx > 0 {
32 offset += self.0.get(offset)?.count();
33 idx -= 1;
34 }
35 let count = self.0.get(offset)?.count();
36 Some(Value(&self.0[offset..offset + count]))
37 }
38
39 #[must_use]
41 pub fn iter<'i>(&'i self) -> Iter<'tape, 'input> {
42 Iter(&self.0[1..])
43 }
44
45 #[must_use]
49 pub fn len(&self) -> usize {
50 if let Some(Node::Array { len, .. }) = self.0.first() {
51 *len
52 } else {
53 panic!("invalid tape array")
54 }
55 }
56 #[must_use]
58 pub fn is_empty(&self) -> bool {
59 self.len() == 0
60 }
61}
62
63impl<'tape, 'input> IntoIterator for &Array<'tape, 'input> {
64 type IntoIter = Iter<'tape, 'input>;
65 type Item = Value<'tape, 'input>;
66 fn into_iter(self) -> Self::IntoIter {
67 self.iter()
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}