From dd2f66f2fe7ee687d8c0b7ca6e60ef7221223fb2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 19 Jun 2026 23:04:37 -0700 Subject: Just cleaned up unsued functions. --- src/context.rs | 91 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 40 deletions(-) (limited to 'src/context.rs') diff --git a/src/context.rs b/src/context.rs index 4de5b45..4501a0a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; use std::fmt; +use crate::error::CrimResult; + use crate::{Identifier,IdentifierValue}; pub type Context = HashMap; @@ -10,42 +12,32 @@ pub trait MappedStructure<'a> { } 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<&MappedValue> = 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(MappedValue::Dictionary(d)) = &cur_val { - cur_val.replace( if let Some(v) = d.get(s) { - v - } else { - return None; - }); - } - } - - return cur_val.cloned(); - */ - - None + fn get_value(&'a self, id: &str) -> Option> { + self.get(id).map(|v|Into::>::into(v)) } } pub trait MappedList<'a> { fn get_index(&'a self, id: usize) -> Option>; -// fn get_iterator(&'a self) -> MappedListIterator<'a>; - fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>)); + fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult; +} + +impl<'a> MappedList<'a> for Vec { + fn get_index(&'a self, id: usize) -> Option> { + if let Some(x) = self.get(id) { + Some(x.into()) + } else { + None + } + } + + fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult { + let mut buf = String::new(); + for i in self { + func( &Into::>::into( i ), &mut buf )?; + } + Ok(buf) + } } pub enum MappedValue<'a> { @@ -90,37 +82,42 @@ impl<'a> fmt::Debug for MappedValue<'a> { } -pub struct StatefulContext<'a, T: MappedStructure<'a>> { - root: &'a T, - local: &'a T, +pub struct StatefulContext<'a> { + root: &'a dyn MappedStructure<'a>, + local: &'a dyn MappedStructure<'a>, } -impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> { - pub fn root( root: &'a T ) -> Self { +impl<'a> StatefulContext<'a> { + pub fn root( root: &'a dyn MappedStructure<'a> ) -> Self { Self { root, local: root, } } - pub fn local(&self, local: &'a T ) -> Self { + pub fn local(&self, local: &'a dyn MappedStructure<'a> ) -> Self { Self { root: self.root, local } } - pub fn full( root: &'a T, local: &'a T ) -> Self { + pub fn full( root: &'a dyn MappedStructure<'a>, local: &'a dyn MappedStructure<'a> ) -> Self { Self { root, local } } pub fn get_value(&self, id: &[IdentifierValue]) -> Option> { - let mut value : Option = Some(MappedValue::Struct(self.root)); + let mut value : Option = + if id[0] == IdentifierValue::Root { + Some(MappedValue::Struct(self.root)) + } else { + Some(MappedValue::Struct(self.local)) + }; for idpart in id { match idpart { IdentifierValue::Root => { - return None + continue; } IdentifierValue::Name(name) => { if let Some(MappedValue::Struct(s)) = value && @@ -139,6 +136,20 @@ impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> { } } +impl<'a> From<&'a Value> for MappedValue<'a> { + fn from(value: &'a Value) -> Self { + match value { + Value::Dictionary(ctx) => MappedValue::Struct(ctx), + Value::List(list) => MappedValue::List(list), + Value::String(s) => MappedValue::String(s), + Value::Int(i) => MappedValue::Int64(*i), + Value::Float(f) => MappedValue::Float64(*f), + Value::Bool(b) => MappedValue::Bool(*b), + Value::Identifier(_identifier) => panic!("Identifier!?"), + } + } +} + #[derive(Debug,Clone)] pub enum Value { Dictionary(Context), -- cgit v1.2.3