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
use super::element::Element;
use super::parser::Parser;
use crate::error::{SimdJsonError, SimdJsonResult};
use crate::libsimdjson::ffi;
use cxx::UniquePtr;
use std::marker::PhantomData;

pub type DocumentStreamIterPtr = UniquePtr<ffi::DocumentStreamIterator>;
pub type DocumentStreamPtr = UniquePtr<ffi::document_stream>;

pub struct DocumentStream<'a> {
    ptr: DocumentStreamPtr,
    _phantom: PhantomData<&'a Parser>,
    iter: DocumentStreamIterPtr,
}

impl<'a> DocumentStream<'a> {
    pub fn new(ptr: DocumentStreamPtr) -> Self {
        DocumentStream {
            ptr,
            _phantom: PhantomData,
            iter: DocumentStreamIterPtr::null(),
        }
    }
}

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

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

    fn next(&mut self) -> Option<Self::Item> {
        if self.iter.is_null() {
            self.iter = ffi::document_stream_get_iterator(self.ptr.pin_mut());
        } else {
            ffi::document_stream_iterator_next(self.iter.pin_mut());
        }

        let result = ffi::document_stream_iterator_deref(self.iter.pin_mut());
        if result.code == 0 && result.value.is_null() {
            None
        } else {
            Some(check_result!(result, Element))
        }
    }
}