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
use crate::{Json, Value};

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

/// Mutable JSON value reference.
pub enum ValueMut<'a, T: Json> {
    Null,
    Boolean(&'a mut 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::Number(_))
				}

				/// 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 `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 its associated [`str`].
    /// Returns `None` otherwise.
    pub fn as_str(&self) -> Option<&'a str> {
        match self {
            Self::String(s) => Some(*s),
            _ => 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: From<&'a str>,
        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).into()),
            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 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 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 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 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,
        }
    }

    /// 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()),
        }
    }
}