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
use crate::value::{AeonValue}; use crate::object::{AeonObject}; use std::collections::HashMap; pub trait AeonConvert { fn nil(self) -> bool; fn bool(self) -> Option<bool>; fn str(self) -> Option<String>; fn int(self) -> Option<i64>; fn double(self) -> Option<f64>; fn map(self) -> Option<HashMap<String,AeonValue>>; fn list(self) -> Option<Vec<AeonValue>>; fn get(&self, prop: &str) -> Option<AeonValue>; fn remove(&mut self, prop: &str) -> Option<AeonValue>; } macro_rules! try_convert( ($self:ident, $to:path) => { match $self { $to(v) => Some(v), _ => None, } }; ); impl AeonConvert for AeonValue { fn nil(self) -> bool { match self { AeonValue::Nil => true, _ => false, } } fn bool(self) -> Option<bool> { try_convert!(self, AeonValue::Bool) } fn str(self) -> Option<String> { try_convert!(self, AeonValue::String) } fn int(self) -> Option<i64> { try_convert!(self, AeonValue::Integer) } fn double(self) -> Option<f64> { try_convert!(self, AeonValue::Double) } fn map(self) -> Option<HashMap<String,AeonValue>> { try_convert!(self, AeonValue::Map) } fn list(self) -> Option<Vec<AeonValue>> { try_convert!(self, AeonValue::List) } fn get(&self, prop: &str) -> Option<AeonValue> { match self { AeonValue::Map(v) => { if let Some(p) = v.get(prop) { Some(p.clone()) } else { None } } _ => None, } } fn remove(&mut self, prop: &str) -> Option<AeonValue> { match self { AeonValue::Map(v) => { if let Some(p) = v.remove(prop) { Some(p) } else { None } } _ => None, } } } macro_rules! opt_convert( ($self:ident, $conversion:ident) => { if let Some(a) = $self { a.$conversion() } else { None } }; ($self:ident, $conversion:ident, $arg:ident) => { if let Some(a) = $self { a.$conversion($arg) } else { None } }; ); impl AeonConvert for Option<AeonValue> { fn nil(self) -> bool { if let Some(a) = self { a.nil() } else { false } } fn bool(self) -> Option<bool> { opt_convert!(self, bool) } fn str(self) -> Option<String> { opt_convert!(self, str) } fn int(self) -> Option<i64> { opt_convert!(self, int) } fn double(self) -> Option<f64> { opt_convert!(self, double) } fn map(self) -> Option<HashMap<String,AeonValue>> { opt_convert!(self, map) } fn list(self) -> Option<Vec<AeonValue>> { opt_convert!(self, list) } fn get(&self, prop: &str) -> Option<AeonValue> { opt_convert!(self, get, prop) } fn remove(&mut self, prop: &str) -> Option<AeonValue> { opt_convert!(self, remove, prop) } } pub trait AeonObjectConvert { fn get(&self, prop: &str) -> Option<AeonValue>; fn get_path(&self, path: &str) -> Option<AeonValue>; fn remove(&mut self, prop: &str) -> Option<AeonValue>; fn remove_path(&mut self, path: &str) -> Option<AeonValue>; } impl AeonObjectConvert for AeonObject { fn get(&self, prop: &str) -> Option<AeonValue> { return if let Some(p) = self.properties.get(prop) { Some(p.value.clone()) } else { None } } fn get_path(&self, path: &str) -> Option<AeonValue> { let fragments = path.split('/'); let mut iter = fragments.filter(|&f| f != ""); let mut current : Option<AeonValue>; if let Some(frag) = iter.next() { current = self.get(frag); } else { return None; } while let Some(frag) = iter.next() { current = current.get(frag); } current } fn remove(&mut self, prop: &str) -> Option<AeonValue> { return if let Some(p) = self.properties.remove(prop) { Some(p.value) } else { None } } fn remove_path(&mut self, path: &str) -> Option<AeonValue> { let fragments = path.split('/'); let mut iter = fragments.filter(|&f| f != ""); let mut current : Option<AeonValue>; if let Some(frag) = iter.next() { current = self.remove(frag); } else { return None; } while let Some(frag) = iter.next() { current = current.remove(frag); } current } }