1use crate::{Json, Number, Value};
2
3pub enum ValueRef<'a, T: Json> {
5 Null,
6 Boolean(bool),
7 Number(&'a T::Number),
8 String(&'a T::String),
9 Array(&'a T::Array),
10 Object(&'a T::Object),
11}
12
13pub enum ValueMut<'a, T: Json> {
15 Null,
16 Boolean(bool),
17 Number(&'a mut T::Number),
18 String(&'a mut T::String),
19 Array(&'a mut T::Array),
20 Object(&'a mut T::Object),
21}
22
23macro_rules! common_impls {
24 ($($ty:ident),*) => {
25 $(
26 impl<'a, T: Json> $ty<'a, T> {
27 pub fn is_null(&self) -> bool {
29 matches!(self, Self::Null)
30 }
31
32 pub fn is_bool(&self) -> bool {
37 matches!(self, Self::Boolean(_))
38 }
39
40 pub fn is_number(&self) -> bool {
45 matches!(self, Self::Number(_))
46 }
47
48 pub fn as_u32(&self) -> Option<u32> {
50 self.as_number().map(Number::as_u32).flatten()
51 }
52
53 pub fn as_u64(&self) -> Option<u64> {
55 self.as_number().map(Number::as_u64).flatten()
56 }
57
58 pub fn as_i32(&self) -> Option<i32> {
60 self.as_number().map(Number::as_i32).flatten()
61 }
62
63 pub fn as_i64(&self) -> Option<i64> {
65 self.as_number().map(Number::as_i64).flatten()
66 }
67
68 pub fn as_f32(&self) -> Option<f32> {
70 self.as_number().map(Number::as_f32).flatten()
71 }
72
73 pub fn as_f32_lossy(&self) -> Option<f32> {
75 self.as_number().map(Number::as_f32_lossy)
76 }
77
78 pub fn as_f64(&self) -> Option<f64> {
80 self.as_number().map(Number::as_f64).flatten()
81 }
82
83 pub fn as_f64_lossy(&self) -> Option<f64> {
85 self.as_number().map(Number::as_f64_lossy)
86 }
87
88 pub fn is_string(&self) -> bool {
94 matches!(self, Self::String(_))
95 }
96
97 pub fn is_array(&self) -> bool {
103 matches!(self, Self::Array(_))
104 }
105
106 pub fn is_object(&self) -> bool {
112 matches!(self, Self::Object(_))
113 }
114 }
115 )*
116 };
117}
118
119common_impls!(ValueRef, ValueMut);
120
121impl<'a, T: Json> ValueRef<'a, T> {
122 pub fn as_bool(&self) -> Option<bool> {
125 match self {
126 Self::Boolean(b) => Some(*b),
127 _ => None,
128 }
129 }
130
131 pub fn as_number(&self) -> Option<&'a T::Number> {
134 match self {
135 Self::Number(n) => Some(n),
136 _ => None,
137 }
138 }
139
140 pub fn as_string(&self) -> Option<&'a T::String> {
143 match self {
144 Self::String(s) => Some(s),
145 _ => None,
146 }
147 }
148
149 pub fn as_str(&self) -> Option<&str> {
152 match self {
153 Self::String(s) => Some(s.as_ref()),
154 _ => None,
155 }
156 }
157
158 pub fn into_str(self) -> Option<&'a str> {
161 match self {
162 Self::String(s) => Some(s.as_ref()),
163 _ => None,
164 }
165 }
166
167 pub fn as_array(&self) -> Option<&'a T::Array> {
170 match self {
171 Self::Array(a) => Some(a),
172 _ => None,
173 }
174 }
175
176 pub fn as_object(&self) -> Option<&'a T::Object> {
179 match self {
180 Self::Object(o) => Some(o),
181 _ => None,
182 }
183 }
184
185 pub fn cloned(&self) -> Value<T>
187 where
188 T::Number: Clone,
189 T::String: Clone,
190 T::Array: Clone,
191 T::Object: Clone,
192 {
193 match self {
194 Self::Null => Value::Null,
195 Self::Boolean(b) => Value::Boolean(*b),
196 Self::Number(n) => Value::Number((*n).clone()),
197 Self::String(s) => Value::String((*s).clone()),
198 Self::Array(a) => Value::Array((*a).clone()),
199 Self::Object(o) => Value::Object((*o).clone()),
200 }
201 }
202}
203
204impl<'a, T: Json> ValueMut<'a, T> {
205 pub fn as_bool(&self) -> Option<bool> {
208 match self {
209 Self::Boolean(b) => Some(*b),
210 _ => None,
211 }
212 }
213
214 pub fn as_number(&self) -> Option<&T::Number> {
217 match self {
218 Self::Number(n) => Some(n),
219 _ => None,
220 }
221 }
222
223 pub fn into_number(self) -> Option<&'a T::Number> {
226 match self {
227 Self::Number(n) => Some(n),
228 _ => None,
229 }
230 }
231
232 pub fn as_str(&self) -> Option<&str> {
235 match self {
236 Self::String(s) => Some(s.as_ref()),
237 _ => None,
238 }
239 }
240
241 pub fn as_array(&self) -> Option<&T::Array> {
244 match self {
245 Self::Array(a) => Some(a),
246 _ => None,
247 }
248 }
249
250 pub fn into_array(self) -> Option<&'a T::Array> {
253 match self {
254 Self::Array(a) => Some(a),
255 _ => None,
256 }
257 }
258
259 pub fn as_object(&self) -> Option<&T::Object> {
262 match self {
263 Self::Object(o) => Some(o),
264 _ => None,
265 }
266 }
267
268 pub fn into_object(self) -> Option<&'a T::Object> {
271 match self {
272 Self::Object(o) => Some(o),
273 _ => None,
274 }
275 }
276
277 pub fn as_array_mut(&mut self) -> Option<&mut T::Array> {
280 match self {
281 Self::Array(a) => Some(a),
282 _ => None,
283 }
284 }
285
286 pub fn into_array_mut(self) -> Option<&'a mut T::Array> {
289 match self {
290 Self::Array(a) => Some(a),
291 _ => None,
292 }
293 }
294
295 pub fn as_object_mut(&mut self) -> Option<&mut T::Object> {
298 match self {
299 Self::Object(o) => Some(o),
300 _ => None,
301 }
302 }
303
304 pub fn into_object_mut(self) -> Option<&'a mut T::Object> {
307 match self {
308 Self::Object(o) => Some(o),
309 _ => None,
310 }
311 }
312
313 pub fn cloned(&self) -> Value<T>
315 where
316 T::Number: Clone,
317 T::String: Clone,
318 T::Array: Clone,
319 T::Object: Clone,
320 {
321 match self {
322 Self::Null => Value::Null,
323 Self::Boolean(b) => Value::Boolean(*b),
324 Self::Number(n) => Value::Number((*n).clone()),
325 Self::String(s) => Value::String((*s).clone()),
326 Self::Array(a) => Value::Array((*a).clone()),
327 Self::Object(o) => Value::Object((*o).clone()),
328 }
329 }
330}