1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::element::Element;
use super::parser::Parser;
use crate::error::{SimdJsonError, SimdJsonResult};
use crate::libsimdjson::ffi;
use cxx::UniquePtr;
use std::fmt;
use std::marker::PhantomData;

pub type ArrayIterPtr = UniquePtr<ffi::ArrayIterator>;
pub type ArrayPtr = UniquePtr<ffi::array>;

pub struct Array<'a> {
    ptr: ArrayPtr,
    _phantom: PhantomData<&'a Parser>,
}

impl<'a> Array<'a> {
    pub fn new(ptr: ArrayPtr) -> Self {
        Array {
            ptr,
            _phantom: PhantomData,
        }
    }

    pub fn at_pointer(&self, json_pointer: &str) -> SimdJsonResult<Element> {
        let result = ffi::array_at_pointer(&self.ptr, json_pointer);
        check_result!(result, Element)
    }

    pub fn at(&self, index: usize) -> SimdJsonResult<Element> {
        let result = ffi::array_at(&self.ptr, index);
        check_result!(result, Element)
    }

    pub fn minify(&self) -> String {
        ffi::array_minify(&self.ptr)
    }
}

impl<'a> From<ArrayPtr> for Array<'a> {
    fn from(ptr: ArrayPtr) -> Self {
        Array::new(ptr)
    }
}

pub struct ArrayIter<'a> {
    ptr: ArrayIterPtr,
    // _phantom: PhantomData<&'a Parser>,
    #[allow(dead_code)]
    array: &'a Array<'a>,
}

impl<'a> ArrayIter<'a> {
    pub fn new(array: &'a Array<'a>) -> Self {
        let ptr = ffi::array_get_iterator(&array.ptr);
        ArrayIter {
            ptr,
            // _phantom: PhantomData,
            array,
        }
    }
}

impl<'a> Iterator for ArrayIter<'a> {
    type Item = Element<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let next_ptr = ffi::array_iterator_next(self.ptr.pin_mut());
        if next_ptr.is_null() {
            None
        } else {
            Some(Element::from(next_ptr))
        }
    }
}

impl<'a> IntoIterator for &'a Array<'a> {
    type Item = Element<'a>;
    type IntoIter = ArrayIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ArrayIter::new(self)
    }
}

#[cfg(test)]
mod tests {

    use crate::dom::parser::Parser;
    // use super::element::GetValue;

    #[test]
    fn array_iter() -> Result<(), Box<dyn std::error::Error>> {
        let mut parser = Parser::default();
        let elm = parser.parse("[true, true, true, true]")?;
        let arr = elm.get_array()?;

        assert!(arr.at(3)?.get_bool()?);

        let mut c = 0;
        for v in &arr {
            c += 1;
            println!("c={}, v={}", c, v.get_bool()?);

            assert!(v.get_bool()?);
        }

        Ok(())
    }
}

impl<'a> fmt::Display for Array<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.minify())
    }
}