From 5eedc8e9080afe1d7fa00d9577343fe5aa25a93d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 22 Jun 2026 09:22:25 -0700 Subject: Attempted fixes. Maybe made it too C++ish --- src/context.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 8 +++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index 4501a0a..0b51677 100644 --- a/src/context.rs +++ b/src/context.rs @@ -40,6 +40,33 @@ impl<'a> MappedList<'a> for Vec { } } +impl<'a> MappedList<'a> for Vec> { + fn get_index(&'a self, id: usize) -> Option> { + if let Some(x) = self.get(id) { + Some(*x) + } 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( i, &mut buf )?; + } + Ok(buf) + } +} + +pub type MappedHash<'a> = HashMap>; + +impl<'a> MappedStructure<'a> for MappedHash<'a> { + fn get_value(&'a self, id: &str) -> Option> { + self.get(id).copied() + } +} + +#[derive(Copy,Clone)] pub enum MappedValue<'a> { Struct(&'a dyn MappedStructure<'a>), List(&'a dyn MappedList<'a>), diff --git a/src/lib.rs b/src/lib.rs index 9867615..162c30c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ mod lexer; mod parser; mod position; mod error; -mod context; +pub mod context; pub use error::{CrimError,CrimResult}; pub use position::*; @@ -213,10 +213,8 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) } Token::If(ident,code,other) => { if let Some(value) = input.get_value( &ident ) && - let MappedValue::Bool(b) = value && - b { - - exec(input, code, buf)? + let MappedValue::Bool(b) = value && b { + exec(input, code, buf)? } else { exec(input, other, buf)? } -- cgit v1.2.3