1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Block helper that iterates arrays and objects.
use crate::{
    error::HelperError,
    helper::{Helper, HelperValue},
    parser::ast::Node,
    render::{Context, Render, Scope},
};

use serde_json::{Number, Value};

static FIRST: &str = "first";
static LAST: &str = "last";
static KEY: &str = "key";
static INDEX: &str = "index";

/// Iterate an array or object.
///
/// Accepts a single argument of the target to iterate, if the
/// target is not an array or object this will return an error.
///
/// Each iteration sets a new scope with the local variables:
///
/// * `@first`: If this is the first iteration `true`.
/// * `@last`: If this is the last iteration `true`.
///
/// Note that these variables are set even for objects where iteration order
/// is not guaranteed which can be useful.
///
/// For objects the `@key` variable contains the name of the field; for
/// arrays the `@index` variable contains the current zero-based index.
///
pub struct Each;

impl Helper for Each {
    fn call<'render, 'call>(
        &self,
        rc: &mut Render<'render>,
        ctx: &Context<'call>,
        template: Option<&'render Node<'render>>,
    ) -> HelperValue {
        ctx.arity(1..1)?;

        if let Some(template) = template {
            let name = ctx.name();
            let args = ctx.arguments();
            let target = args.get(0).unwrap();

            rc.push_scope(Scope::new());
            match target {
                Value::Object(t) => {
                    let mut it = t.into_iter().enumerate();
                    let mut next_value = it.next();
                    while let Some((index, (key, value))) = next_value {
                        next_value = it.next();
                        if let Some(ref mut scope) = rc.scope_mut() {
                            scope.set_local(FIRST, Value::Bool(index == 0));
                            scope.set_local(
                                LAST,
                                Value::Bool(next_value.is_none()),
                            );
                            scope.set_local(
                                INDEX,
                                Value::Number(Number::from(index)),
                            );
                            scope.set_local(KEY, Value::String(key.to_owned()));
                            scope.set_base_value(value.clone());
                        }
                        rc.template(template)?;
                    }
                }
                Value::Array(t) => {
                    let len = t.len();
                    for (index, value) in t.into_iter().enumerate() {
                        if let Some(ref mut scope) = rc.scope_mut() {
                            scope.set_local(FIRST, Value::Bool(index == 0));
                            scope
                                .set_local(LAST, Value::Bool(index == len - 1));
                            scope.set_local(
                                INDEX,
                                Value::Number(Number::from(index)),
                            );
                            scope.set_base_value(value.clone());
                        }
                        rc.template(template)?;
                    }
                }
                _ => {
                    return Err(HelperError::IterableExpected(
                        name.to_string(),
                        1,
                    ))
                }
            }
            rc.pop_scope();
        }

        Ok(None)
    }
}