• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sunng87 / handlebars-rust / 20853181580

05 Jan 2026 02:29AM UTC coverage: 83.449%. Remained the same
20853181580

push

github

web-flow
refactor: update to rust 2024 (#736)

* refactor: update to rust 2024

* chore: format code with rust 2024

23 of 25 new or added lines in 5 files covered. (92.0%)

1 existing line in 1 file now uncovered.

1679 of 2012 relevant lines covered (83.45%)

7.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.81
/src/render.rs
1
use std::borrow::Cow;
2
use std::collections::{BTreeMap, VecDeque};
3
use std::fmt;
4
use std::rc::Rc;
5

6
use serde_json::value::Value as Json;
7

8
use crate::block::BlockContext;
9
use crate::context::Context;
10
use crate::error::RenderError;
11
use crate::helpers::HelperDef;
12
use crate::json::path::Path;
13
use crate::json::value::{JsonRender, PathAndJson, ScopedJson};
14
use crate::output::{Output, StringOutput};
15
use crate::registry::Registry;
16
use crate::support;
17
use crate::support::str::newline_matcher;
18
use crate::template::TemplateElement::{
19
    DecoratorBlock, DecoratorExpression, Expression, HelperBlock, HtmlExpression, PartialBlock,
20
    PartialExpression, RawString,
21
};
22
use crate::template::{
23
    BlockParam, DecoratorTemplate, HelperTemplate, Parameter, Template, TemplateElement,
24
    TemplateMapping,
25
};
26
use crate::{RenderErrorReason, partial};
27

28
const HELPER_MISSING: &str = "helperMissing";
29
const BLOCK_HELPER_MISSING: &str = "blockHelperMissing";
30

31
/// The context of a render call
32
///
33
/// This context stores information of a render and a writer where generated
34
/// content is written to.
35
///
36
#[derive(Clone)]
37
pub struct RenderContext<'reg: 'rc, 'rc> {
38
    dev_mode_templates: Option<&'rc BTreeMap<String, Cow<'rc, Template>>>,
39

40
    blocks: VecDeque<BlockContext<'rc>>,
41

42
    // copy-on-write context
43
    modified_context: Option<Rc<Context>>,
44

45
    // when rendering partials, store rendered string into partial_block_stack
46
    // for `@partial-block` referencing. If it's a `{{> partial}}`, push `None`
47
    // to stack and report error when child partial try to referencing
48
    // `@partial-block`.
49
    partial_block_stack: VecDeque<Option<String>>,
50
    local_helpers: BTreeMap<String, Rc<dyn HelperDef + Send + Sync + 'rc>>,
51
    /// current template name
52
    current_template: Option<&'rc String>,
53
    /// root template name
54
    root_template: Option<&'reg String>,
55
    disable_escape: bool,
56

57
    // Indicates whether the previous text that we rendered ended on a newline.
58
    // This is necessary to make indenting decisions after the end of partials.
59
    trailing_newline: bool,
60

61
    // This should be set to true whenever any output is written.
62
    // We need this to detect empty partials/helpers for indenting decisions.
63
    content_produced: bool,
64

65
    // The next text that we render should indent itself.
66
    indent_before_write: bool,
67
    indent_string: Option<Cow<'rc, str>>,
68

69
    // Threads the recursive_lookup state from the registry down
70
    // through to the context navigation layer
71
    recursive_lookup: bool,
72
}
73

74
impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
75
    /// Create a render context
76
    pub fn new(root_template: Option<&'reg String>) -> RenderContext<'reg, 'rc> {
19✔
77
        let mut blocks = VecDeque::with_capacity(5);
18✔
78
        blocks.push_front(BlockContext::new());
34✔
79

80
        let modified_context = None;
20✔
81
        RenderContext {
82
            partial_block_stack: VecDeque::new(),
16✔
83
            local_helpers: BTreeMap::new(),
21✔
84
            current_template: None,
85
            root_template,
86
            disable_escape: false,
87
            trailing_newline: false,
88
            content_produced: false,
89
            indent_before_write: false,
90
            indent_string: None,
91
            blocks,
92
            modified_context,
93
            dev_mode_templates: None,
94
            recursive_lookup: false,
95
        }
96
    }
97

98
    /// Push a block context into render context stack. This is typically
99
    /// called when you entering a block scope.
100
    pub fn push_block(&mut self, block: BlockContext<'rc>) {
11✔
101
        self.blocks.push_front(block);
13✔
102
    }
103

104
    /// Pop and drop current block context.
105
    /// This is typically called when leaving a block scope.
106
    pub fn pop_block(&mut self) {
10✔
107
        self.blocks.pop_front();
9✔
108
    }
109

110
    /// Remove all blocks
111
    pub(crate) fn clear_blocks(&mut self) {
×
112
        self.blocks.clear();
×
113
    }
114

115
    /// Borrow a reference to current block context
116
    pub fn block(&self) -> Option<&BlockContext<'rc>> {
×
117
        self.blocks.front()
×
118
    }
119

120
    /// Borrow a mutable reference to current block context in order to
121
    /// modify some data.
122
    pub fn block_mut(&mut self) -> Option<&mut BlockContext<'rc>> {
7✔
123
        self.blocks.front_mut()
9✔
124
    }
125

126
    /// Replace blocks hold by render context and return the replaced blocks
127
    pub fn replace_blocks(
×
128
        &mut self,
129
        blocks: VecDeque<BlockContext<'rc>>,
130
    ) -> VecDeque<BlockContext<'rc>> {
131
        std::mem::replace(&mut self.blocks, blocks)
×
132
    }
133

134
    /// Get the modified context data if any
135
    pub fn context(&self) -> Option<Rc<Context>> {
15✔
136
        self.modified_context.clone()
14✔
137
    }
138

139
    /// Set new context data into the render process.
140
    /// This is typically called in decorators where user can modify
141
    /// the data they were rendering.
142
    pub fn set_context(&mut self, ctx: Context) {
1✔
143
        self.modified_context = Some(Rc::new(ctx));
2✔
144
    }
145

146
    /// Evaluate a Json path in current scope.
147
    ///
148
    /// Typically you don't need to evaluate it by yourself.
149
    /// The Helper and Decorator API will provide your evaluated value of
150
    /// their parameters and hash data.
151
    pub fn evaluate(
3✔
152
        &self,
153
        context: &'rc Context,
154
        relative_path: &str,
155
    ) -> Result<ScopedJson<'rc>, RenderError> {
156
        let path = Path::parse(relative_path)?;
3✔
157
        self.evaluate2(context, &path)
3✔
158
    }
159

160
    pub(crate) fn evaluate2(
14✔
161
        &self,
162
        context: &'rc Context,
163
        path: &Path,
164
    ) -> Result<ScopedJson<'rc>, RenderError> {
165
        match path {
14✔
166
            Path::Local((level, name, _)) => Ok(self
2✔
167
                .get_local_var(*level, name)
1✔
168
                .map_or_else(|| ScopedJson::Missing, |v| ScopedJson::Derived(v.clone()))),
6✔
169
            Path::Relative((segs, _)) => {
14✔
170
                context.navigate(segs, &self.blocks, self.recursive_lookup)
14✔
171
            }
172
        }
173
    }
174

175
    /// Get registered partial in this render context
176
    pub fn get_partial(&self, name: &str) -> Option<&'rc Template> {
6✔
177
        // resolve partial from block
178
        for block in &self.blocks {
13✔
179
            if let Some(partial) = block.get_local_partial(name) {
11✔
180
                return Some(partial);
5✔
181
            }
182
        }
183

184
        None
4✔
185
    }
186

187
    pub(crate) fn push_partial_block(&mut self, partial_block: Option<String>) {
6✔
188
        self.partial_block_stack.push_front(partial_block);
7✔
189
    }
190

191
    pub(crate) fn pop_partial_block(&mut self) {
7✔
192
        self.partial_block_stack.pop_front();
7✔
193
    }
194

195
    pub(crate) fn peek_partial_block(&self) -> Option<&Option<String>> {
1✔
196
        self.partial_block_stack.front()
1✔
197
    }
198

199
    pub(crate) fn set_indent_string(&mut self, indent: Option<Cow<'rc, str>>) {
8✔
200
        self.indent_string = indent;
15✔
201
    }
202

203
    #[inline]
204
    pub(crate) fn get_indent_string(&self) -> Option<&Cow<'rc, str>> {
15✔
205
        self.indent_string.as_ref()
15✔
206
    }
207

208
    pub(crate) fn get_dev_mode_template(&self, name: &str) -> Option<&'rc Template> {
4✔
209
        self.dev_mode_templates
4✔
210
            .and_then(|dmt| dmt.get(name).map(|t| &**t))
4✔
211
    }
212

213
    pub(crate) fn set_dev_mode_templates(
1✔
214
        &mut self,
215
        t: Option<&'rc BTreeMap<String, Cow<'rc, Template>>>,
216
    ) {
217
        self.dev_mode_templates = t;
1✔
218
    }
219

220
    fn get_local_var(&self, level: usize, name: &str) -> Option<&Json> {
1✔
221
        self.blocks
×
222
            .get(level)
1✔
223
            .and_then(|blk| blk.get_local_var(name))
3✔
224
    }
225

226
    /// Test if given template name is current template.
227
    pub fn is_current_template(&self, p: &str) -> bool {
6✔
228
        self.current_template.is_some_and(|s| s == p)
11✔
229
    }
230

231
    /// Register a helper in this render context.
232
    /// This is a feature provided by Decorator where you can create
233
    /// temporary helpers.
234
    pub fn register_local_helper(
1✔
235
        &mut self,
236
        name: &str,
237
        def: Box<dyn HelperDef + Send + Sync + 'rc>,
238
    ) {
239
        self.local_helpers.insert(name.to_string(), def.into());
2✔
240
    }
241

242
    /// Remove a helper from render context
243
    pub fn unregister_local_helper(&mut self, name: &str) {
1✔
244
        self.local_helpers.remove(name);
1✔
245
    }
246

247
    /// Attempt to get a helper from current render context.
248
    pub fn get_local_helper(&self, name: &str) -> Option<Rc<dyn HelperDef + Send + Sync + 'rc>> {
11✔
249
        self.local_helpers.get(name).cloned()
12✔
250
    }
251

252
    #[inline]
253
    fn has_local_helper(&self, name: &str) -> bool {
15✔
254
        self.local_helpers.contains_key(name)
14✔
255
    }
256

257
    /// Returns the current template name.
258
    /// Note that the name can be vary from root template when you are rendering
259
    /// from partials.
260
    pub fn get_current_template_name(&self) -> Option<&'rc String> {
7✔
261
        self.current_template
6✔
262
    }
263

264
    /// Set the current template name.
265
    pub fn set_current_template_name(&mut self, name: Option<&'rc String>) {
17✔
266
        self.current_template = name;
17✔
267
    }
268

269
    /// Get root template name if any.
270
    /// This is the template name that you call `render` from `Handlebars`.
271
    pub fn get_root_template_name(&self) -> Option<&'reg String> {
×
272
        self.root_template
×
273
    }
274

275
    /// Get the escape toggle
276
    pub fn is_disable_escape(&self) -> bool {
13✔
277
        self.disable_escape
12✔
278
    }
279

280
    /// Set the escape toggle.
281
    /// When toggle is on, `escape_fn` will be called when rendering.
282
    pub fn set_disable_escape(&mut self, disable: bool) {
4✔
283
        self.disable_escape = disable;
4✔
284
    }
285

286
    #[inline]
287
    pub fn set_trailing_newline(&mut self, trailing_newline: bool) {
16✔
288
        self.trailing_newline = trailing_newline;
16✔
289
    }
290

291
    #[inline]
292
    pub fn get_trailine_newline(&self) -> bool {
16✔
293
        self.trailing_newline
17✔
294
    }
295

296
    #[inline]
297
    pub fn set_content_produced(&mut self, content_produced: bool) {
16✔
298
        self.content_produced = content_produced;
17✔
299
    }
300

301
    #[inline]
302
    pub fn get_content_produced(&self) -> bool {
14✔
303
        self.content_produced
15✔
304
    }
305

306
    #[inline]
307
    pub fn set_indent_before_write(&mut self, indent_before_write: bool) {
17✔
308
        self.indent_before_write = indent_before_write;
16✔
309
    }
310

311
    #[inline]
312
    pub fn get_indent_before_write(&self) -> bool {
17✔
313
        self.indent_before_write
17✔
314
    }
315

316
    pub fn set_recursive_lookup(&mut self, enabled: bool) {
20✔
317
        self.recursive_lookup = enabled;
15✔
318
    }
319
}
320

321
impl fmt::Debug for RenderContext<'_, '_> {
322
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
×
323
        f.debug_struct("RenderContextInner")
×
324
            .field("dev_mode_templates", &self.dev_mode_templates)
×
325
            .field("blocks", &self.blocks)
×
326
            .field("modified_context", &self.modified_context)
×
327
            .field("partial_block_stack", &self.partial_block_stack)
×
328
            .field("root_template", &self.root_template)
×
329
            .field("current_template", &self.current_template)
×
330
            .field("disable_escape", &self.disable_escape)
×
331
            .finish()
332
    }
333
}
334

335
/// Render-time Helper data when using in a helper definition
336
#[derive(Debug, Clone)]
337
pub struct Helper<'rc> {
338
    name: Cow<'rc, str>,
339
    params: Vec<PathAndJson<'rc>>,
340
    hash: BTreeMap<&'rc str, PathAndJson<'rc>>,
341
    template: Option<&'rc Template>,
342
    inverse: Option<&'rc Template>,
343
    block_param: Option<&'rc BlockParam>,
344
    block: bool,
345
}
346

347
impl<'reg: 'rc, 'rc> Helper<'rc> {
348
    fn try_from_template(
11✔
349
        ht: &'rc HelperTemplate,
350
        registry: &'reg Registry<'reg>,
351
        context: &'rc Context,
352
        render_context: &mut RenderContext<'reg, 'rc>,
353
    ) -> Result<Helper<'rc>, RenderError> {
354
        let name = ht.name.expand_as_name(registry, context, render_context)?;
11✔
355
        let mut pv = Vec::with_capacity(ht.params.len());
23✔
356
        for p in &ht.params {
33✔
357
            let r = p.expand(registry, context, render_context)?;
20✔
358
            pv.push(r);
11✔
359
        }
360

361
        let mut hm = BTreeMap::new();
12✔
362
        for (k, p) in &ht.hash {
25✔
363
            let r = p.expand(registry, context, render_context)?;
6✔
364
            hm.insert(k.as_ref(), r);
6✔
365
        }
366

367
        Ok(Helper {
11✔
368
            name,
10✔
369
            params: pv,
11✔
370
            hash: hm,
11✔
371
            template: ht.template.as_ref(),
11✔
372
            inverse: ht.inverse.as_ref(),
11✔
373
            block_param: ht.block_param.as_ref(),
11✔
374
            block: ht.block,
11✔
375
        })
376
    }
377

378
    /// Returns helper name
379
    pub fn name(&self) -> &str {
13✔
380
        &self.name
10✔
381
    }
382

383
    /// Returns all helper params, resolved within the context
384
    pub fn params(&self) -> &Vec<PathAndJson<'rc>> {
3✔
385
        &self.params
×
386
    }
387

388
    /// Returns nth helper param, resolved within the context.
389
    ///
390
    /// ## Example
391
    ///
392
    /// To get the first param in `{{my_helper abc}}` or `{{my_helper 2}}`,
393
    /// use `h.param(0)` in helper definition.
394
    /// Variable `abc` is auto resolved in current context.
395
    ///
396
    /// ```
397
    /// use handlebars::*;
398
    ///
399
    /// fn my_helper(h: &Helper, rc: &mut RenderContext) -> Result<(), RenderError> {
400
    ///     let v = h.param(0).map(|v| v.value())
401
    ///         .ok_or(RenderErrorReason::ParamNotFoundForIndex("myhelper", 0));
402
    ///     // ..
403
    ///     Ok(())
404
    /// }
405
    /// ```
406
    pub fn param(&self, idx: usize) -> Option<&PathAndJson<'rc>> {
12✔
407
        self.params.get(idx)
10✔
408
    }
409

410
    /// Returns hash, resolved within the context
411
    pub fn hash(&self) -> &BTreeMap<&'rc str, PathAndJson<'rc>> {
2✔
412
        &self.hash
2✔
413
    }
414

415
    /// Return hash value of a given key, resolved within the context
416
    ///
417
    /// ## Example
418
    ///
419
    /// To get the first param in `{{my_helper v=abc}}` or `{{my_helper v=2}}`,
420
    /// use `h.hash_get("v")` in helper definition.
421
    /// Variable `abc` is auto resolved in current context.
422
    ///
423
    /// ```
424
    /// use handlebars::*;
425
    ///
426
    /// fn my_helper(h: &Helper, rc: &mut RenderContext) -> Result<(), RenderError> {
427
    ///     let v = h.hash_get("v").map(|v| v.value())
428
    ///         .ok_or(RenderErrorReason::ParamNotFoundForIndex("my_helper", 0));
429
    ///     // ..
430
    ///     Ok(())
431
    /// }
432
    /// ```
433
    pub fn hash_get(&self, key: &str) -> Option<&PathAndJson<'rc>> {
4✔
434
        self.hash.get(key)
4✔
435
    }
436

437
    /// Returns the default inner template if the helper is a block helper.
438
    ///
439
    /// Typically you will render the template via: `template.render(registry, render_context)`
440
    ///
441
    pub fn template(&self) -> Option<&'rc Template> {
9✔
442
        self.template
11✔
443
    }
444

445
    /// Returns the template of `else` branch if any
446
    pub fn inverse(&self) -> Option<&'rc Template> {
2✔
447
        self.inverse
2✔
448
    }
449

450
    /// Returns if the helper is a block one `{{#helper}}{{/helper}}` or not `{{helper 123}}`
451
    pub fn is_block(&self) -> bool {
2✔
452
        self.block
2✔
453
    }
454

455
    /// Returns if the helper has either a block param or block param pair
456
    pub fn has_block_param(&self) -> bool {
×
457
        self.block_param.is_some()
×
458
    }
459

460
    /// Returns block param if any
461
    pub fn block_param(&self) -> Option<&'rc str> {
9✔
462
        if let Some(&BlockParam::Single(Parameter::Name(ref s))) = self.block_param {
18✔
463
            Some(s)
5✔
464
        } else {
465
            None
6✔
466
        }
467
    }
468

469
    /// Return block param pair (for example |key, val|) if any
470
    pub fn block_param_pair(&self) -> Option<(&'rc str, &'rc str)> {
7✔
471
        if let Some(&BlockParam::Pair((Parameter::Name(ref s1), Parameter::Name(ref s2)))) =
17✔
472
            self.block_param
×
473
        {
474
            Some((s1, s2))
3✔
475
        } else {
476
            None
7✔
477
        }
478
    }
479
}
480

481
/// Render-time Decorator data when using in a decorator definition
482
#[derive(Debug)]
483
pub struct Decorator<'rc> {
484
    name: Cow<'rc, str>,
485
    params: Vec<PathAndJson<'rc>>,
486
    hash: BTreeMap<&'rc str, PathAndJson<'rc>>,
487
    template: Option<&'rc Template>,
488
    indent: Option<Cow<'rc, str>>,
489
}
490

491
impl<'reg: 'rc, 'rc> Decorator<'rc> {
492
    fn try_from_template(
7✔
493
        dt: &'rc DecoratorTemplate,
494
        registry: &'reg Registry<'reg>,
495
        context: &'rc Context,
496
        render_context: &mut RenderContext<'reg, 'rc>,
497
    ) -> Result<Decorator<'rc>, RenderError> {
498
        let name = dt.name.expand_as_name(registry, context, render_context)?;
6✔
499

500
        let mut pv = Vec::with_capacity(dt.params.len());
12✔
501
        for p in &dt.params {
19✔
502
            let r = p.expand(registry, context, render_context)?;
11✔
503
            pv.push(r);
7✔
504
        }
505

506
        let mut hm = BTreeMap::new();
8✔
507
        for (k, p) in &dt.hash {
15✔
508
            let r = p.expand(registry, context, render_context)?;
4✔
509
            hm.insert(k.as_ref(), r);
4✔
510
        }
511

512
        let indent = match (render_context.get_indent_string(), dt.indent.as_ref()) {
7✔
513
            (None, None) => None,
6✔
514
            (Some(s), None) => Some(s.clone()),
1✔
515
            (None, Some(s)) => Some(Cow::Borrowed(&**s)),
4✔
516
            (Some(s1), Some(s2)) => {
2✔
517
                let mut res = s1.to_string();
4✔
518
                res.push_str(s2);
4✔
519
                Some(Cow::from(res))
2✔
520
            }
521
        };
522

523
        Ok(Decorator {
6✔
524
            name,
7✔
525
            params: pv,
6✔
526
            hash: hm,
7✔
527
            template: dt.template.as_ref(),
6✔
528
            indent,
6✔
529
        })
530
    }
531

532
    /// Returns helper name
533
    pub fn name(&self) -> &str {
7✔
534
        self.name.as_ref()
8✔
535
    }
536

537
    /// Returns all helper params, resolved within the context
538
    pub fn params(&self) -> &Vec<PathAndJson<'rc>> {
×
539
        &self.params
×
540
    }
541

542
    /// Returns nth helper param, resolved within the context
543
    pub fn param(&self, idx: usize) -> Option<&PathAndJson<'rc>> {
6✔
544
        self.params.get(idx)
7✔
545
    }
546

547
    /// Returns hash, resolved within the context
548
    pub fn hash(&self) -> &BTreeMap<&'rc str, PathAndJson<'rc>> {
7✔
549
        &self.hash
6✔
550
    }
551

552
    /// Return hash value of a given key, resolved within the context
553
    pub fn hash_get(&self, key: &str) -> Option<&PathAndJson<'rc>> {
×
554
        self.hash.get(key)
×
555
    }
556

557
    /// Returns the default inner template if any
558
    pub fn template(&self) -> Option<&'rc Template> {
7✔
559
        self.template
6✔
560
    }
561

562
    pub fn indent(&self) -> Option<&Cow<'rc, str>> {
7✔
563
        self.indent.as_ref()
8✔
564
    }
565
}
566

567
/// Render trait
568
pub trait Renderable {
569
    /// render into `RenderContext`'s `writer`
570
    fn render<'reg: 'rc, 'rc>(
571
        &'rc self,
572
        registry: &'reg Registry<'reg>,
573
        context: &'rc Context,
574
        rc: &mut RenderContext<'reg, 'rc>,
575
        out: &mut dyn Output,
576
    ) -> Result<(), RenderError>;
577

578
    /// render into string
579
    fn renders<'reg: 'rc, 'rc>(
×
580
        &'rc self,
581
        registry: &'reg Registry<'reg>,
582
        ctx: &'rc Context,
583
        rc: &mut RenderContext<'reg, 'rc>,
584
    ) -> Result<String, RenderError> {
585
        let mut so = StringOutput::new();
×
586
        self.render(registry, ctx, rc, &mut so)?;
×
587
        so.into_string()
×
588
            .map_err(|e| RenderErrorReason::from(e).into())
×
589
    }
590
}
591

592
/// Evaluate decorator
593
pub trait Evaluable {
594
    fn eval<'reg: 'rc, 'rc>(
595
        &'rc self,
596
        registry: &'reg Registry<'reg>,
597
        context: &'rc Context,
598
        rc: &mut RenderContext<'reg, 'rc>,
599
    ) -> Result<(), RenderError>;
600
}
601

602
#[inline]
603
fn call_helper_for_value<'reg: 'rc, 'rc>(
4✔
604
    hd: &dyn HelperDef,
605
    ht: &Helper<'rc>,
606
    r: &'reg Registry<'reg>,
607
    ctx: &'rc Context,
608
    rc: &mut RenderContext<'reg, 'rc>,
609
) -> Result<PathAndJson<'rc>, RenderError> {
610
    match hd.call_inner(ht, r, ctx, rc) {
2✔
611
        Ok(result) => Ok(PathAndJson::new(None, result)),
3✔
612
        Err(e) => {
2✔
613
            if e.is_unimplemented() {
6✔
614
                // parse value from output
615
                let mut so = StringOutput::new();
1✔
616

617
                // here we don't want subexpression result escaped,
618
                // so we temporarily disable it
619
                let disable_escape = rc.is_disable_escape();
2✔
620
                rc.set_disable_escape(true);
1✔
621

622
                hd.call(ht, r, ctx, rc, &mut so)?;
1✔
623
                rc.set_disable_escape(disable_escape);
1✔
624

625
                let string = so.into_string().map_err(RenderError::from)?;
1✔
626
                Ok(PathAndJson::new(
2✔
627
                    None,
1✔
628
                    ScopedJson::Derived(Json::String(string)),
1✔
629
                ))
630
            } else {
631
                Err(e)
1✔
632
            }
633
        }
634
    }
635
}
636

637
impl Parameter {
638
    pub fn expand_as_name<'reg: 'rc, 'rc>(
14✔
639
        &'rc self,
640
        registry: &'reg Registry<'reg>,
641
        ctx: &'rc Context,
642
        rc: &mut RenderContext<'reg, 'rc>,
643
    ) -> Result<Cow<'rc, str>, RenderError> {
644
        match self {
13✔
645
            Parameter::Name(name) => Ok(Cow::Borrowed(name)),
12✔
646
            Parameter::Path(p) => Ok(Cow::Borrowed(p.raw())),
14✔
UNCOV
647
            Parameter::Subexpression(_) => self
×
648
                .expand(registry, ctx, rc)
1✔
649
                .map(|v| v.value().render())
3✔
650
                .map(Cow::Owned),
1✔
NEW
651
            Parameter::Literal(j) => Ok(Cow::Owned(j.render())),
×
652
        }
653
    }
654

655
    pub fn expand<'reg: 'rc, 'rc>(
14✔
656
        &'rc self,
657
        registry: &'reg Registry<'reg>,
658
        ctx: &'rc Context,
659
        rc: &mut RenderContext<'reg, 'rc>,
660
    ) -> Result<PathAndJson<'rc>, RenderError> {
661
        match self {
14✔
NEW
662
            Parameter::Name(name) => {
×
663
                // FIXME: raise error when expanding with name?
664
                Ok(PathAndJson::new(Some(name.to_owned()), ScopedJson::Missing))
×
665
            }
666
            Parameter::Path(path) => {
13✔
667
                if let Some(rc_context) = rc.context() {
29✔
668
                    let result = rc.evaluate2(&rc_context, path)?;
2✔
669
                    Ok(PathAndJson::new(
2✔
670
                        Some(path.raw().to_owned()),
2✔
671
                        ScopedJson::Derived(result.as_json().clone()),
2✔
672
                    ))
673
                } else {
674
                    let result = rc.evaluate2(ctx, path)?;
25✔
675
                    Ok(PathAndJson::new(Some(path.raw().to_owned()), result))
27✔
676
                }
677
            }
678
            Parameter::Literal(j) => Ok(PathAndJson::new(None, ScopedJson::Constant(j))),
8✔
679
            Parameter::Subexpression(t) => match *t.as_element() {
4✔
680
                Expression(ref ht) => {
3✔
681
                    let name = ht.name.expand_as_name(registry, ctx, rc)?;
7✔
682

683
                    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
6✔
684
                    if let Some(ref d) = rc.get_local_helper(&name) {
9✔
685
                        call_helper_for_value(d.as_ref(), &h, registry, ctx, rc)
×
686
                    } else {
687
                        let mut helper = registry.get_or_load_helper(&name)?;
4✔
688

689
                        if helper.is_none() {
7✔
690
                            helper = registry.get_or_load_helper(if ht.block {
4✔
691
                                BLOCK_HELPER_MISSING
×
692
                            } else {
693
                                HELPER_MISSING
1✔
694
                            })?;
695
                        }
696

697
                        helper
2✔
698
                            .ok_or_else(|| {
4✔
699
                                RenderErrorReason::HelperNotFound(name.to_string()).into()
×
700
                            })
701
                            .and_then(|d| call_helper_for_value(d.as_ref(), &h, registry, ctx, rc))
8✔
702
                    }
703
                }
704
                _ => unreachable!(),
705
            },
706
        }
707
    }
708
}
709

710
impl Renderable for Template {
711
    fn render<'reg: 'rc, 'rc>(
18✔
712
        &'rc self,
713
        registry: &'reg Registry<'reg>,
714
        ctx: &'rc Context,
715
        rc: &mut RenderContext<'reg, 'rc>,
716
        out: &mut dyn Output,
717
    ) -> Result<(), RenderError> {
718
        rc.set_current_template_name(self.name.as_ref());
17✔
719
        let iter = self.elements.iter();
15✔
720

721
        for (idx, t) in iter.enumerate() {
32✔
722
            t.render(registry, ctx, rc, out).map_err(|mut e| {
21✔
723
                // add line/col number if the template has mapping data
724
                if e.line_no.is_none() {
4✔
725
                    if let Some(&TemplateMapping(line, col)) = self.mapping.get(idx) {
4✔
726
                        e.line_no = Some(line);
2✔
727
                        e.column_no = Some(col);
2✔
728
                    }
729
                }
730

731
                if e.template_name.is_none() {
4✔
732
                    e.template_name.clone_from(&self.name);
2✔
733
                }
734

735
                e
2✔
736
            })?;
737
        }
738

739
        Ok(())
17✔
740
    }
741
}
742

743
impl Evaluable for Template {
744
    fn eval<'reg: 'rc, 'rc>(
×
745
        &'rc self,
746
        registry: &'reg Registry<'reg>,
747
        ctx: &'rc Context,
748
        rc: &mut RenderContext<'reg, 'rc>,
749
    ) -> Result<(), RenderError> {
750
        let iter = self.elements.iter();
×
751

752
        for (idx, t) in iter.enumerate() {
×
753
            t.eval(registry, ctx, rc).map_err(|mut e| {
×
754
                if e.line_no.is_none() {
×
755
                    if let Some(&TemplateMapping(line, col)) = self.mapping.get(idx) {
×
756
                        e.line_no = Some(line);
×
757
                        e.column_no = Some(col);
×
758
                    }
759
                }
760

761
                e.template_name.clone_from(&self.name);
×
762
                e
×
763
            })?;
764
        }
765
        Ok(())
×
766
    }
767
}
768

769
fn helper_exists<'reg: 'rc, 'rc>(
13✔
770
    name: &str,
771
    reg: &Registry<'reg>,
772
    rc: &RenderContext<'reg, 'rc>,
773
) -> bool {
774
    rc.has_local_helper(name) || reg.has_helper(name)
13✔
775
}
776

777
#[inline]
778
fn render_helper<'reg: 'rc, 'rc>(
11✔
779
    ht: &'rc HelperTemplate,
780
    registry: &'reg Registry<'reg>,
781
    ctx: &'rc Context,
782
    rc: &mut RenderContext<'reg, 'rc>,
783
    out: &mut dyn Output,
784
) -> Result<(), RenderError> {
785
    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
11✔
786
    debug!(
×
787
        "Rendering helper: {:?}, params: {:?}, hash: {:?}",
788
        h.name(),
×
789
        h.params(),
×
790
        h.hash()
×
791
    );
792
    let mut call_indent_aware = |helper_def: &dyn HelperDef, rc: &mut RenderContext<'reg, 'rc>| {
23✔
793
        let indent_directive_before = rc.get_indent_before_write();
13✔
794
        let content_produced_before = rc.get_content_produced();
13✔
795
        rc.set_content_produced(false);
12✔
796
        rc.set_indent_before_write(
12✔
797
            indent_directive_before || (ht.indent_before_write && rc.get_trailine_newline()),
13✔
798
        );
799

800
        helper_def.call(&h, registry, ctx, rc, out)?;
13✔
801

802
        if rc.get_content_produced() {
12✔
803
            rc.set_indent_before_write(rc.get_trailine_newline());
12✔
804
        } else {
805
            rc.set_content_produced(content_produced_before);
5✔
806
            rc.set_indent_before_write(indent_directive_before);
5✔
807
        }
808
        Ok(())
12✔
809
    };
810
    if let Some(ref d) = rc.get_local_helper(h.name()) {
35✔
811
        call_indent_aware(&**d, rc)
2✔
812
    } else {
813
        let mut helper = registry.get_or_load_helper(h.name())?;
11✔
814

815
        if helper.is_none() {
25✔
816
            helper = registry.get_or_load_helper(if ht.block {
8✔
817
                BLOCK_HELPER_MISSING
1✔
818
            } else {
819
                HELPER_MISSING
2✔
820
            })?;
821
        }
822

823
        helper
13✔
824
            .ok_or_else(|| RenderErrorReason::HelperNotFound(h.name().to_owned()).into())
16✔
825
            .and_then(|d| call_indent_aware(&*d, rc))
38✔
826
    }
827
}
828

829
pub(crate) fn do_escape(r: &Registry<'_>, rc: &RenderContext<'_, '_>, content: String) -> String {
13✔
830
    if !rc.is_disable_escape() {
28✔
831
        r.get_escape_fn()(&content)
25✔
832
    } else {
833
        content
4✔
834
    }
835
}
836

837
#[inline]
838
pub fn indent_aware_write(
18✔
839
    v: &str,
840
    rc: &mut RenderContext<'_, '_>,
841
    out: &mut dyn Output,
842
) -> Result<(), RenderError> {
843
    if v.is_empty() {
16✔
844
        return Ok(());
6✔
845
    }
846
    rc.set_content_produced(true);
16✔
847

848
    if !v.starts_with(newline_matcher) && rc.get_indent_before_write() {
33✔
849
        if let Some(indent) = rc.get_indent_string() {
4✔
850
            out.write(indent)?;
2✔
851
        }
852
    }
853

854
    if let Some(indent) = rc.get_indent_string() {
18✔
855
        support::str::write_indented(v, indent, out)?;
2✔
856
    } else {
857
        out.write(v.as_ref())?;
16✔
858
    }
859

860
    let trailing_newline = v.ends_with(newline_matcher);
16✔
861
    rc.set_trailing_newline(trailing_newline);
16✔
862
    rc.set_indent_before_write(trailing_newline);
16✔
863

864
    Ok(())
16✔
865
}
866

867
impl Renderable for TemplateElement {
868
    fn render<'reg: 'rc, 'rc>(
15✔
869
        &'rc self,
870
        registry: &'reg Registry<'reg>,
871
        ctx: &'rc Context,
872
        rc: &mut RenderContext<'reg, 'rc>,
873
        out: &mut dyn Output,
874
    ) -> Result<(), RenderError> {
875
        match self {
44✔
876
            RawString(v) => indent_aware_write(v.as_ref(), rc, out),
12✔
877
            Expression(ht) | HtmlExpression(ht) => {
15✔
878
                let is_html_expression = matches!(self, HtmlExpression(_));
12✔
879
                if is_html_expression {
13✔
880
                    rc.set_disable_escape(true);
4✔
881
                }
882

883
                // test if the expression is to render some value
884
                let result = if ht.is_name_only() {
27✔
885
                    let helper_name = ht.name.expand_as_name(registry, ctx, rc)?;
27✔
886
                    if helper_exists(&helper_name, registry, rc) {
27✔
887
                        render_helper(ht, registry, ctx, rc, out)
6✔
888
                    } else {
889
                        debug!("Rendering value: {:?}", ht.name);
×
890
                        let context_json = ht.name.expand(registry, ctx, rc)?;
20✔
891
                        if context_json.is_value_missing() {
19✔
892
                            if registry.strict_mode() {
3✔
893
                                Err(RenderError::strict_error(context_json.relative_path()))
2✔
894
                            } else {
895
                                // helper missing
896
                                if let Some(hook) = registry.get_or_load_helper(HELPER_MISSING)? {
3✔
897
                                    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
3✔
898
                                    hook.call(&h, registry, ctx, rc, out)
2✔
899
                                } else {
900
                                    Ok(())
1✔
901
                                }
902
                            }
903
                        } else {
904
                            let rendered = context_json.value().render();
20✔
905
                            let output = do_escape(registry, rc, rendered);
10✔
906
                            indent_aware_write(output.as_ref(), rc, out)
22✔
907
                        }
908
                    }
909
                } else {
910
                    // this is a helper expression
911
                    render_helper(ht, registry, ctx, rc, out)
10✔
912
                };
913

914
                if is_html_expression {
14✔
915
                    rc.set_disable_escape(false);
8✔
916
                }
917

918
                result
13✔
919
            }
920
            HelperBlock(ht) => render_helper(ht, registry, ctx, rc, out),
18✔
921
            DecoratorExpression(_) | DecoratorBlock(_) => self.eval(registry, ctx, rc),
4✔
922
            PartialExpression(dt) | PartialBlock(dt) => {
8✔
923
                let di = Decorator::try_from_template(dt, registry, ctx, rc)?;
6✔
924

925
                let indent_directive_before = rc.get_indent_before_write();
13✔
926
                let content_produced_before = rc.get_content_produced();
6✔
927

928
                rc.set_indent_before_write(
8✔
929
                    dt.indent_before_write && (rc.get_trailine_newline() || dt.indent.is_some()),
15✔
930
                );
931
                rc.set_content_produced(false);
6✔
932

933
                partial::expand_partial(&di, registry, ctx, rc, out)?;
7✔
934

935
                if rc.get_content_produced() {
8✔
936
                    rc.set_indent_before_write(rc.get_trailine_newline());
16✔
937
                } else {
938
                    rc.set_content_produced(content_produced_before);
2✔
939
                    rc.set_indent_before_write(indent_directive_before);
2✔
940
                }
941
                Ok(())
8✔
942
            }
943
            _ => Ok(()),
2✔
944
        }
945
    }
946
}
947

948
impl Evaluable for TemplateElement {
949
    fn eval<'reg: 'rc, 'rc>(
4✔
950
        &'rc self,
951
        registry: &'reg Registry<'reg>,
952
        ctx: &'rc Context,
953
        rc: &mut RenderContext<'reg, 'rc>,
954
    ) -> Result<(), RenderError> {
955
        match *self {
6✔
956
            DecoratorExpression(ref dt) | DecoratorBlock(ref dt) => {
6✔
957
                let di = Decorator::try_from_template(dt, registry, ctx, rc)?;
5✔
958
                match registry.get_decorator(di.name()) {
11✔
959
                    Some(d) => d.call(&di, registry, ctx, rc),
10✔
960
                    None => Err(RenderErrorReason::DecoratorNotFound(di.name().to_owned()).into()),
1✔
961
                }
962
            }
963
            _ => Ok(()),
×
964
        }
965
    }
966
}
967

968
#[cfg(test)]
969
mod test {
970
    use std::collections::BTreeMap;
971

972
    use super::{Helper, RenderContext, Renderable};
973
    use crate::block::BlockContext;
974
    use crate::context::Context;
975
    use crate::error::RenderError;
976
    use crate::json::path::Path;
977
    use crate::json::value::JsonRender;
978
    use crate::output::{Output, StringOutput};
979
    use crate::registry::Registry;
980
    use crate::template::TemplateElement::*;
981
    use crate::template::{HelperTemplate, Template, TemplateElement};
982

983
    #[test]
984
    fn test_raw_string() {
985
        let r = Registry::new();
986
        let raw_string = RawString("<h1>hello world</h1>".to_string());
987

988
        let mut out = StringOutput::new();
989
        let ctx = Context::null();
990
        {
991
            let mut rc = RenderContext::new(None);
992
            raw_string.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
993
        }
994
        assert_eq!(
995
            out.into_string().unwrap(),
996
            "<h1>hello world</h1>".to_string()
997
        );
998
    }
999

1000
    #[test]
1001
    fn test_expression() {
1002
        let r = Registry::new();
1003
        let element = Expression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1004
            &["hello"],
1005
        ))));
1006

1007
        let mut out = StringOutput::new();
1008
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1009
        let value = "<p></p>".to_string();
1010
        m.insert("hello".to_string(), value);
1011
        let ctx = Context::wraps(&m).unwrap();
1012
        {
1013
            let mut rc = RenderContext::new(None);
1014
            element.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1015
        }
1016

1017
        assert_eq!(
1018
            out.into_string().unwrap(),
1019
            "&lt;p&gt;&lt;/p&gt;".to_string()
1020
        );
1021
    }
1022

1023
    #[test]
1024
    fn test_html_expression() {
1025
        let r = Registry::new();
1026
        let element = HtmlExpression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1027
            &["hello"],
1028
        ))));
1029

1030
        let mut out = StringOutput::new();
1031
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1032
        let value = "world";
1033
        m.insert("hello".to_string(), value.to_string());
1034
        let ctx = Context::wraps(&m).unwrap();
1035
        {
1036
            let mut rc = RenderContext::new(None);
1037
            element.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1038
        }
1039

1040
        assert_eq!(out.into_string().unwrap(), value.to_string());
1041
    }
1042

1043
    #[test]
1044
    fn test_template() {
1045
        let r = Registry::new();
1046
        let mut out = StringOutput::new();
1047
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1048
        let value = "world".to_string();
1049
        m.insert("hello".to_string(), value);
1050
        let ctx = Context::wraps(&m).unwrap();
1051

1052
        let elements: Vec<TemplateElement> = vec![
1053
            RawString("<h1>".to_string()),
1054
            Expression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1055
                &["hello"],
1056
            )))),
1057
            RawString("</h1>".to_string()),
1058
            Comment(String::new()),
1059
        ];
1060

1061
        let template = Template {
1062
            elements,
1063
            name: None,
1064
            mapping: Vec::new(),
1065
        };
1066

1067
        {
1068
            let mut rc = RenderContext::new(None);
1069
            template.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1070
        }
1071

1072
        assert_eq!(out.into_string().unwrap(), "<h1>world</h1>".to_string());
1073
    }
1074

1075
    #[test]
1076
    fn test_render_context_promotion_and_demotion() {
1077
        use crate::json::value::to_json;
1078
        let mut render_context = RenderContext::new(None);
1079
        let mut block = BlockContext::new();
1080

1081
        block.set_local_var("index", to_json(0));
1082
        render_context.push_block(block);
1083

1084
        render_context.push_block(BlockContext::new());
1085
        assert_eq!(
1086
            render_context.get_local_var(1, "index").unwrap(),
1087
            &to_json(0)
1088
        );
1089

1090
        render_context.pop_block();
1091

1092
        assert_eq!(
1093
            render_context.get_local_var(0, "index").unwrap(),
1094
            &to_json(0)
1095
        );
1096
    }
1097

1098
    #[test]
1099
    fn test_render_subexpression_issue_115() {
1100
        use crate::support::str::StringWriter;
1101

1102
        let mut r = Registry::new();
1103
        r.register_helper(
1104
            "format",
1105
            Box::new(
1106
                |h: &Helper<'_>,
1107
                 _: &Registry<'_>,
1108
                 _: &Context,
1109
                 _: &mut RenderContext<'_, '_>,
1110
                 out: &mut dyn Output|
1111
                 -> Result<(), RenderError> {
1112
                    out.write(&h.param(0).unwrap().value().render())
1113
                        .map_err(RenderError::from)
1114
                },
1115
            ),
1116
        );
1117

1118
        let mut sw = StringWriter::new();
1119
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1120
        m.insert("a".to_string(), "123".to_string());
1121

1122
        {
1123
            if let Err(e) = r.render_template_to_write("{{format (format a)}}", &m, &mut sw) {
1124
                panic!("{}", e);
1125
            }
1126
        }
1127

1128
        assert_eq!(sw.into_string(), "123".to_string());
1129
    }
1130

1131
    #[test]
1132
    fn test_render_error_line_no() {
1133
        let mut r = Registry::new();
1134
        let m: BTreeMap<String, String> = BTreeMap::new();
1135

1136
        let name = "invalid_template";
1137
        assert!(
1138
            r.register_template_string(name, "<h1>\n{{#if true}}\n  {{#each}}{{/each}}\n{{/if}}")
1139
                .is_ok()
1140
        );
1141

1142
        if let Err(e) = r.render(name, &m) {
1143
            assert_eq!(e.line_no.unwrap(), 3);
1144
            assert_eq!(e.column_no.unwrap(), 3);
1145
            assert_eq!(e.template_name, Some(name.to_owned()));
1146
        } else {
1147
            panic!("Error expected");
1148
        }
1149
    }
1150

1151
    #[test]
1152
    fn test_partial_failback_render() {
1153
        let mut r = Registry::new();
1154

1155
        assert!(
1156
            r.register_template_string("parent", "<html>{{> layout}}</html>")
1157
                .is_ok()
1158
        );
1159
        assert!(
1160
            r.register_template_string(
1161
                "child",
1162
                "{{#*inline \"layout\"}}content{{/inline}}{{#> parent}}{{> seg}}{{/parent}}",
1163
            )
1164
            .is_ok()
1165
        );
1166
        assert!(r.register_template_string("seg", "1234").is_ok());
1167

1168
        let r = r.render("child", &true).expect("should work");
1169
        assert_eq!(r, "<html>content</html>");
1170
    }
1171

1172
    #[test]
1173
    fn test_key_with_slash() {
1174
        let mut r = Registry::new();
1175

1176
        assert!(
1177
            r.register_template_string("t", "{{#each this}}{{@key}}: {{this}}\n{{/each}}")
1178
                .is_ok()
1179
        );
1180

1181
        let r = r.render("t", &json!({"/foo": "bar"})).unwrap();
1182

1183
        assert_eq!(r, "/foo: bar\n");
1184
    }
1185

1186
    #[test]
1187
    fn test_comment() {
1188
        let r = Registry::new();
1189

1190
        assert_eq!(
1191
            r.render_template("Hello {{this}} {{! test me }}", &0)
1192
                .unwrap(),
1193
            "Hello 0 "
1194
        );
1195
    }
1196

1197
    #[test]
1198
    fn test_zero_args_heler() {
1199
        let mut r = Registry::new();
1200

1201
        r.register_helper(
1202
            "name",
1203
            Box::new(
1204
                |_: &Helper<'_>,
1205
                 _: &Registry<'_>,
1206
                 _: &Context,
1207
                 _: &mut RenderContext<'_, '_>,
1208
                 out: &mut dyn Output|
1209
                 -> Result<(), RenderError> {
1210
                    out.write("N/A").map_err(Into::into)
1211
                },
1212
            ),
1213
        );
1214

1215
        r.register_template_string("t0", "Output name: {{name}}")
1216
            .unwrap();
1217
        r.register_template_string("t1", "Output name: {{first_name}}")
1218
            .unwrap();
1219
        r.register_template_string("t2", "Output name: {{./name}}")
1220
            .unwrap();
1221

1222
        // when "name" is available in context, use context first
1223
        assert_eq!(
1224
            r.render("t0", &json!({"name": "Alex"})).unwrap(),
1225
            "Output name: N/A"
1226
        );
1227

1228
        // when "name" is unavailable, call helper with same name
1229
        assert_eq!(
1230
            r.render("t2", &json!({"name": "Alex"})).unwrap(),
1231
            "Output name: Alex"
1232
        );
1233

1234
        // output nothing when neither context nor helper available
1235
        assert_eq!(
1236
            r.render("t1", &json!({"name": "Alex"})).unwrap(),
1237
            "Output name: "
1238
        );
1239

1240
        // generate error in strict mode for above case
1241
        r.set_strict_mode(true);
1242
        assert!(r.render("t1", &json!({"name": "Alex"})).is_err());
1243

1244
        // output nothing when helperMissing was defined
1245
        r.set_strict_mode(false);
1246
        r.register_helper(
1247
            "helperMissing",
1248
            Box::new(
1249
                |h: &Helper<'_>,
1250
                 _: &Registry<'_>,
1251
                 _: &Context,
1252
                 _: &mut RenderContext<'_, '_>,
1253
                 out: &mut dyn Output|
1254
                 -> Result<(), RenderError> {
1255
                    let name = h.name();
1256
                    write!(out, "{name} not resolved")?;
1257
                    Ok(())
1258
                },
1259
            ),
1260
        );
1261
        assert_eq!(
1262
            r.render("t1", &json!({"name": "Alex"})).unwrap(),
1263
            "Output name: first_name not resolved"
1264
        );
1265
    }
1266

1267
    #[test]
1268
    fn test_identifiers_starting_with_numbers() {
1269
        let mut r = Registry::new();
1270

1271
        assert!(
1272
            r.register_template_string("r1", "{{#if 0a}}true{{/if}}")
1273
                .is_ok()
1274
        );
1275
        let r1 = r.render("r1", &json!({"0a": true})).unwrap();
1276
        assert_eq!(r1, "true");
1277

1278
        assert!(r.register_template_string("r2", "{{eq 1a 1}}").is_ok());
1279
        let r2 = r.render("r2", &json!({"1a": 2, "a": 1})).unwrap();
1280
        assert_eq!(r2, "false");
1281

1282
        assert!(r
1283
            .register_template_string("r3", "0: {{0}} {{#if (eq 0 true)}}resolved from context{{/if}}\n1a: {{1a}} {{#if (eq 1a true)}}resolved from context{{/if}}\n2_2: {{2_2}} {{#if (eq 2_2 true)}}resolved from context{{/if}}") // YUP it is just eq that barfs! is if handled specially? maybe this test should go nearer to specific helpers that fail?
1284
            .is_ok());
1285
        let r3 = r
1286
            .render("r3", &json!({"0": true, "1a": true, "2_2": true}))
1287
            .unwrap();
1288
        assert_eq!(
1289
            r3,
1290
            "0: true \n1a: true resolved from context\n2_2: true resolved from context"
1291
        );
1292

1293
        // these should all be errors:
1294
        assert!(r.register_template_string("r4", "{{eq 1}}").is_ok());
1295
        assert!(r.register_template_string("r5", "{{eq a1}}").is_ok());
1296
        assert!(r.register_template_string("r6", "{{eq 1a}}").is_ok());
1297
        assert!(r.render("r4", &()).is_err());
1298
        assert!(r.render("r5", &()).is_err());
1299
        assert!(r.render("r6", &()).is_err());
1300
    }
1301

1302
    #[test]
1303
    fn test_recursive_path_resolution() {
1304
        let mut r = Registry::new();
1305
        const TEMPLATE: &str = "{{#each children}}outer={{{outer}}} inner={{{inner}}}{{/each}}";
1306

1307
        let data = json!({
1308
          "children": [{"inner": "inner"}],
1309
          "outer": "outer"
1310
        });
1311

1312
        let r1 = r.render_template(TEMPLATE, &data).unwrap();
1313
        assert_eq!(r1, "outer= inner=inner");
1314

1315
        r.set_recursive_lookup(true);
1316

1317
        let r2 = r.render_template(TEMPLATE, &data).unwrap();
1318
        assert_eq!(r2, "outer=outer inner=inner");
1319
    }
1320
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc