From 00ffb6d11ed3a1d6508cbf065679b6f04673f238 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 17 Jun 2026 00:05:37 -0700 Subject: I think this is a better structure. We deal with mapped structures better now, and they are distinct from our handy value tree. I haven't figured out how to make it like iterators yet, but I think we're close... --- src/context.rs | 136 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 76 insertions(+), 60 deletions(-) (limited to 'src/context.rs') diff --git a/src/context.rs b/src/context.rs index 2f12ea9..83f8539 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,23 +1,21 @@ use std::collections::HashMap; +use std::fmt; use crate::{Identifier,IdentifierValue}; pub type Context = HashMap; -pub trait MappedStructure { - fn get_value(&self, id: &[IdentifierValue]) -> Option; - fn get_iterable(&self, id: &[IdentifierValue]) -> Option - where - T: IntoIterator, - T::Item: MappedStructure; +pub trait MappedStructure<'a> { + fn get_value(&'a self, id: &str) -> Option>; } -impl MappedStructure for Context { - fn get_value(&self, id: &[IdentifierValue]) -> Option { +impl<'a> MappedStructure<'a> for Context { + fn get_value(&'a self, _id: &str) -> Option> { + /* if id.len() == 0 { return None; } - let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] { + let mut cur_val : Option<&MappedValue> = if let IdentifierValue::Name(s) = &id[0] { self.get(s) } else { return None; @@ -28,7 +26,7 @@ impl MappedStructure for Context { for idx in 1..id.len() { if let IdentifierValue::Name(s) = &id[idx] && - let Some(Value::Dictionary(d)) = &cur_val { + let Some(MappedValue::Dictionary(d)) = &cur_val { cur_val.replace( if let Some(v) = d.get(s) { v } else { @@ -38,49 +36,64 @@ impl MappedStructure for Context { } return cur_val.cloned(); + */ + + None } - - fn get_iter(&self, id: &[IdentifierValue]) -> Option - where - T: IntoIterator, - T::Item: MappedStructure, { - if id.len() == 0 { - return None; - } - let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] { - self.get(s) - } else { - return None; - }; - if cur_val.is_none() { - return None; - } +} - for idx in 1..id.len() { - if let IdentifierValue::Name(s) = &id[idx] && - let Some(Value::Dictionary(d)) = &cur_val { - cur_val.replace( if let Some(v) = d.get(s) { - v - } else { - return None; - }); - } - } +pub trait MappedList<'a> { + fn get_index(&'a self, id: usize) -> Option>; +} - if let Some(Value::List(l)) = cur_val { - return l.into - } +pub enum MappedValue<'a> { + Struct(&'a dyn MappedStructure<'a>), + List(&'a dyn MappedList<'a>), + + Str(&'a str), + String(&'a String), + Int8(i8), + Int16(i16), + Int32(i32), + Int64(i64), + UInt8(u8), + UInt16(u16), + UInt32(u32), + UInt64(u64), + Float32(f32), + Float64(f64), + Bool(bool), +} - return cur_val.cloned(); +impl<'a> fmt::Debug for MappedValue<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + MappedValue::<'a>::Struct(_) => write!(f, "MappedValue"), + MappedValue::<'a>::List(_) => write!(f, "MappedList"), + MappedValue::<'a>::Str(v) => write!(f, "{:?}", v), + MappedValue::<'a>::String(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Int8(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Int16(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Int32(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Int64(v) => write!(f, "{:?}", v), + MappedValue::<'a>::UInt8(v) => write!(f, "{:?}", v), + MappedValue::<'a>::UInt16(v) => write!(f, "{:?}", v), + MappedValue::<'a>::UInt32(v) => write!(f, "{:?}", v), + MappedValue::<'a>::UInt64(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Float32(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Float64(v) => write!(f, "{:?}", v), + MappedValue::<'a>::Bool(v) => write!(f, "{:?}", v), + } } + } -pub struct StatefulContext<'a, T: MappedStructure> { - pub root: &'a T, +pub struct StatefulContext<'a, T: MappedStructure<'a>> { + root: &'a T, local: &'a T, } -impl<'a, T: MappedStructure> StatefulContext<'a, T> { +impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> { pub fn root( root: &'a T ) -> Self { Self { root, local: root, @@ -99,25 +112,28 @@ impl<'a, T: MappedStructure> StatefulContext<'a, T> { root, local } } -} -impl<'a, T: MappedStructure> MappedStructure for StatefulContext<'a,T> { - fn get_value(&self, id: &[IdentifierValue]) -> Option { - if id.is_empty() { - return None; - } - if id[0] == IdentifierValue::Root { - self.root.get_value( &id[1..] ) - } else { - self.local.get_value( id ) + pub fn get_value(&self, id: &[IdentifierValue]) -> Option> { + let mut value : Option = Some(MappedValue::Struct(self.root)); + for idpart in id { + match idpart { + IdentifierValue::Root => { + return None + } + IdentifierValue::Name(name) => { + if let Some(MappedValue::Struct(s)) = value && + let Some(nv) = s.get_value( &name ) { + value.replace( nv ); + } else { + return None; + } + } + IdentifierValue::Index(_) => { + return None; + } + } } - } - - fn get_iter(&self, id: &[IdentifierValue]) -> Option - where - U: IntoIterator, - U::Item: MappedStructure, { - None + value } } -- cgit v1.2.3