Skip to main content

handlebars/
block.rs

1use std::collections::BTreeMap;
2
3use serde_json::value::Value as Json;
4
5use crate::Template;
6use crate::error::RenderError;
7use crate::local_vars::LocalVars;
8
9#[derive(Clone, Debug)]
10pub enum BlockParamHolder {
11    // a reference to certain context value
12    Path(Vec<String>),
13    // an actual value holder
14    Value(Json),
15}
16
17impl BlockParamHolder {
18    pub fn value(v: Json) -> BlockParamHolder {
19        BlockParamHolder::Value(v)
20    }
21
22    pub fn path(r: Vec<String>) -> BlockParamHolder {
23        BlockParamHolder::Path(r)
24    }
25}
26
27/// A map holds block parameters. The parameter can be either a value or a reference
28#[derive(Clone, Debug, Default)]
29pub struct BlockParams<'reg> {
30    data: BTreeMap<&'reg str, BlockParamHolder>,
31}
32
33impl<'reg> BlockParams<'reg> {
34    /// Create a empty block parameter map.
35    pub fn new() -> BlockParams<'reg> {
36        BlockParams::default()
37    }
38
39    /// Add a path reference as the parameter. The `path` is a vector of path
40    /// segments the relative to current block's base path.
41    pub fn add_path(&mut self, k: &'reg str, path: Vec<String>) -> Result<(), RenderError> {
42        self.data.insert(k, BlockParamHolder::path(path));
43        Ok(())
44    }
45
46    /// Add a value as parameter.
47    pub fn add_value(&mut self, k: &'reg str, v: Json) -> Result<(), RenderError> {
48        self.data.insert(k, BlockParamHolder::value(v));
49        Ok(())
50    }
51
52    /// Get a block parameter by its name.
53    pub fn get(&self, k: &str) -> Option<&BlockParamHolder> {
54        self.data.get(k)
55    }
56}
57
58/// A data structure holds contextual data for current block scope.
59#[derive(Debug, Clone, Default)]
60pub struct BlockContext<'rc> {
61    /// the `base_path` of current block scope
62    base_path: Vec<String>,
63    /// the `base_value` of current block scope, when the block is using a
64    /// constant or derived value as block base
65    base_value: Option<Json>,
66    /// current block context variables
67    block_params: BlockParams<'rc>,
68    /// the partials available in this block
69    block_partials: BTreeMap<String, &'rc Template>,
70    /// local variables in current context
71    local_variables: LocalVars,
72}
73
74impl<'rc> BlockContext<'rc> {
75    /// create a new `BlockContext` with default data
76    pub fn new() -> BlockContext<'rc> {
77        BlockContext::default()
78    }
79
80    /// set a local variable into current scope
81    pub fn set_local_var(&mut self, name: &str, value: Json) {
82        self.local_variables.put(name, value);
83    }
84
85    /// Get mutable access to the local variables
86    pub fn local_variables_mut(&mut self) -> &mut LocalVars {
87        &mut self.local_variables
88    }
89
90    /// borrow the local variables
91    pub fn local_variables(&self) -> &LocalVars {
92        &self.local_variables
93    }
94
95    /// get a local variable from current scope
96    pub fn get_local_var(&self, name: &str) -> Option<&Json> {
97        self.local_variables.get(name)
98    }
99
100    /// borrow a reference to current scope's base path
101    /// all paths inside this block will be relative to this path
102    pub fn base_path(&self) -> &Vec<String> {
103        &self.base_path
104    }
105
106    /// borrow a mutable reference to the base path
107    pub fn base_path_mut(&mut self) -> &mut Vec<String> {
108        &mut self.base_path
109    }
110
111    /// borrow the base value
112    pub fn base_value(&self) -> Option<&Json> {
113        self.base_value.as_ref()
114    }
115
116    /// set the base value
117    pub fn set_base_value(&mut self, value: Json) {
118        self.base_value = Some(value);
119    }
120
121    pub fn get_local_partial(&self, name: &str) -> Option<&'rc Template> {
122        self.block_partials.get(name).map(|v| &**v)
123    }
124
125    pub fn set_local_partial(&mut self, name: String, template: &'rc Template) {
126        self.block_partials.insert(name, template);
127    }
128
129    /// Get a block parameter from this block.
130    /// Block parameters needed to be supported by the block helper.
131    /// The typical syntax for block parameter is:
132    ///
133    /// ```skip
134    /// {{#myblock param1 as |block_param1|}}
135    ///    ...
136    /// {{/myblock}}
137    /// ```
138    ///
139    pub fn get_block_param(&self, block_param_name: &str) -> Option<&BlockParamHolder> {
140        self.block_params.get(block_param_name)
141    }
142
143    /// Reassign the block parameters for this block.
144    pub fn set_block_params(&mut self, block_params: BlockParams<'rc>) {
145        self.block_params = block_params;
146    }
147
148    /// Set a block parameter into this block.
149    pub fn set_block_param(&mut self, key: &'rc str, value: BlockParamHolder) {
150        self.block_params.data.insert(key, value);
151    }
152}