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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::{Json, Number, Value};

/// JSON value reference.
pub enum ValueRef<'a, T: Json> {
	Null,
	Boolean(bool),
	Number(&'a T::Number),
	String(&'a T::String),
	Array(&'a T::Array),
	Object(&'a T::Object),
}

/// Mutable JSON value reference.
pub enum ValueMut<'a, T: Json> {
	Null,
	Boolean(bool),
	Number(&'a mut T::Number),
	String(&'a mut T::String),
	Array(&'a mut T::Array),
	Object(&'a mut T::Object),
}

macro_rules! common_impls {
	($($ty:ident),*) => {
		$(
			impl<'a, T: Json> $ty<'a, T> {
				/// Returns `true` if the value is a `Null`. Returns `false` otherwise.
				pub fn is_null(&self) -> bool {
					matches!(self, Self::Null)
				}

				/// Returns `true` if the value is a boolean. Returns `false` otherwise.
				///
				/// For any value on which `is_boolean` returns `true`,
				/// [`as_bool`](Self::as_bool()) is guaranteed to return the boolean value.
				pub fn is_bool(&self) -> bool {
					matches!(self, Self::Boolean(_))
				}

				/// Returns `true` if the value is a number. Returns `false` otherwise.
				///
				/// For any value on which `is_number` returns `true`,
				/// [`as_number`](Self::as_number()) is guaranteed to return the number value.
				pub fn is_number(&self) -> bool {
					matches!(self, Self::Number(_))
				}

                /// Returns this number as an `u32` if it can be exactly represented as such.
                pub fn as_u32(&self) -> Option<u32> {
                    self.as_number().map(Number::as_u32).flatten()
                }

                /// Returns this number as an `u64` if it can be exactly represented as such.
                pub fn as_u64(&self) -> Option<u64> {
                    self.as_number().map(Number::as_u64).flatten()
                }

                /// Returns this number as an `i32` if it can be exactly represented as such.
                pub fn as_i32(&self) -> Option<i32> {
                    self.as_number().map(Number::as_i32).flatten()
                }

                /// Returns this number as an `i64` if it can be exactly represented as such.
                pub fn as_i64(&self) -> Option<i64> {
                    self.as_number().map(Number::as_i64).flatten()
                }

                /// Returns this number as an `f32` if it can be exactly represented as such.
                pub fn as_f32(&self) -> Option<f32> {
                    self.as_number().map(Number::as_f32).flatten()
                }

                /// Returns this number as an `f32` if it is a number, potentially losing precision in the process.
                pub fn as_f32_lossy(&self) -> Option<f32> {
                    self.as_number().map(Number::as_f32_lossy)
                }

                /// Returns this number as an `f64` if it can be exactly represented as such.
                pub fn as_f64(&self) -> Option<f64> {
                    self.as_number().map(Number::as_f64).flatten()
                }

                /// Returns this number as an `f64` if it is a number, potentially losing precision in the process.
                pub fn as_f64_lossy(&self) -> Option<f64> {
                    self.as_number().map(Number::as_f64_lossy)
                }

				/// Returns `true` if the value is a string.
				/// Returns `false` otherwise.
				///
				/// For any value on which `is_string` returns `true`,
				/// [`as_str`](Self::as_str()) is guaranteed to return the string value.
				pub fn is_string(&self) -> bool {
					matches!(self, Self::String(_))
				}

				/// Returns `true` if the value is an array.
				/// Returns `false` otherwise.
				///
				/// For any value on which `is_array` returns `true`,
				/// [`as_array`](Self::as_array()) is guaranteed to return the array value.
				pub fn is_array(&self) -> bool {
					matches!(self, Self::Array(_))
				}

				/// Returns `true` if the value is an object.
				/// Returns `false` otherwise.
				///
				/// For any value on which `is_object` returns `true`,
				/// [`as_object`](Self::as_object()) is guaranteed to return the object value.
				pub fn is_object(&self) -> bool {
					matches!(self, Self::Object(_))
				}
			}
		)*
	};
}

common_impls!(ValueRef, ValueMut);

impl<'a, T: Json> ValueRef<'a, T> {
	/// If the value is a boolean, returns the associated `bool`.
	/// Returns `None` otherwise.
	pub fn as_bool(&self) -> Option<bool> {
		match self {
			Self::Boolean(b) => Some(*b),
			_ => None,
		}
	}

	/// If the value is a number, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_number(&self) -> Option<&'a T::Number> {
		match self {
			Self::Number(n) => Some(n),
			_ => None,
		}
	}

	/// If the value is a string, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_string(&self) -> Option<&'a T::String> {
		match self {
			Self::String(s) => Some(s),
			_ => None,
		}
	}

	/// If the value is a string, returns its associated [`str`].
	/// Returns `None` otherwise.
	pub fn as_str(&self) -> Option<&str> {
		match self {
			Self::String(s) => Some(s.as_ref()),
			_ => None,
		}
	}

	/// If the value is a string, returns its associated [`str`].
	/// Returns `None` otherwise.
	pub fn into_str(self) -> Option<&'a str> {
		match self {
			Self::String(s) => Some(s.as_ref()),
			_ => None,
		}
	}

	/// If the value is an array, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_array(&self) -> Option<&'a T::Array> {
		match self {
			Self::Array(a) => Some(a),
			_ => None,
		}
	}

	/// If the value is an object, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_object(&self) -> Option<&'a T::Object> {
		match self {
			Self::Object(o) => Some(o),
			_ => None,
		}
	}

	/// Creates a new value by cloning the referenced value.
	pub fn cloned(&self) -> Value<T>
	where
		T::Number: Clone,
		T::String: Clone,
		T::Array: Clone,
		T::Object: Clone,
	{
		match self {
			Self::Null => Value::Null,
			Self::Boolean(b) => Value::Boolean(*b),
			Self::Number(n) => Value::Number((*n).clone()),
			Self::String(s) => Value::String((*s).clone()),
			Self::Array(a) => Value::Array((*a).clone()),
			Self::Object(o) => Value::Object((*o).clone()),
		}
	}
}

impl<'a, T: Json> ValueMut<'a, T> {
	/// If the value is a boolean, returns the associated `bool`.
	/// Returns `None` otherwise.
	pub fn as_bool(&self) -> Option<bool> {
		match self {
			Self::Boolean(b) => Some(*b),
			_ => None,
		}
	}

	/// If the value is a number, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_number(&self) -> Option<&T::Number> {
		match self {
			Self::Number(n) => Some(n),
			_ => None,
		}
	}

	/// If the value is a number, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn into_number(self) -> Option<&'a T::Number> {
		match self {
			Self::Number(n) => Some(n),
			_ => None,
		}
	}

	/// If the value is a string, returns its associated [`str`].
	/// Returns `None` otherwise.
	pub fn as_str(&self) -> Option<&str> {
		match self {
			Self::String(s) => Some(s.as_ref()),
			_ => None,
		}
	}

	/// If the value is an array, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_array(&self) -> Option<&T::Array> {
		match self {
			Self::Array(a) => Some(a),
			_ => None,
		}
	}

	/// If the value is an array, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn into_array(self) -> Option<&'a T::Array> {
		match self {
			Self::Array(a) => Some(a),
			_ => None,
		}
	}

	/// If the value is an object, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn as_object(&self) -> Option<&T::Object> {
		match self {
			Self::Object(o) => Some(o),
			_ => None,
		}
	}

	/// If the value is an object, returns a reference to it.
	/// Returns `None` otherwise.
	pub fn into_object(self) -> Option<&'a T::Object> {
		match self {
			Self::Object(o) => Some(o),
			_ => None,
		}
	}

	/// If the value is an array, returns a mutable reference to it.
	/// Returns `None` otherwise.
	pub fn as_array_mut(&mut self) -> Option<&mut T::Array> {
		match self {
			Self::Array(a) => Some(a),
			_ => None,
		}
	}

	/// If the value is an array, returns a mutable reference to it.
	/// Returns `None` otherwise.
	pub fn into_array_mut(self) -> Option<&'a mut T::Array> {
		match self {
			Self::Array(a) => Some(a),
			_ => None,
		}
	}

	/// If the value is an object, returns a mutable reference to it.
	/// Returns `None` otherwise.
	pub fn as_object_mut(&mut self) -> Option<&mut T::Object> {
		match self {
			Self::Object(o) => Some(o),
			_ => None,
		}
	}

	/// If the value is an object, returns a mutable reference to it.
	/// Returns `None` otherwise.
	pub fn into_object_mut(self) -> Option<&'a mut T::Object> {
		match self {
			Self::Object(o) => Some(o),
			_ => None,
		}
	}

	/// Creates a new value by cloning the referenced value.
	pub fn cloned(&self) -> Value<T>
	where
		T::Number: Clone,
		T::String: Clone,
		T::Array: Clone,
		T::Object: Clone,
	{
		match self {
			Self::Null => Value::Null,
			Self::Boolean(b) => Value::Boolean(*b),
			Self::Number(n) => Value::Number((*n).clone()),
			Self::String(s) => Value::String((*s).clone()),
			Self::Array(a) => Value::Array((*a).clone()),
			Self::Object(o) => Value::Object((*o).clone()),
		}
	}
}