diff options
| author | Mike Buland <mike@xagasoft.com> | 2026-08-19 10:06:25 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2026-08-19 10:06:25 -0700 |
| commit | 5602585e7afe431f25e68f30ac54761db2a710e5 (patch) | |
| tree | 94461a3d5eba1ce0a6402e2c07cdfc9facf21d76 /deepcrow/src/context.rs | |
| parent | ba96bd8a4a1583edd3677dd613c89504d9ce88cf (diff) | |
| download | deepcrow-5602585e7afe431f25e68f30ac54761db2a710e5.tar.gz deepcrow-5602585e7afe431f25e68f30ac54761db2a710e5.tar.bz2 deepcrow-5602585e7afe431f25e68f30ac54761db2a710e5.tar.xz deepcrow-5602585e7afe431f25e68f30ac54761db2a710e5.zip | |
Renamed to DeepCrow
Diffstat (limited to 'deepcrow/src/context.rs')
| -rw-r--r-- | deepcrow/src/context.rs | 334 |
1 files changed, 334 insertions, 0 deletions
diff --git a/deepcrow/src/context.rs b/deepcrow/src/context.rs new file mode 100644 index 0000000..4a22cdd --- /dev/null +++ b/deepcrow/src/context.rs | |||
| @@ -0,0 +1,334 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | use std::fmt; | ||
| 3 | |||
| 4 | use crate::error::Result; | ||
| 5 | |||
| 6 | use crate::{Identifier,IdentifierValue}; | ||
| 7 | |||
| 8 | pub type Context = HashMap<String,Value>; | ||
| 9 | |||
| 10 | pub trait MappedStructure<'a> { | ||
| 11 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>>; | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<'a> MappedStructure<'a> for Context { | ||
| 15 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> { | ||
| 16 | self.get(id).map(|v|Into::<MappedValue<'a>>::into(v)) | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | pub trait MappedList<'a> { | ||
| 21 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>>; | ||
| 22 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> Result<()>) -> Result<String>; | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<'a, T: MappedStructure<'a>> MappedList<'a> for Vec<T> { | ||
| 26 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>> { | ||
| 27 | if let Some(x) = self.get(id) { | ||
| 28 | Some(MappedValue::<'a>::Struct(x)) | ||
| 29 | } else { | ||
| 30 | None | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> Result<()>) -> Result<String> { | ||
| 35 | let mut buf = String::new(); | ||
| 36 | for i in self { | ||
| 37 | func( &MappedValue::Struct(i), &mut buf )?; | ||
| 38 | } | ||
| 39 | Ok(buf) | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | impl<'a> MappedList<'a> for Vec<Value> { | ||
| 44 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>> { | ||
| 45 | if let Some(x) = self.get(id) { | ||
| 46 | Some(x.into()) | ||
| 47 | } else { | ||
| 48 | None | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> Result<()>) -> Result<String> { | ||
| 53 | let mut buf = String::new(); | ||
| 54 | for i in self { | ||
| 55 | func( &Into::<MappedValue<'a>>::into( i ), &mut buf )?; | ||
| 56 | } | ||
| 57 | Ok(buf) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | impl<'a> MappedList<'a> for Vec<MappedValue<'a>> { | ||
| 62 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>> { | ||
| 63 | if let Some(x) = self.get(id) { | ||
| 64 | Some(*x) | ||
| 65 | } else { | ||
| 66 | None | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> Result<()>) -> Result<String> { | ||
| 71 | let mut buf = String::new(); | ||
| 72 | for i in self { | ||
| 73 | func( i, &mut buf )?; | ||
| 74 | } | ||
| 75 | Ok(buf) | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | pub type MappedHash<'a> = HashMap<String, MappedValue<'a>>; | ||
| 80 | |||
| 81 | impl<'a> MappedStructure<'a> for MappedHash<'a> { | ||
| 82 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> { | ||
| 83 | self.get(id).copied() | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | #[derive(Copy,Clone)] | ||
| 88 | pub enum MappedValue<'a> { | ||
| 89 | Struct(&'a dyn MappedStructure<'a>), | ||
| 90 | List(&'a dyn MappedList<'a>), | ||
| 91 | |||
| 92 | Str(&'a str), | ||
| 93 | String(&'a String), | ||
| 94 | Int8(i8), | ||
| 95 | Int16(i16), | ||
| 96 | Int32(i32), | ||
| 97 | Int64(i64), | ||
| 98 | UInt8(u8), | ||
| 99 | UInt16(u16), | ||
| 100 | UInt32(u32), | ||
| 101 | UInt64(u64), | ||
| 102 | Float32(f32), | ||
| 103 | Float64(f64), | ||
| 104 | Bool(bool), | ||
| 105 | } | ||
| 106 | |||
| 107 | impl<'a> fmt::Debug for MappedValue<'a> { | ||
| 108 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 109 | match self { | ||
| 110 | MappedValue::<'a>::Struct(_) => write!(f, "MappedValue"), | ||
| 111 | MappedValue::<'a>::List(_) => write!(f, "MappedList"), | ||
| 112 | MappedValue::<'a>::Str(v) => write!(f, "{:?}", v), | ||
| 113 | MappedValue::<'a>::String(v) => write!(f, "{:?}", v), | ||
| 114 | MappedValue::<'a>::Int8(v) => write!(f, "{:?}", v), | ||
| 115 | MappedValue::<'a>::Int16(v) => write!(f, "{:?}", v), | ||
| 116 | MappedValue::<'a>::Int32(v) => write!(f, "{:?}", v), | ||
| 117 | MappedValue::<'a>::Int64(v) => write!(f, "{:?}", v), | ||
| 118 | MappedValue::<'a>::UInt8(v) => write!(f, "{:?}", v), | ||
| 119 | MappedValue::<'a>::UInt16(v) => write!(f, "{:?}", v), | ||
| 120 | MappedValue::<'a>::UInt32(v) => write!(f, "{:?}", v), | ||
| 121 | MappedValue::<'a>::UInt64(v) => write!(f, "{:?}", v), | ||
| 122 | MappedValue::<'a>::Float32(v) => write!(f, "{:?}", v), | ||
| 123 | MappedValue::<'a>::Float64(v) => write!(f, "{:?}", v), | ||
| 124 | MappedValue::<'a>::Bool(v) => write!(f, "{:?}", v), | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | macro_rules! basic_mapped_value_from { | ||
| 130 | ( $x:ident, $y:ident ) => { | ||
| 131 | impl<'a> From<&$x> for MappedValue<'a> { | ||
| 132 | fn from(val: &$x) -> MappedValue<'a> { | ||
| 133 | MappedValue::$y(*val) | ||
| 134 | } | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | |||
| 139 | basic_mapped_value_from!(i8, Int8); | ||
| 140 | basic_mapped_value_from!(i16, Int16); | ||
| 141 | basic_mapped_value_from!(i32, Int32); | ||
| 142 | basic_mapped_value_from!(i64, Int64); | ||
| 143 | basic_mapped_value_from!(u8, UInt8); | ||
| 144 | basic_mapped_value_from!(u16, UInt16); | ||
| 145 | basic_mapped_value_from!(u32, UInt32); | ||
| 146 | basic_mapped_value_from!(u64, UInt64); | ||
| 147 | basic_mapped_value_from!(f32, Float32); | ||
| 148 | basic_mapped_value_from!(f64, Float64); | ||
| 149 | basic_mapped_value_from!(bool, Bool); | ||
| 150 | |||
| 151 | impl<'a> MappedStructure<'a> for HashMap<String,String> { | ||
| 152 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> { | ||
| 153 | self.get(id).map(|x|MappedValue::String(x)) | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | impl<'a> From<&'a String> for MappedValue<'a> { | ||
| 158 | fn from(val: &'a String ) -> MappedValue<'a> { | ||
| 159 | MappedValue::String(val) | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | impl<'a, T: MappedStructure<'a>> From<&'a Vec<T>> for MappedValue<'a> { | ||
| 164 | fn from(val: &'a Vec<T> ) -> MappedValue<'a> { | ||
| 165 | MappedValue::List(val) | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | impl<'a,T: MappedStructure<'a>> From<&'a T> for MappedValue<'a> { | ||
| 170 | fn from(val: &'a T ) -> MappedValue<'a> { | ||
| 171 | MappedValue::Struct(val) | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | pub struct StatefulContext<'a> { | ||
| 176 | root: &'a dyn MappedStructure<'a>, | ||
| 177 | local: &'a dyn MappedStructure<'a>, | ||
| 178 | } | ||
| 179 | |||
| 180 | impl<'a> StatefulContext<'a> { | ||
| 181 | pub fn root( root: &'a dyn MappedStructure<'a> ) -> Self { | ||
| 182 | Self { | ||
| 183 | root, local: root, | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | pub fn local(&self, local: &'a dyn MappedStructure<'a> ) -> Self { | ||
| 188 | Self { | ||
| 189 | root: self.root, | ||
| 190 | local | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | pub fn full( root: &'a dyn MappedStructure<'a>, local: &'a dyn MappedStructure<'a> ) -> Self { | ||
| 195 | Self { | ||
| 196 | root, local | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | pub fn get_value(&self, id: &[IdentifierValue]) -> Option<MappedValue<'a>> { | ||
| 201 | let mut value : Option<MappedValue> = | ||
| 202 | if id[0] == IdentifierValue::Root { | ||
| 203 | Some(MappedValue::Struct(self.root)) | ||
| 204 | } else { | ||
| 205 | Some(MappedValue::Struct(self.local)) | ||
| 206 | }; | ||
| 207 | for idpart in id { | ||
| 208 | match idpart { | ||
| 209 | IdentifierValue::Root => { | ||
| 210 | continue; | ||
| 211 | } | ||
| 212 | IdentifierValue::Name(name) => { | ||
| 213 | if let Some(MappedValue::Struct(s)) = value && | ||
| 214 | let Some(nv) = s.get_value( &name ) { | ||
| 215 | value.replace( nv ); | ||
| 216 | } else { | ||
| 217 | return None; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | IdentifierValue::Index(_) => { | ||
| 221 | return None; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | value | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | impl<'a> From<&'a Value> for MappedValue<'a> { | ||
| 230 | fn from(value: &'a Value) -> Self { | ||
| 231 | match value { | ||
| 232 | Value::Dictionary(ctx) => MappedValue::Struct(ctx), | ||
| 233 | Value::List(list) => MappedValue::List(list), | ||
| 234 | Value::String(s) => MappedValue::String(s), | ||
| 235 | Value::Int(i) => MappedValue::Int64(*i), | ||
| 236 | Value::Float(f) => MappedValue::Float64(*f), | ||
| 237 | Value::Bool(b) => MappedValue::Bool(*b), | ||
| 238 | Value::Identifier(_identifier) => panic!("Identifier!?"), | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | #[derive(Debug,Clone)] | ||
| 244 | pub enum Value { | ||
| 245 | Dictionary(Context), | ||
| 246 | List(Vec<Value>), | ||
| 247 | String(String), | ||
| 248 | Int(i64), | ||
| 249 | Float(f64), | ||
| 250 | Bool(bool), | ||
| 251 | Identifier(Identifier), | ||
| 252 | } | ||
| 253 | |||
| 254 | impl From<&str> for Value { | ||
| 255 | fn from(val: &str ) -> Value { | ||
| 256 | Value::String(val.into()) | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | impl From<String> for Value { | ||
| 261 | fn from(val: String) -> Value { | ||
| 262 | Value::String(val) | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | impl From<i64> for Value { | ||
| 267 | fn from(val: i64) -> Value { | ||
| 268 | Value::Int(val) | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | impl From<f64> for Value { | ||
| 273 | fn from(val: f64) -> Value { | ||
| 274 | Value::Float(val) | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | impl From<bool> for Value { | ||
| 279 | fn from(val: bool) -> Value { | ||
| 280 | Value::Bool(val) | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | impl From<Vec<Value>> for Value { | ||
| 285 | fn from(val: Vec<Value>) -> Value { | ||
| 286 | Value::List(val) | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | impl From<&[Value]> for Value { | ||
| 291 | fn from(val: &[Value]) -> Value { | ||
| 292 | Value::List(Vec::from(val)) | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | impl From<Context> for Value { | ||
| 297 | fn from(val: Context) -> Value { | ||
| 298 | Value::Dictionary(val) | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | impl<const N: usize> From<[(String,Value); N]> for Value { | ||
| 303 | fn from(val: [(String,Value); N]) -> Value { | ||
| 304 | Value::Dictionary(HashMap::from(val)) | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | /* | ||
| 309 | impl MappedStructure for Value { | ||
| 310 | fn get_value(&self, id: &Identifier) -> Option<Value> { | ||
| 311 | if id.len() == 0 { | ||
| 312 | return Some(self); | ||
| 313 | } | ||
| 314 | match Value { | ||
| 315 | Value::Dictionary(dict) => { | ||
| 316 | } | ||
| 317 | } | ||
| 318 | let mut cur_val : Option<&Value> = Some(&self); | ||
| 319 | |||
| 320 | for idx in 0..id.len() { | ||
| 321 | if let IdentifierValue::Name(s) = &id[idx] && | ||
| 322 | let Some(Value::Dictionary(d)) = &cur_val { | ||
| 323 | cur_val.replace( if let Some(v) = d.get(s) { | ||
| 324 | v | ||
| 325 | } else { | ||
| 326 | return None; | ||
| 327 | }); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | return cur_val.cloned(); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | */ | ||
