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
use std::{marker::PhantomData, ptr::NonNull};

use simdjson_sys as ffi;

use super::{document::Document, object_iterator::ObjectIterator, value::Value};
use crate::{
    error::Result,
    macros::{impl_drop, map_result},
    utils::string_view_to_str,
};

pub struct Object<'a> {
    ptr: NonNull<ffi::SJ_OD_object>,
    _doc: PhantomData<&'a mut Document<'a, 'a>>,
}

impl<'a> Object<'a> {
    pub fn new(ptr: NonNull<ffi::SJ_OD_object>) -> Self {
        Self {
            ptr,
            _doc: PhantomData,
        }
    }

    pub fn at_pointer(&mut self, json_pointer: &str) -> Result<Value<'a>> {
        map_result!(
            ffi::SJ_OD_object_at_pointer(
                self.ptr.as_mut(),
                json_pointer.as_ptr().cast(),
                json_pointer.len()
            ),
            ffi::SJ_OD_value_result_error,
            ffi::SJ_OD_value_result_value_unsafe
        )
        .map(Value::new)
    }

    pub fn iter(&mut self) -> Result<ObjectIterator<'a>> {
        let begin = map_result!(
            ffi::SJ_OD_object_begin(self.ptr.as_mut()),
            ffi::SJ_OD_object_iterator_result_error,
            ffi::SJ_OD_object_iterator_result_value_unsafe
        )?;
        let end = map_result!(
            ffi::SJ_OD_object_end(self.ptr.as_mut()),
            ffi::SJ_OD_object_iterator_result_error,
            ffi::SJ_OD_object_iterator_result_value_unsafe
        )?;
        Ok(ObjectIterator::new(begin, end))
    }

    pub fn raw_json(&mut self) -> Result<&'a str> {
        let sv = map_result!(
            ffi::SJ_OD_object_raw_json(self.ptr.as_mut()),
            ffi::STD_string_view_result_error,
            ffi::STD_string_view_result_value_unsafe
        )?;
        Ok(string_view_to_str(sv))
    }

    pub fn find_field(&mut self, key: &str) -> Result<Value<'a>> {
        map_result!(
            ffi::SJ_OD_object_find_field(self.ptr.as_mut(), key.as_ptr().cast(), key.len()),
            ffi::SJ_OD_value_result_error,
            ffi::SJ_OD_value_result_value_unsafe
        )
        .map(Value::new)
    }

    pub fn find_field_unordered(&mut self, key: &str) -> Result<Value<'a>> {
        map_result!(
            ffi::SJ_OD_object_find_field_unordered(
                self.ptr.as_mut(),
                key.as_ptr().cast(),
                key.len()
            ),
            ffi::SJ_OD_value_result_error,
            ffi::SJ_OD_value_result_value_unsafe
        )
        .map(Value::new)
    }

    pub fn count_fields(&mut self) -> Result<usize> {
        map_result!(
            primitive,
            ffi::SJ_OD_object_count_fields(self.ptr.as_mut()),
            ffi::size_t_result_error,
            ffi::size_t_result_value_unsafe
        )
    }

    pub fn is_empty(&mut self) -> Result<bool> {
        map_result!(
            primitive,
            ffi::SJ_OD_object_is_empty(self.ptr.as_mut()),
            ffi::bool_result_error,
            ffi::bool_result_value_unsafe
        )
    }

    pub fn reset(&mut self) -> Result<bool> {
        map_result!(
            primitive,
            ffi::SJ_OD_object_reset(self.ptr.as_mut()),
            ffi::bool_result_error,
            ffi::bool_result_value_unsafe
        )
    }
}

impl_drop!(Object<'a>, ffi::SJ_OD_object_free);