Skip to main content

fixed_json/
builder.rs

1use core::{array, mem::MaybeUninit, ptr, slice};
2
3use crate::{Array, Attr, Result, read_object};
4
5pub struct ObjectBuilder<'input, 'a, const N: usize> {
6    input: &'input str,
7    attrs: [MaybeUninit<Attr<'a>>; N],
8    len: usize,
9}
10
11impl<'input, 'a, const N: usize> ObjectBuilder<'input, 'a, N> {
12    #[inline]
13    pub fn new(input: &'input str) -> Self {
14        Self {
15            input,
16            attrs: array::from_fn(|_| MaybeUninit::uninit()),
17            len: 0,
18        }
19    }
20
21    #[inline]
22    pub fn attr(mut self, attr: Attr<'a>) -> Self {
23        assert!(self.len < N, "too many attributes for ObjectBuilder");
24        self.attrs[self.len].write(attr);
25        self.len += 1;
26        self
27    }
28
29    #[inline]
30    pub fn integer(self, name: &'a str, target: &'a mut i32) -> Self {
31        self.attr(Attr::integer(name, target))
32    }
33
34    #[inline]
35    pub fn integers(self, name: &'a str, target: &'a mut [i32]) -> Self {
36        self.attr(Attr::integers(name, target))
37    }
38
39    #[inline]
40    pub fn uinteger(self, name: &'a str, target: &'a mut u32) -> Self {
41        self.attr(Attr::uinteger(name, target))
42    }
43
44    #[inline]
45    pub fn uintegers(self, name: &'a str, target: &'a mut [u32]) -> Self {
46        self.attr(Attr::uintegers(name, target))
47    }
48
49    #[inline]
50    pub fn short(self, name: &'a str, target: &'a mut i16) -> Self {
51        self.attr(Attr::short(name, target))
52    }
53
54    #[inline]
55    pub fn shorts(self, name: &'a str, target: &'a mut [i16]) -> Self {
56        self.attr(Attr::shorts(name, target))
57    }
58
59    #[inline]
60    pub fn ushort(self, name: &'a str, target: &'a mut u16) -> Self {
61        self.attr(Attr::ushort(name, target))
62    }
63
64    #[inline]
65    pub fn ushorts(self, name: &'a str, target: &'a mut [u16]) -> Self {
66        self.attr(Attr::ushorts(name, target))
67    }
68}
69
70impl<'input, 'a, const N: usize> ObjectBuilder<'input, 'a, N> {
71    #[inline]
72    pub fn real(self, name: &'a str, target: &'a mut f64) -> Self {
73        self.attr(Attr::real(name, target))
74    }
75
76    #[inline]
77    pub fn reals(self, name: &'a str, target: &'a mut [f64]) -> Self {
78        self.attr(Attr::reals(name, target))
79    }
80
81    #[inline]
82    pub fn string(self, name: &'a str, target: &'a mut [u8]) -> Self {
83        self.attr(Attr::string(name, target))
84    }
85
86    #[inline]
87    pub fn boolean(self, name: &'a str, target: &'a mut bool) -> Self {
88        self.attr(Attr::boolean(name, target))
89    }
90
91    #[inline]
92    pub fn booleans(self, name: &'a str, target: &'a mut [bool]) -> Self {
93        self.attr(Attr::booleans(name, target))
94    }
95
96    #[inline]
97    pub fn character(self, name: &'a str, target: &'a mut u8) -> Self {
98        self.attr(Attr::character(name, target))
99    }
100
101    #[inline]
102    pub fn characters(self, name: &'a str, target: &'a mut [u8]) -> Self {
103        self.attr(Attr::characters(name, target))
104    }
105}
106
107impl<'input, 'a, const N: usize> ObjectBuilder<'input, 'a, N> {
108    #[inline]
109    pub fn time(self, name: &'a str, target: &'a mut f64) -> Self {
110        self.attr(Attr::time(name, target))
111    }
112
113    #[inline]
114    pub fn object(self, name: &'a str, attrs: &'a mut [Attr<'a>]) -> Self {
115        self.attr(Attr::object(name, attrs))
116    }
117
118    #[inline]
119    pub fn array(self, name: &'a str, array: Array<'a>) -> Self {
120        self.attr(Attr::array(name, array))
121    }
122
123    #[inline]
124    pub fn check(self, name: &'a str, expected: &'a str) -> Self {
125        self.attr(Attr::check(name, expected))
126    }
127
128    #[inline]
129    pub fn ignore_any(self) -> Self {
130        self.attr(Attr::ignore_any())
131    }
132
133    #[inline]
134    pub fn read(mut self) -> Result<usize> {
135        // SAFETY: `self.attrs` is initialized up to `self.len` by the builder methods, and we only read that prefix.
136        let attrs = unsafe {
137            slice::from_raw_parts_mut(self.attrs.as_mut_ptr().cast::<Attr<'a>>(), self.len)
138        };
139        read_object(self.input, attrs)
140    }
141}
142
143impl<const N: usize> Drop for ObjectBuilder<'_, '_, N> {
144    fn drop(&mut self) {
145        for attr in &mut self.attrs[..self.len] {
146            // SAFETY: `self.attrs` is initialized up to `self.len` by the builder methods, and we only drop that prefix.
147            unsafe {
148                ptr::drop_in_place(attr.as_mut_ptr());
149            }
150        }
151    }
152}