-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
context.rs
72 lines (69 loc) · 1.71 KB
/
context.rs
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
#[cfg(test)]
use similar_asserts::assert_eq;
/// Creates a template context with keys and values.
///
/// ```rust
/// # use minijinja::context;
/// let ctx = context! {
/// name => "Peter",
/// location => "World",
/// };
/// ```
///
/// Alternatively if the variable name matches the key name it can
/// be omitted:
///
/// ```rust
/// # use minijinja::context;
/// let name = "Peter";
/// let ctx = context! { name };
/// ```
///
/// The return value is a [`Value`](crate::value::Value).
///
/// Note that [`context!`] can also be used recursively if you need to
/// create nested objects:
///
/// ```rust
/// # use minijinja::context;
/// let ctx = context! {
/// nav => vec![
/// context!(path => "/", title => "Index"),
/// context!(path => "/downloads", title => "Downloads"),
/// context!(path => "/faq", title => "FAQ"),
/// ]
/// };
/// ```
#[macro_export]
macro_rules! context {
(
$($key:ident $(=> $value:expr)?),* $(,)?
) => {{
let mut ctx = std::collections::BTreeMap::default();
$(
$crate::__pair!(ctx, $key $(, $value)?);
)*
$crate::value::Value::from(ctx)
}}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __pair {
($ctx:ident, $key:ident) => {{
$crate::__pair!($ctx, $key, $key);
}};
($ctx:ident, $key:ident, $value:expr) => {
$ctx.insert(
stringify!($key),
$crate::value::Value::from_serializable(&$value),
);
};
}
#[test]
fn test_macro() {
use crate::value::Value;
let var1 = 23;
let ctx = context!(var1, var2 => 42);
assert_eq!(ctx.get_attr("var1").unwrap(), Value::from(23));
assert_eq!(ctx.get_attr("var2").unwrap(), Value::from(42));
}