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/lib.rs | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 156 insertions(+), 18 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 362e017..eda2f31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ mod context; pub use error::{CrimError,CrimResult}; pub use position::*; -pub use context::{Context,Value,MappedStructure,StatefulContext}; +pub use context::{Context,Value,MappedStructure,StatefulContext,MappedValue,MappedList}; #[derive(Debug)] pub struct Crimtag { @@ -41,7 +41,7 @@ struct View { } impl View { - fn process(&self, input: &T) -> CrimResult { + fn process MappedStructure<'a>>(&self, input: &T) -> CrimResult { let mut out_vars = Context::new(); for output in &self.outputs { let mut buf = String::new(); @@ -163,7 +163,7 @@ impl Crimtag { } } - pub fn render_partial(&self, view: &str, input: &T ) -> CrimResult { + pub fn render_partial MappedStructure<'a>>(&self, view: &str, input: &T ) -> CrimResult { if let Some(view) = self.views.get(view) { return view.process( input ) } else { @@ -171,7 +171,7 @@ impl Crimtag { } } - pub fn render(&self, view: &str, input: &T ) -> CrimResult { + pub fn render MappedStructure<'a>>(&self, view: &str, input: &T ) -> CrimResult { if let Some(view) = self.views.get( view ) { //println!("::Token tree::\n{:?}", view ); let mut output = view.process( input )?; @@ -193,10 +193,11 @@ impl Crimtag { } } -fn exec(input: &StatefulContext, tokens: &Vec, buf: &mut String) -> CrimResult<()> { +fn exec MappedStructure<'a>>(input: &StatefulContext, tokens: &Vec, buf: &mut String) -> CrimResult<()> { for token in tokens { match token { - Token::Loop(ident,code) => { + Token::Loop(_ident,_code) => { + /* if let Some(value) = input.get_value( &ident ) && let Value::List(list) = value { for rec in &list { @@ -212,10 +213,12 @@ fn exec(input: &StatefulContext, tokens: &Vec, buf } } } + */ + return Ok(()); } Token::If(ident,code,other) => { if let Some(value) = input.get_value( &ident ) && - let Value::Bool(b) = value && + let MappedValue::Bool(b) = value && b { exec(input, code, buf)? @@ -231,34 +234,62 @@ fn exec(input: &StatefulContext, tokens: &Vec, buf }; if let Some(value) = input.get_value( &ident ) { match value { - Value::Dictionary(_) => { - buf.push_str(""); + MappedValue::Struct(_) => { + buf.push_str("Struct"); } - Value::List(_) => { - buf.push_str(""); + MappedValue::List(_) => { + buf.push_str("List"); } - Value::Bool(b) => { + MappedValue::Bool(b) => { if b { buf.push_str("true"); } else { buf.push_str("false"); } } - Value::String(s) => { + MappedValue::Str(s) => { + buf.push_str(s); + } + MappedValue::String(s) => { buf.push_str(s.as_str()); } - Value::Int(i) => { + MappedValue::Int8(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::Int16(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::Int32(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::Int64(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::UInt8(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::UInt16(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::UInt32(i) => { buf.push_str(&i.to_string()); } - Value::Float(f) => { + MappedValue::UInt64(i) => { + buf.push_str(&i.to_string()); + } + MappedValue::Float32(f) => { if format == "" { buf.push_str(&f.to_string()); } else { buf.push_str(&f.to_string()); } } - Value::Identifier(_) => { - // ??? + MappedValue::Float64(f) => { + if format == "" { + buf.push_str(&f.to_string()); + } else { + buf.push_str(&f.to_string()); + } } } } @@ -266,7 +297,7 @@ fn exec(input: &StatefulContext, tokens: &Vec, buf Token::Text(s) => { buf.push_str( s ); } - _ => {} + //_ => {} } } Ok(()) @@ -394,4 +425,111 @@ Now with explicit outputs: Ok(()) } + + struct Item { + pub id: i32, + pub title: String, + pub body: String, + } + + struct Person { + pub id: i32, + pub username: String, + pub name: String, + } + + struct Page { + pub user: Person, + pub total_items: i32, + pub total_pages: i32, + pub cur_page: i32, + pub items: Vec, + } + + impl Page { + fn new() -> Page { + Page { + user: Person { + id: 443, + username: "eichlan".into(), + name: "Mike".into(), + }, + total_items: 4000, + total_pages: 400, + cur_page: 35, + items: vec![ + Item{ + id: 123, + title: "hi".into(), + body: "body".into(), + }, + Item{ + id: 124, + title: "bye".into(), + body: "wooo".into(), + }, + Item{ + id: 125, + title: "ciao".into(), + body: "whatever".into(), + } + ], + } + } + } + + impl<'a> MappedStructure<'a> for Page { + fn get_value(&'a self, id: &str) -> Option> { + match id { + "user" => Some(MappedValue::Struct(&self.user)), + "total_items" => Some(MappedValue::Int32(self.total_items)), + "total_pages" => Some(MappedValue::Int32(self.total_pages)), + "cur_page" => Some(MappedValue::Int32(self.cur_page)), + "items" => Some(MappedValue::<'a>::List(&self.items)), + _ => None, + } + } + } + + impl<'a> MappedStructure<'a> for Person { + fn get_value(&'a self, id: &str) -> Option> { + match id { + "id" => Some(MappedValue::Int32(self.id)), + "username" => Some(MappedValue::String(&self.username)), + "name" => Some(MappedValue::String(&self.name)), + _ => None, + } + } + } + + impl<'a> MappedStructure<'a> for Item { + fn get_value(&'a self, id: &str) -> Option> { + match id { + "id" => Some(MappedValue::Int32(self.id)), + "title" => Some(MappedValue::String(&self.title)), + "body" => Some(MappedValue::String(&self.body)), + _ => None, + } + } + } + + impl<'a> MappedList<'a> for Vec { + fn get_index(&'a self, id: usize) -> Option> { + if let Some(x) = self.get(id) { + Some(MappedValue::<'a>::Struct(x)) + } else { + None + } + } + } + + #[test] + fn custom() -> Result<(), CrimError> { + let page = Page::new(); + + println!("{:?}", + page.get_value(&"total_pages") + ); + Ok(()) + } } -- cgit v1.2.3