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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::ops::Range;
use dyn_clone::DynClone;
use crate::{error::HelperError as Error, parser::ast::Node, render::Render};
pub type ValueResult = std::result::Result<Option<Value>, Error>;
pub type BlockResult = std::result::Result<(), Error>;
pub trait Helper: Send + Sync + DynClone {
fn call<'reg, 'source, 'render>(
&self,
rc: &mut Render<'reg, 'source, 'render>,
ctx: &mut Context<'source>,
) -> ValueResult;
}
dyn_clone::clone_trait_object!(Helper);
pub trait BlockHelper: Send + Sync + DynClone {
fn call<'reg, 'source, 'render>(
&self,
rc: &mut Render<'reg, 'source, 'render>,
ctx: &mut Context<'source>,
block: BlockTemplate<'source>,
) -> BlockResult;
}
dyn_clone::clone_trait_object!(BlockHelper);
#[cfg(feature = "each-helper")]
pub mod each;
#[cfg(feature = "conditional-helper")]
pub mod r#if;
#[cfg(feature = "json-helper")]
pub mod json;
#[cfg(feature = "log-helper")]
pub mod log;
#[cfg(feature = "logical-helper")]
pub mod logical;
#[cfg(feature = "lookup-helper")]
pub mod lookup;
#[cfg(feature = "conditional-helper")]
pub mod unless;
#[cfg(feature = "with-helper")]
pub mod with;
#[derive(Debug)]
pub struct BlockTemplate<'source> {
template: &'source Node<'source>,
}
impl<'source> BlockTemplate<'source> {
pub fn new(template: &'source Node<'source>) -> Self {
Self { template }
}
pub fn template(&self) -> &'source Node<'source> {
self.template
}
pub fn inverse<'reg, 'render>(
&self,
rc: &mut Render<'reg, 'source, 'render>,
) -> Result<Option<&'source Node<'source>>, Error> {
let mut alt: Option<&'source Node<'source>> = None;
let mut branch: Option<&'source Node<'source>> = None;
match &self.template {
Node::Block(ref block) => {
if !block.conditions().is_empty() {
for node in block.conditions().iter() {
match node {
Node::Condition(clause) => {
if clause.call().is_empty() {
alt = Some(node);
} else {
if let Some(value) = rc
.call(clause.call())
.map_err(Box::new)?
{
if rc.is_truthy(&value) {
branch = Some(node);
break;
}
}
}
}
_ => {}
}
}
}
}
_ => {}
}
Ok(branch.or(alt))
}
}
pub struct Context<'ctx> {
name: String,
arguments: Vec<Value>,
hash: Map<String, Value>,
helpers: HelperRegistry<'ctx>,
}
impl<'ctx> Context<'ctx> {
pub fn new(
name: String,
arguments: Vec<Value>,
hash: Map<String, Value>,
) -> Self {
Self {
name,
arguments,
hash,
helpers: Default::default(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn arguments(&self) -> &Vec<Value> {
&self.arguments
}
pub fn hash(&self) -> &Map<String, Value> {
&self.hash
}
}
pub trait Assertion {
fn arity(&self, context: &Context<'_>, range: Range<usize>) -> BlockResult;
}
#[derive(Clone, Default)]
pub struct HelperRegistry<'reg> {
helpers: HashMap<&'reg str, Box<dyn Helper + 'reg>>,
block_helpers: HashMap<&'reg str, Box<dyn BlockHelper + 'reg>>,
}
impl<'reg> HelperRegistry<'reg> {
pub fn new() -> Self {
let mut reg = Self {
helpers: Default::default(),
block_helpers: Default::default(),
};
reg.builtins();
reg
}
fn builtins(&mut self) {
#[cfg(feature = "conditional-helper")]
self.register_helper("if", Box::new(r#if::IfHelper {}));
#[cfg(feature = "conditional-helper")]
self.register_block_helper("if", Box::new(r#if::IfBlockHelper {}));
#[cfg(feature = "conditional-helper")]
self.register_block_helper("unless", Box::new(unless::UnlessHelper {}));
#[cfg(feature = "log-helper")]
self.register_helper("log", Box::new(log::LogHelper {}));
#[cfg(feature = "lookup-helper")]
self.register_helper("lookup", Box::new(lookup::LookupHelper {}));
#[cfg(feature = "logical-helper")]
self.register_helper("and", Box::new(logical::AndHelper {}));
#[cfg(feature = "logical-helper")]
self.register_helper("or", Box::new(logical::OrHelper {}));
#[cfg(feature = "logical-helper")]
self.register_helper("not", Box::new(logical::NotHelper {}));
#[cfg(feature = "with-helper")]
self.register_block_helper("with", Box::new(with::WithHelper {}));
#[cfg(feature = "each-helper")]
self.register_block_helper("each", Box::new(each::EachHelper {}));
#[cfg(feature = "json-helper")]
self.register_helper("json", Box::new(json::JsonHelper {}));
}
pub fn register_helper(
&mut self,
name: &'reg str,
helper: Box<dyn Helper + 'reg>,
) {
self.helpers.insert(name, helper);
}
pub fn register_block_helper(
&mut self,
name: &'reg str,
helper: Box<dyn BlockHelper + 'reg>,
) {
self.block_helpers.insert(name, helper);
}
pub fn get(&self, name: &str) -> Option<&Box<dyn Helper + 'reg>> {
self.helpers.get(name)
}
pub fn get_block(
&self,
name: &str,
) -> Option<&Box<dyn BlockHelper + 'reg>> {
self.block_helpers.get(name)
}
}