1use std::collections::BTreeMap;
23use serde_json::value::Value as Json;
45use crate::error::RenderError;
6use crate::local_vars::LocalVars;
78#[derive(Clone, Debug)]
9pub enum BlockParamHolder {
10// a reference to certain context value
11Path(Vec<String>),
12// an actual value holder
13Value(Json),
14}
1516impl BlockParamHolder {
17pub fn value(v: Json) -> BlockParamHolder {
18 BlockParamHolder::Value(v)
19 }
2021pub fn path(r: Vec<String>) -> BlockParamHolder {
22 BlockParamHolder::Path(r)
23 }
24}
2526/// A map holds block parameters. The parameter can be either a value or a reference
27#[derive(Clone, Debug, Default)]
28pub struct BlockParams<'reg> {
29 data: BTreeMap<&'reg str, BlockParamHolder>,
30}
3132impl<'reg> BlockParams<'reg> {
33/// Create a empty block parameter map.
34pub fn new() -> BlockParams<'reg> {
35 BlockParams::default()
36 }
3738/// Add a path reference as the parameter. The `path` is a vector of path
39 /// segments the relative to current block's base path.
40pub fn add_path(&mut self, k: &'reg str, path: Vec<String>) -> Result<(), RenderError> {
41self.data.insert(k, BlockParamHolder::path(path));
42Ok(())
43 }
4445/// Add a value as parameter.
46pub fn add_value(&mut self, k: &'reg str, v: Json) -> Result<(), RenderError> {
47self.data.insert(k, BlockParamHolder::value(v));
48Ok(())
49 }
5051/// Get a block parameter by its name.
52pub fn get(&self, k: &str) -> Option<&BlockParamHolder> {
53self.data.get(k)
54 }
55}
5657/// A data structure holds contextual data for current block scope.
58#[derive(Debug, Clone, Default)]
59pub struct BlockContext<'rc> {
60/// the `base_path` of current block scope
61base_path: Vec<String>,
62/// the `base_value` of current block scope, when the block is using a
63 /// constant or derived value as block base
64base_value: Option<Json>,
65/// current block context variables
66block_params: BlockParams<'rc>,
67/// local variables in current context
68local_variables: LocalVars,
69}
7071impl<'rc> BlockContext<'rc> {
72/// create a new `BlockContext` with default data
73pub fn new() -> BlockContext<'rc> {
74 BlockContext::default()
75 }
7677/// set a local variable into current scope
78pub fn set_local_var(&mut self, name: &str, value: Json) {
79self.local_variables.put(name, value);
80 }
8182/// Get mutable access to the local variables
83pub fn local_variables_mut(&mut self) -> &mut LocalVars {
84&mut self.local_variables
85 }
8687/// get a local variable from current scope
88pub fn get_local_var(&self, name: &str) -> Option<&Json> {
89self.local_variables.get(name)
90 }
9192/// borrow a reference to current scope's base path
93 /// all paths inside this block will be relative to this path
94pub fn base_path(&self) -> &Vec<String> {
95&self.base_path
96 }
9798/// borrow a mutable reference to the base path
99pub fn base_path_mut(&mut self) -> &mut Vec<String> {
100&mut self.base_path
101 }
102103/// borrow the base value
104pub fn base_value(&self) -> Option<&Json> {
105self.base_value.as_ref()
106 }
107108/// set the base value
109pub fn set_base_value(&mut self, value: Json) {
110self.base_value = Some(value);
111 }
112113/// Get a block parameter from this block.
114 /// Block parameters needed to be supported by the block helper.
115 /// The typical syntax for block parameter is:
116 ///
117 /// ```skip
118 /// {{#myblock param1 as |block_param1|}}
119 /// ...
120 /// {{/myblock}}
121 /// ```
122 ///
123pub fn get_block_param(&self, block_param_name: &str) -> Option<&BlockParamHolder> {
124self.block_params.get(block_param_name)
125 }
126127/// Reassign the block parameters for this block.
128pub fn set_block_params(&mut self, block_params: BlockParams<'rc>) {
129self.block_params = block_params;
130 }
131132/// Set a block parameter into this block.
133pub fn set_block_param(&mut self, key: &'rc str, value: BlockParamHolder) {
134self.block_params.data.insert(key, value);
135 }
136}