|
| 1 | +use super::generator::{stringify, JsonGenerateResult}; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::convert::TryInto; |
| 4 | +use std::error; |
| 5 | +use std::fmt; |
| 6 | +use std::ops::{Index, IndexMut}; |
| 7 | + |
| 8 | +const NULL: () = (); |
| 9 | + |
| 10 | +#[derive(Debug, Clone, PartialEq)] |
| 11 | +pub enum JsonValue { |
| 12 | + Number(f64), |
| 13 | + Boolean(bool), |
| 14 | + String(String), |
| 15 | + Null, |
| 16 | + Array(Vec<JsonValue>), |
| 17 | + Object(HashMap<String, JsonValue>), |
| 18 | +} |
| 19 | + |
| 20 | +pub trait InnerAsRef { |
| 21 | + fn json_value_as(v: &JsonValue) -> Option<&Self>; |
| 22 | +} |
| 23 | + |
| 24 | +macro_rules! impl_inner_ref { |
| 25 | + ($to:ty, $pat:pat => $val:expr) => { |
| 26 | + impl InnerAsRef for $to { |
| 27 | + fn json_value_as(v: &JsonValue) -> Option<&$to> { |
| 28 | + use JsonValue::*; |
| 29 | + match v { |
| 30 | + $pat => Some($val), |
| 31 | + _ => None, |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +impl_inner_ref!(f64, Number(n) => n); |
| 39 | +impl_inner_ref!(bool, Boolean(b) => b); |
| 40 | +impl_inner_ref!(String, String(s) => s); |
| 41 | +impl_inner_ref!((), Null => &NULL); |
| 42 | +impl_inner_ref!(Vec<JsonValue>, Array(a) => a); |
| 43 | +impl_inner_ref!(HashMap<String, JsonValue>, Object(h) => h); |
| 44 | + |
| 45 | +pub trait InnerAsRefMut { |
| 46 | + fn json_value_as_mut(v: &mut JsonValue) -> Option<&mut Self>; |
| 47 | +} |
| 48 | + |
| 49 | +macro_rules! impl_inner_ref_mut { |
| 50 | + ($to:ty, $pat:pat => $val:expr) => { |
| 51 | + impl InnerAsRefMut for $to { |
| 52 | + fn json_value_as_mut(v: &mut JsonValue) -> Option<&mut $to> { |
| 53 | + use JsonValue::*; |
| 54 | + match v { |
| 55 | + $pat => Some($val), |
| 56 | + _ => None, |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +impl_inner_ref_mut!(f64, Number(n) => n); |
| 64 | +impl_inner_ref_mut!(bool, Boolean(b) => b); |
| 65 | +impl_inner_ref_mut!(String, String(s) => s); |
| 66 | +impl_inner_ref_mut!(Vec<JsonValue>, Array(a) => a); |
| 67 | +impl_inner_ref_mut!(HashMap<String, JsonValue>, Object(h) => h); |
| 68 | + |
| 69 | +// Note: matches! is available from Rust 1.42 |
| 70 | +macro_rules! is_xxx { |
| 71 | + ($name:ident, $variant:pat) => { |
| 72 | + pub fn $name(&self) -> bool { |
| 73 | + match self { |
| 74 | + $variant => true, |
| 75 | + _ => false, |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +impl JsonValue { |
| 82 | + pub fn get<T: InnerAsRef>(&self) -> Option<&T> { |
| 83 | + T::json_value_as(self) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn get_mut<T: InnerAsRefMut>(&mut self) -> Option<&mut T> { |
| 87 | + T::json_value_as_mut(self) |
| 88 | + } |
| 89 | + |
| 90 | + is_xxx!(is_bool, JsonValue::Boolean(_)); |
| 91 | + is_xxx!(is_number, JsonValue::Number(_)); |
| 92 | + is_xxx!(is_string, JsonValue::String(_)); |
| 93 | + is_xxx!(is_null, JsonValue::Null); |
| 94 | + is_xxx!(is_array, JsonValue::Array(_)); |
| 95 | + is_xxx!(is_object, JsonValue::Object(_)); |
| 96 | + |
| 97 | + pub fn stringify(&self) -> JsonGenerateResult { |
| 98 | + stringify(self) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<'a> Index<&'a str> for JsonValue { |
| 103 | + type Output = JsonValue; |
| 104 | + |
| 105 | + fn index(&self, key: &'a str) -> &Self::Output { |
| 106 | + let obj = match self { |
| 107 | + JsonValue::Object(o) => o, |
| 108 | + _ => panic!( |
| 109 | + "Attempted to access to an object with key '{}' but actually it was {:?}", |
| 110 | + key, self |
| 111 | + ), |
| 112 | + }; |
| 113 | + |
| 114 | + match obj.get(key) { |
| 115 | + Some(json) => json, |
| 116 | + None => panic!("Key '{}' was not found in {:?}", key, self), |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Index<usize> for JsonValue { |
| 122 | + type Output = JsonValue; |
| 123 | + |
| 124 | + fn index(&self, index: usize) -> &'_ Self::Output { |
| 125 | + let array = match self { |
| 126 | + JsonValue::Array(a) => a, |
| 127 | + _ => panic!( |
| 128 | + "Attempted to access to an array with index {} but actually the value was {:?}", |
| 129 | + index, self, |
| 130 | + ), |
| 131 | + }; |
| 132 | + &array[index] |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl<'a> IndexMut<&'a str> for JsonValue { |
| 137 | + fn index_mut(&mut self, key: &'a str) -> &mut Self::Output { |
| 138 | + let obj = match self { |
| 139 | + JsonValue::Object(o) => o, |
| 140 | + _ => panic!( |
| 141 | + "Attempted to access to an object with key '{}' but actually it was {:?}", |
| 142 | + key, self |
| 143 | + ), |
| 144 | + }; |
| 145 | + |
| 146 | + if let Some(json) = obj.get_mut(key) { |
| 147 | + json |
| 148 | + } else { |
| 149 | + panic!("Key '{}' was not found in object", key) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl IndexMut<usize> for JsonValue { |
| 155 | + fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 156 | + let array = match self { |
| 157 | + JsonValue::Array(a) => a, |
| 158 | + _ => panic!( |
| 159 | + "Attempted to access to an array with index {} but actually the value was {:?}", |
| 160 | + index, self, |
| 161 | + ), |
| 162 | + }; |
| 163 | + |
| 164 | + &mut array[index] |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +#[derive(Debug)] |
| 169 | +pub struct UnexpectedValue { |
| 170 | + value: JsonValue, |
| 171 | + expected: &'static str, |
| 172 | +} |
| 173 | + |
| 174 | +impl fmt::Display for UnexpectedValue { |
| 175 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 176 | + write!( |
| 177 | + f, |
| 178 | + "Unexpected JSON value: {:?}. Expected {} value", |
| 179 | + self.value, self.expected |
| 180 | + ) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +impl error::Error for UnexpectedValue {} |
| 185 | + |
| 186 | +macro_rules! impl_try_into { |
| 187 | + ($ty:ty, $pat:pat => $val:expr) => { |
| 188 | + impl TryInto<$ty> for JsonValue { |
| 189 | + type Error = UnexpectedValue; |
| 190 | + |
| 191 | + fn try_into(self) -> Result<$ty, UnexpectedValue> { |
| 192 | + match self { |
| 193 | + $pat => Ok($val), |
| 194 | + v => Err(UnexpectedValue { |
| 195 | + value: v, |
| 196 | + expected: stringify!($ty), |
| 197 | + }), |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +impl_try_into!(f64, JsonValue::Number(n) => n); |
| 205 | +impl_try_into!(bool, JsonValue::Boolean(b) => b); |
| 206 | +impl_try_into!(String, JsonValue::String(s) => s); |
| 207 | +impl_try_into!((), JsonValue::Null => ()); |
| 208 | +impl_try_into!(Vec<JsonValue>, JsonValue::Array(a) => a); |
| 209 | +impl_try_into!(HashMap<String, JsonValue>, JsonValue::Object(o) => o); |
0 commit comments