From d1e78b4a3454984db74ebf2a302a64b985ce6de4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 19 Aug 2026 13:05:01 -0700 Subject: Rearranged and renamed some bits. --- deepcrow/src/lib.rs | 246 ---------------------------------------------------- 1 file changed, 246 deletions(-) (limited to 'deepcrow/src/lib.rs') diff --git a/deepcrow/src/lib.rs b/deepcrow/src/lib.rs index b10cd03..b9a4ffe 100644 --- a/deepcrow/src/lib.rs +++ b/deepcrow/src/lib.rs @@ -298,249 +298,3 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) Ok(()) } -#[cfg(test)] -mod tests { - use super::*; - - use crate::lexer::*; - - #[test] - fn lexing() { - let data = r#"Leading comment: [|view "basic" something="yeup"|>Hello there <|view|] Trailing text"#; - let ll = lexer::Lexer::new( &data ); - for sym in ll { - if let SymbolType::Error{what} = sym.symbol() { - println!("Error {}:{}: {:?}", sym.start().line, sym.start().column, what ); - break; - } else { - println!("Symbol: {:?}", sym ); - } - } - } - - #[test] - fn parsing() { - let mut ct = DeepCrow::new(); - if let Err(e) = ct.load_static(r#"Here is a sample -[|view "index" theme="standard"|>Hi<|view|] -That was fun! now a placeholder: [|view "placeholder"|] and now one with an implicit [|view "simple"|>Body -> [|show content|] <- and end<|view|] aoeu - -[|view "person" layout="simple"|>Welcome [|show person.last_name|], [|show person.first_name|]! It's lovely to see you again.<|view|] - -Now with explicit outputs: -[|view "complex"|> - [|output "content"|> - Here's a [|show name|]. - <|output|] - [|output "sidebar"|> - What's up world? - <|output|] - [|output "footer"|> - Same, I guess! - <|output|] -<|view|]"#) { - println!("Error: {:?}", e ); - } else { - println!("It finished"); - if let Ok(v) = ct.render( - "person", - &Context::from([ - ("hi".to_string(),Value::String("hi".to_string())), - ("person".to_string(),Value::Dictionary(HashMap::from([ - ("first_name".to_string(), Value::String("Bob".to_string())), - ("last_name".to_string(), Value::String("Smith".to_string())), - ]))), - ]) - ) { - println!("View result: {:?}", v ); - } else { - println!("Error?"); - } - } - } - - #[test] - fn loops() -> Result<()> { - let mut ct = DeepCrow::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|] -<|view|] -"#)?; - - let ctx = Context::from([ - ("people".to_string(), vec![ - [ - ("first_name".into(), "Joe".into()), - ("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()), - ]); - - println!("View result: {}", ct.render("index", &ctx)? ); - - Ok(()) - } - - #[test] - fn conditional() -> Result<()> { - let mut ct = DeepCrow::new(); - ct.load_static(r#"Hi there -[|view "index"|> - Color: [|if is_red|> red <|elif is_blue |> blue <|else|> green <|if|] -<|view|]"#)?; - - let ctx = Context::from([ - ("is_red".into(), false.into()), - ("is_blue".into(), false.into()), - ("color".into(), "purple".into()), - ]); - - println!("View result: {}", ct.render("index", &ctx)? ); - - 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, - } - } - } - - #[test] - fn custom() -> Result<()> { - let page = Page::new(); - - println!("{:?}", - page.get_value(&"total_pages") - ); - - if let Some(MappedValue::List(l)) = page.get_value(&"items") { - l.for_each(&|x: &MappedValue, buf: &mut String| { - if let MappedValue::Struct(s) = x { - println!(" - {:?}", s.get_value(&"title")); - buf.push_str(&format!("{:?}",s.get_value(&"title"))); - } - Ok(()) - }).expect("loop"); - } - Ok(()) - } -} -- cgit v1.2.3