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

sunng87 / handlebars-rust / 20740331273

06 Jan 2026 06:35AM UTC coverage: 83.35% (-0.1%) from 83.449%
20740331273

Pull #737

github

web-flow
Merge 775bc6f56 into b1b39cd03
Pull Request #737: refactor: use smol_str for template strings

45 of 52 new or added lines in 9 files covered. (86.54%)

1 existing line in 1 file now uncovered.

1682 of 2018 relevant lines covered (83.35%)

7.05 hits per line

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

90.0
/src/block.rs
1
use std::collections::BTreeMap;
2

3
use serde_json::value::Value as Json;
4
use smol_str::SmolStr;
5

6
use crate::Template;
7
use crate::error::RenderError;
8
use crate::local_vars::LocalVars;
9

10
#[derive(Clone, Debug)]
11
pub enum BlockParamHolder {
12
    // a reference to certain context value
13
    Path(Vec<SmolStr>),
14
    // an actual value holder
15
    Value(Json),
16
}
17

18
impl BlockParamHolder {
19
    pub fn value(v: Json) -> BlockParamHolder {
2✔
20
        BlockParamHolder::Value(v)
2✔
21
    }
22

23
    pub fn path(r: Vec<SmolStr>) -> BlockParamHolder {
3✔
24
        BlockParamHolder::Path(r)
3✔
25
    }
26
}
27

28
/// A map holds block parameters. The parameter can be either a value or a reference
29
#[derive(Clone, Debug, Default)]
30
pub struct BlockParams<'reg> {
31
    data: BTreeMap<&'reg str, BlockParamHolder>,
32
}
33

34
impl<'reg> BlockParams<'reg> {
35
    /// Create a empty block parameter map.
36
    pub fn new() -> BlockParams<'reg> {
3✔
37
        BlockParams::default()
3✔
38
    }
39

40
    /// Add a path reference as the parameter. The `path` is a vector of path
41
    /// segments the relative to current block's base path.
42
    pub fn add_path(&mut self, k: &'reg str, path: Vec<SmolStr>) -> Result<(), RenderError> {
3✔
43
        self.data.insert(k, BlockParamHolder::path(path));
3✔
44
        Ok(())
3✔
45
    }
46

47
    /// Add a value as parameter.
48
    pub fn add_value(&mut self, k: &'reg str, v: Json) -> Result<(), RenderError> {
2✔
49
        self.data.insert(k, BlockParamHolder::value(v));
2✔
50
        Ok(())
2✔
51
    }
52

53
    /// Get a block parameter by its name.
54
    pub fn get(&self, k: &str) -> Option<&BlockParamHolder> {
11✔
55
        self.data.get(k)
11✔
56
    }
57
}
58

59
/// A data structure holds contextual data for current block scope.
60
#[derive(Debug, Clone, Default)]
61
pub struct BlockContext<'rc> {
62
    /// `base_path` of current block scope
63
    base_path: Vec<SmolStr>,
64
    /// `base_value` of current block scope, when the block is using a
65
    /// constant or derived value as block base
66
    base_value: Option<Json>,
67
    /// current block context variables
68
    block_params: BlockParams<'rc>,
69
    /// partials available in this block
70
    block_partials: BTreeMap<String, &'rc Template>,
71
    /// local variables in current context
72
    local_variables: LocalVars,
73
}
74

75
impl<'rc> BlockContext<'rc> {
76
    /// create a new `BlockContext` with default data
77
    pub fn new() -> BlockContext<'rc> {
15✔
78
        BlockContext::default()
14✔
79
    }
80

81
    /// set a local variable into current scope
82
    pub fn set_local_var(&mut self, name: &str, value: Json) {
7✔
83
        self.local_variables.put(name, value);
9✔
84
    }
85

86
    /// Get mutable access to the local variables
87
    pub fn local_variables_mut(&mut self) -> &mut LocalVars {
×
88
        &mut self.local_variables
×
89
    }
90

91
    /// get a local variable from current scope
92
    pub fn get_local_var(&self, name: &str) -> Option<&Json> {
1✔
93
        self.local_variables.get(name)
1✔
94
    }
95

96
    /// borrow a reference to current scope's base path
97
    /// all paths inside this block will be relative to this path
98
    pub fn base_path(&self) -> &Vec<SmolStr> {
13✔
99
        &self.base_path
×
100
    }
101

102
    /// borrow a mutable reference to base path
103
    pub fn base_path_mut(&mut self) -> &mut Vec<SmolStr> {
7✔
UNCOV
104
        &mut self.base_path
×
105
    }
106

107
    /// borrow the base value
108
    pub fn base_value(&self) -> Option<&Json> {
13✔
109
        self.base_value.as_ref()
16✔
110
    }
111

112
    /// set the base value
113
    pub fn set_base_value(&mut self, value: Json) {
8✔
114
        self.base_value = Some(value);
15✔
115
    }
116

117
    pub fn get_local_partial(&self, name: &str) -> Option<&'rc Template> {
6✔
118
        self.block_partials.get(name).map(|v| &**v)
16✔
119
    }
120

121
    pub fn set_local_partial(&mut self, name: String, template: &'rc Template) {
4✔
122
        self.block_partials.insert(name, template);
5✔
123
    }
124

125
    /// Get a block parameter from this block.
126
    /// Block parameters needed to be supported by the block helper.
127
    /// The typical syntax for block parameter is:
128
    ///
129
    /// ```skip
130
    /// {{#myblock param1 as |block_param1|}}
131
    ///    ...
132
    /// {{/myblock}}
133
    /// ```
134
    ///
135
    pub fn get_block_param(&self, block_param_name: &str) -> Option<&BlockParamHolder> {
11✔
136
        self.block_params.get(block_param_name)
12✔
137
    }
138

139
    /// Reassign the block parameters for this block.
140
    pub fn set_block_params(&mut self, block_params: BlockParams<'rc>) {
3✔
141
        self.block_params = block_params;
6✔
142
    }
143

144
    /// Set a block parameter into this block.
145
    pub fn set_block_param(&mut self, key: &'rc str, value: BlockParamHolder) {
2✔
146
        self.block_params.data.insert(key, value);
2✔
147
    }
148
}
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