simd_json/value/lazy/
array.rs1use std::borrow::Cow;
2
3use super::Value;
4use crate::{borrowed, tape};
5
6#[derive(Clone)]
7pub enum Array<'borrow, 'tape, 'input> {
9 Tape(tape::Array<'tape, 'input>),
11 Value(&'borrow borrowed::Array<'input>),
13}
14
15pub enum Iter<'borrow, 'tape, 'input> {
17 Tape(tape::array::Iter<'tape, 'input>),
19 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
34impl<'borrow, 'tape, 'input> Array<'borrow, 'tape, 'input> {
36 #[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 #[allow(clippy::pedantic)] #[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 #[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 #[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}