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
use std::collections::BTreeMap;
use serde_json::value::Value as Json;
use crate::error::RenderError;
#[derive(Clone, Debug)]
pub enum BlockParamHolder {
Path(Vec<String>),
Value(Json),
}
impl BlockParamHolder {
pub fn value(v: Json) -> BlockParamHolder {
BlockParamHolder::Value(v)
}
pub fn path(r: Vec<String>) -> BlockParamHolder {
BlockParamHolder::Path(r)
}
}
#[derive(Clone, Debug, Default)]
pub struct BlockParams<'reg> {
data: BTreeMap<&'reg str, BlockParamHolder>,
}
impl<'reg> BlockParams<'reg> {
pub fn new() -> BlockParams<'reg> {
BlockParams::default()
}
pub fn add_path(&mut self, k: &'reg str, path: Vec<String>) -> Result<(), RenderError> {
self.data.insert(k, BlockParamHolder::path(path));
Ok(())
}
pub fn add_value(&mut self, k: &'reg str, v: Json) -> Result<(), RenderError> {
self.data.insert(k, BlockParamHolder::value(v));
Ok(())
}
pub fn get(&self, k: &str) -> Option<&BlockParamHolder> {
self.data.get(k)
}
}
#[derive(Debug, Clone, Default)]
pub struct BlockContext<'reg> {
base_path: Vec<String>,
base_value: Option<Json>,
block_params: BlockParams<'reg>,
local_variables: BTreeMap<String, Json>,
}
impl<'reg> BlockContext<'reg> {
pub fn new() -> BlockContext<'reg> {
BlockContext::default()
}
pub fn set_local_var(&mut self, name: String, value: Json) {
self.local_variables.insert(name, value);
}
pub fn get_local_var(&self, name: &str) -> Option<&Json> {
self.local_variables.get(&format!("@{}", name))
}
pub fn base_path(&self) -> &Vec<String> {
&self.base_path
}
pub fn base_path_mut(&mut self) -> &mut Vec<String> {
&mut self.base_path
}
pub fn base_value(&self) -> Option<&Json> {
self.base_value.as_ref()
}
pub fn set_base_value(&mut self, value: Json) {
self.base_value = Some(value);
}
pub fn get_block_param(&self, block_param_name: &str) -> Option<&BlockParamHolder> {
self.block_params.get(block_param_name)
}
pub fn set_block_params(&mut self, block_params: BlockParams<'reg>) {
self.block_params = block_params;
}
}