use std::collections::HashMap; use deepcrow::*; /* #[test] fn lexing() { let data = r#"Leading comment: [|view "basic" something="yeup"|>Hello there <|view|] Trailing text"#; let ll = 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