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 ++++++++++++++++++++++++++++++++-------------------------- src/lexer.rs | 2 ++ src/lib.rs | 53 ++++++++++++++++++++++++---------- src/parser.rs | 23 ++++++++------- 4 files changed, 104 insertions(+), 65 deletions(-) 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), diff --git a/src/lexer.rs b/src/lexer.rs index 4e95854..d4d39e9 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -66,9 +66,11 @@ impl<'a> Symbol<'a> { &self.start } + /* pub fn end(&self) -> &Position { &self.end } + */ } #[derive(Debug)] diff --git a/src/lib.rs b/src/lib.rs index 95462b4..9867615 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,12 +41,12 @@ struct View { } impl View { - fn process MappedStructure<'a>>(&self, input: &T) -> CrimResult { + fn process(&self, input: &dyn for<'a> MappedStructure<'a>) -> CrimResult { let mut out_vars = Context::new(); for output in &self.outputs { let mut buf = String::new(); let sc = StatefulContext::root( input ); - exec::( &sc, &output.code, &mut buf )?; + exec( &sc, &output.code, &mut buf )?; out_vars.insert( output.name.clone(), Value::String(buf) ); } Ok(out_vars) @@ -163,7 +163,7 @@ impl Crimtag { } } - pub fn render_partial MappedStructure<'a>>(&self, view: &str, input: &T ) -> CrimResult { + pub fn render_partial(&self, view: &str, input: &dyn for<'a> MappedStructure<'a> ) -> CrimResult { if let Some(view) = self.views.get(view) { return view.process( input ) } else { @@ -171,7 +171,7 @@ impl Crimtag { } } - pub fn render MappedStructure<'a>>(&self, view: &str, input: &T ) -> CrimResult { + pub fn render(&self, view: &str, input: &dyn for<'a> MappedStructure<'a> ) -> CrimResult { if let Some(view) = self.views.get( view ) { //println!("::Token tree::\n{:?}", view ); let mut output = view.process( input )?; @@ -193,19 +193,23 @@ impl Crimtag { } } -fn exec MappedStructure<'a>>(input: &StatefulContext, tokens: &Vec, buf: &mut String) -> CrimResult<()> { +fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) -> CrimResult<()> { for token in tokens { match token { Token::Loop(ident,code) => { + //println!("!!! Loop over: {:?} {:?}", ident, input.get_value( &ident )); if let Some(value) = input.get_value( &ident ) && let MappedValue::List(list) = value { - list.for_each(&|rec: &MappedValue| { - if let MappedValue::Struct(d) = rec { - exec( &StatefulContext::root(d), code, buf )? - } - }); + let output = + &list.for_each(&|rec: &MappedValue, buf: &mut String| { + if let MappedValue::Struct(d) = rec { + exec( &input.local(*d), code, buf )?; + } + Ok(()) + })?; + //println!("!!! Output from loop: {}", output ); + buf.push_str( &output ); } - return Ok(()); } Token::If(ident,code,other) => { if let Some(value) = input.get_value( &ident ) && @@ -360,7 +364,7 @@ Now with explicit outputs: let mut ct = Crimtag::new(); ct.load_static(r#"Looping code: [|view "index"|>We will enumerate people here:[|loop people|> - - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|else|> **title redacted** <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] + - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|else|> **title redacted** <|if|] ::>[|loop tags|> [|show tag|]<|loop|] <:: <|loop|] <|view|] "#)?; @@ -371,24 +375,40 @@ Now with explicit outputs: ("last_name".into(), "Smith".into()), ("show_title".into(), true.into()), ("title".into(), "CEO".into()), + ("tags".into(), vec![ + Context::from([("tag".into(), "jerk".into()),]).into(), + Context::from([("tag".into(), "ugly".into()),]).into(), + ].into()), ].into(), [ ("first_name".into(), "Chris".into()), ("last_name".into(), "Perkens".into()), ("show_title".into(), false.into()), ("title".into(), "Baconeer".into()), + ("tags".into(), vec![ + Context::from([("tag".into(), "jerk".into()),]).into(), + Context::from([("tag".into(), "ugly".into()),]).into(), + ].into()), ].into(), [ ("first_name".into(), "Will".into()), ("last_name".into(), "Power".into()), ("show_title".into(), false.into()), ("title".into(), "CFO".into()), + ("tags".into(), vec![ + Context::from([("tag".into(), "jerk".into()),]).into(), + Context::from([("tag".into(), "ugly".into()),]).into(), + ].into()), ].into(), [ ("first_name".into(), "Justin".into()), ("last_name".into(), "Time".into()), ("show_title".into(), true.into()), ("title".into(), "CIO".into()), + ("tags".into(), vec![ + Context::from([("tag".into(), "jerk".into()),]).into(), + Context::from([("tag".into(), "ugly".into()),]).into(), + ].into()), ].into(), ].into()), ]); @@ -513,10 +533,12 @@ Now with explicit outputs: } } - fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>)) { + fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult { + let mut buf = String::new(); for i in self { - func( &MappedValue::Struct(i) ); + func( &MappedValue::Struct(i), &mut buf )?; } + Ok(buf) } } @@ -529,10 +551,11 @@ Now with explicit outputs: ); if let Some(MappedValue::List(l)) = page.get_value(&"items") { - l.for_each(&|x: &MappedValue| { + l.for_each(&|x: &MappedValue, buf: &mut String| { if let MappedValue::Struct(s) = x { println!(" - {:?}", s.get_value(&"title")); } + Ok(()) }); } Ok(()) diff --git a/src/parser.rs b/src/parser.rs index d1fe4d7..4f7ff6a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -35,6 +35,7 @@ impl<'a> Context<'a> { self.cur[(self.icur+1)%2] } + /* pub fn check_unwrapped bool>(&self, context: &str, f: T) -> CrimResult { if self.cur().is_none() || self.peek().is_none() { Err(CrimError::eos( context )) @@ -42,6 +43,7 @@ impl<'a> Context<'a> { Ok(f( self.cur().unwrap().symbol(), self.peek().unwrap().symbol())) } } + */ pub fn source(&self) -> usize { self.source @@ -50,6 +52,7 @@ impl<'a> Context<'a> { trait SymbolHelper { fn is bool>(&self, context: &str, f: T ) -> CrimResult; + #[allow(dead_code)] fn is_valid_tag_name(&self) -> CrimResult; } @@ -572,11 +575,11 @@ impl<'a> TagBuilder<'a> { pub fn is_unary(&self) -> bool { self.tag_type == TagType::Unary } - +/* pub fn is_multinary_open(&self) -> bool { self.tag_type == TagType::MultinaryOpen } - +*/ pub fn can_have_params(&self) -> bool { match self.name.unwrap().symbol() { SymbolType::View | SymbolType::Output | SymbolType::Loop => true, @@ -598,7 +601,7 @@ impl<'a> TagBuilder<'a> { pub fn can_have_props(&self) -> bool { true } - +/* pub fn can_have_children(&self) -> bool { if self.tag_type == TagType::MultinaryOpen || self.tag_type == TagType::MultinaryMid { @@ -607,7 +610,7 @@ impl<'a> TagBuilder<'a> { false } } - +*/ pub fn is_name(&self, name: &SymbolType<'a>) -> bool { if let Some(a) = self.name { a.symbol() == name @@ -619,11 +622,11 @@ impl<'a> TagBuilder<'a> { pub fn name(&self) -> Option> { self.name } - +/* pub fn params(&self) -> &Vec { &self.params } - +*/ pub fn set_name(&mut self, name: Symbol<'a>) { self.name = Some(name); } @@ -639,19 +642,19 @@ impl<'a> TagBuilder<'a> { pub fn add_child(&mut self, tb: Entry<'a>) { self.children.push( tb ); } - +/* pub fn append_children(&mut self, children: &mut Vec::>) { self.children.append( children ); } - +*/ pub fn add_chain(&mut self, tb: Entry<'a>) { self.chain.push( tb ); } - +/* pub fn set_type(&mut self, tag_type: TagType) { self.tag_type = tag_type; } - +*/ pub fn tag_type(&self) -> TagType { self.tag_type } -- cgit v1.2.3