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
use getter::Getter; use number::Number; use std::collections::HashMap; use std::cmp; use std::fmt; use std::str; #[derive(PartialEq, Clone)] pub enum Value { String(String), Number(Number), Object(String), Array(String), Boolean(bool), Null, NotExist, } impl fmt::Debug for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Value::NotExist => write!(f, "<NOT EXSIT>"), Value::String(_) => write!(f, r#""{}""#, self.as_str()), _ => write!(f, "{}", self.as_str()), } } } impl fmt::Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.as_str()) } } impl Value { pub fn get(&self, path: &str) -> Value { self.get_by_utf8(&path.as_bytes()) } pub fn get_by_utf8(&self, v: &[u8]) -> Value { match self { Value::Array(s) | Value::Object(s) => Getter::from_str(s).get_by_utf8(v), _ => Value::NotExist, } } } impl Value { pub fn exists(&self) -> bool { *self != Value::NotExist } pub fn is_string(&self) -> bool { match self { Value::String(_) => true, _ => false, } } pub fn is_number(&self) -> bool { match self { Value::Number(_) => true, _ => false, } } pub fn is_array(&self) -> bool { match self { Value::Array(_) => true, _ => false, } } pub fn is_object(&self) -> bool { match self { Value::Object(_) => true, _ => false, } } pub fn is_bool(&self) -> bool { match self { Value::Boolean(_) => true, _ => false, } } pub fn is_null(&self) -> bool { match self { Value::Null => true, _ => false, } } } impl Value { pub fn as_str(&self) -> &str { match &self { Value::String(ref s) => s, Value::Number(number) => number.as_str(), Value::Boolean(true) => "true", Value::Boolean(false) => "false", Value::Object(ref s) => s, Value::Array(ref s) => s, Value::NotExist => "", Value::Null => "null", } } pub fn as_f64(&self) -> f64 { match self { Value::Number(number) => number.as_f64(), Value::Boolean(true) => 1.0, Value::String(s) => Number::from(s.as_bytes()).as_f64(), _ => 0.0, } } pub fn as_u64(&self) -> u64 { match self { Value::Number(number) => number.as_u64(), Value::Boolean(true) => 1, Value::String(s) => Number::from(s.as_bytes()).as_u64(), _ => 0, } } pub fn as_i64(&self) -> i64 { match self { Value::Number(number) => number.as_i64(), Value::Boolean(true) => 1, Value::String(ref s) => Number::from(s.as_bytes()).as_i64(), _ => 0, } } pub fn as_bool(&self) -> bool { match *self { Value::Boolean(b) => b, _ => false, } } pub fn as_array(&self) -> Vec<Value> { match self { Value::Array(s) => Getter::from_str(s).as_array(), Value::Null | Value::NotExist => vec![], _ => vec![self.clone()], } } pub fn as_map(&self) -> HashMap<String, Value> { match self { Value::Object(s) => Getter::from_str(s).as_map(), _ => HashMap::new(), } } } impl cmp::PartialEq<&str> for Value { fn eq(&self, other: &&str) -> bool { self.as_str() == *other } } impl cmp::PartialEq<f64> for Value { fn eq(&self, other: &f64) -> bool { self.as_f64() == *other } }