summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs55
1 files changed, 54 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a2bfc37..c900e39 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -59,13 +59,14 @@ type Properties = HashMap<String, String>;
59 59
60#[derive(Debug)] 60#[derive(Debug)]
61enum Token { 61enum Token {
62 Loop(Identifier, Vec<Token>),
62 Show(Identifier,Properties), 63 Show(Identifier,Properties),
63 Text(String), 64 Text(String),
64} 65}
65 66
66#[derive(Debug,Clone)] 67#[derive(Debug,Clone)]
67pub enum Value { 68pub enum Value {
68 Dictionary(HashMap<String,Value>), 69 Dictionary(Context),
69 List(Vec<Value>), 70 List(Vec<Value>),
70 String(String), 71 String(String),
71 Int(i64), 72 Int(i64),
@@ -232,6 +233,16 @@ impl Crimtag {
232fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> Result<(),ParseError> { 233fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> Result<(),ParseError> {
233 for token in tokens { 234 for token in tokens {
234 match token { 235 match token {
236 Token::Loop(ident,code) => {
237 if let Some(value) = input.get_value( &ident ) &&
238 let Value::List(list) = value {
239 for rec in &list {
240 if let Value::Dictionary(d) = rec {
241 exec( d, code, buf )?
242 }
243 }
244 }
245 }
235 Token::Show(ident,p) => { 246 Token::Show(ident,p) => {
236 let format = if let Some(s) = p.get("format") { 247 let format = if let Some(s) = p.get("format") {
237 s 248 s
@@ -333,4 +344,46 @@ Now with explicit outputs:
333 } 344 }
334 } 345 }
335 } 346 }
347
348 #[test]
349 fn loops() {
350 let mut ct = Crimtag::new();
351 if let Err(e) = ct.load_static(r#"Looping code:
352[|view "index"|>We will enumerate people here:[|loop people|>
353 - [|show last_name|], [|show first_name|]: [|show title|]<|loop|]
354<|view|]
355"#) {
356 println!("Error: {:?}", e );
357 return;
358 }
359
360 let ctx = Context::from([
361 ("people".to_string(), crate::Value::List(vec![
362 crate::Value::Dictionary(Context::from([
363 ("first_name".to_string(), Value::String("Joe".to_string())),
364 ("last_name".to_string(), Value::String("Smith".to_string())),
365 ("title".to_string(), Value::String("CEO".to_string())),
366 ])),
367 crate::Value::Dictionary(Context::from([
368 ("first_name".to_string(), Value::String("Chris".to_string())),
369 ("last_name".to_string(), Value::String("Perkens".to_string())),
370 ("title".to_string(), Value::String("Baconeer".to_string())),
371 ])),
372 crate::Value::Dictionary(Context::from([
373 ("first_name".to_string(), Value::String("Will".to_string())),
374 ("last_name".to_string(), Value::String("Power".to_string())),
375 ("title".to_string(), Value::String("CFO".to_string())),
376 ])),
377 crate::Value::Dictionary(Context::from([
378 ("first_name".to_string(), Value::String("Justin".to_string())),
379 ("last_name".to_string(), Value::String("Time".to_string())),
380 ("title".to_string(), Value::String("CIO".to_string())),
381 ])),
382 ])),
383 ]);
384 match ct.render("index", &ctx) {
385 Ok(s) => println!("View result: {}", s ),
386 Err(e) => println!("Error: {:?}", e ),
387 }
388 }
336} 389}