simdjson_rust/ondemand/
field.rs1use std::{marker::PhantomData, ptr::NonNull};
2
3use simdjson_sys as ffi;
4
5use super::{document::Document, value::Value};
6use crate::{
7 error::Result,
8 macros::{impl_drop, map_result},
9 utils::string_view_to_str,
10};
11
12pub struct Field<'a> {
13 ptr: NonNull<ffi::SJ_OD_field>,
14 _doc: PhantomData<&'a mut Document<'a, 'a>>,
15}
16
17impl<'a> Field<'a> {
18 pub fn new(ptr: NonNull<ffi::SJ_OD_field>) -> Self {
19 Self {
20 ptr,
21 _doc: PhantomData,
22 }
23 }
24
25 pub fn unescaped_key(&mut self, allow_replacement: bool) -> Result<&'a str> {
26 let sv = map_result!(
27 ffi::SJ_OD_field_unescaped_key(self.ptr.as_mut(), allow_replacement),
28 ffi::STD_string_view_result_error,
29 ffi::STD_string_view_result_value_unsafe
30 )?;
31 Ok(string_view_to_str(sv))
32 }
33
34 pub fn take_value(self) -> Value<'a> {
44 let ptr = unsafe {
45 let ptr = ffi::SJ_OD_field_take_value(self.ptr.as_ptr());
46 NonNull::new_unchecked(ptr)
47 };
48
49 Value::new(ptr)
50 }
51}
52
53impl_drop!(Field<'a>, ffi::SJ_OD_field_free);