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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::{errors::StringTemplaterError, StringTemplaterOptions};
use std::collections::HashMap;
/// Inject values of a hashmap based on their field names.
/// Datas are injected on a specific string interpolation using the pattern `{{key_name}}`.
/// This allow a simple navigation around the datas.
pub fn generate_with_options(
template_str: &str,
data: &HashMap<String, String>,
option: &StringTemplaterOptions,
) -> Result<String, StringTemplaterError> {
let mut result = String::new();
let mut chars = template_str.chars().peekable();
while let Some(c) = chars.next() {
match c {
'{' => {
if let Some('{') = chars.peek() {
chars.next();
let mut apply_template = false;
let mut mirror = false;
let mut mirror_count = 0;
let mut key = String::new();
// Templating
if let Some('{') = chars.peek() {
apply_template = true;
chars.next();
}
// Mirroring
if let Some('*') = chars.peek() {
mirror = true;
chars.next();
mirror_count = 1;
while let Some('*') = chars.peek() {
chars.next();
mirror_count += 1;
}
}
// Key looking
while let Some(&next) = chars.peek() {
if next == '}' {
break;
} else if next == '\\' {
chars.next();
match chars.peek() {
Some('*') => {
chars.next();
key.push('*');
}
Some('\\') => {
chars.next();
key.push('\\');
}
Some('{') => {
chars.next();
key.push('{');
}
Some('}') => {
chars.next();
key.push('}');
}
_ => key.push('\\'),
}
} else {
key.push(chars.next().unwrap());
}
}
// Error handling
if let Some('}') = chars.peek() {
chars.next();
} else if apply_template {
return Err(StringTemplaterError::MissingCurvyBracket(format!(
"Missing three curvy bracket `}}` around `{}`.",
key
)));
} else {
return Err(StringTemplaterError::MissingCurvyBracket(format!(
"Missing two curvy bracket `}}` around `{}`.",
key
)));
}
if let Some('}') = chars.peek() {
chars.next();
} else if apply_template {
return Err(StringTemplaterError::MissingCurvyBracket(format!(
"Missing two curvy bracket `}}` around `{}`.",
key
)));
} else {
return Err(StringTemplaterError::MissingCurvyBracket(format!(
"Missing one curvy bracket `}}` around `{}`.",
key
)));
}
if apply_template {
if let Some('}') = chars.peek() {
chars.next();
} else {
return Err(StringTemplaterError::MissingCurvyBracket(format!(
"Missing one curvy bracket `}}` around `{}`.",
key
)));
}
}
// Data handling
if let Some(value) = data.get(&key) {
let data_value = if mirror {
// Apply C mirrors
let mut maybe_mirror = data.get(value);
mirror_count -= 1;
while mirror_count > 0 {
if let Some(value) = maybe_mirror {
maybe_mirror = data.get(value);
} else if option.safe_parse {
if option.display_missing_keys {
let patched_value = option.override_missing_keys.as_ref();
let patched_value = patched_value
.map(|f| f(value))
.unwrap_or(format!("[MISSING_KEY: `{}`]", value));
result.push_str(&patched_value);
}
} else {
return Err(StringTemplaterError::UnknownField(format!(
"The field `{}` does not exist in the hashmap.",
value
)));
}
mirror_count -= 1;
}
maybe_mirror
} else {
Some(value)
};
if let Some(value) = data_value {
if apply_template {
let generated = generate_with_options(value.as_str(), data, option);
if let Err(err) = generated {
return Err(err);
} else if let Ok(value) = generated {
result.push_str(&value);
}
} else {
result.push_str(value);
}
} else if option.safe_parse {
if option.display_missing_keys {
let patched_value = option.override_missing_keys.as_ref();
let patched_value = patched_value
.map(|f| f(value))
.unwrap_or(format!("[MISSING_KEY: `{}`]", value));
result.push_str(&patched_value);
}
} else {
return Err(StringTemplaterError::UnknownField(format!(
"The field `{}` does not exist in the hashmap.",
value
)));
}
} else if option.safe_parse {
if option.display_missing_keys {
let patched_value = option.override_missing_keys.as_ref();
let patched_value = patched_value
.map(|f| f(&key))
.unwrap_or(format!("[MISSING_KEY: `{}`]", key));
result.push_str(&patched_value);
}
} else {
return Err(StringTemplaterError::UnknownField(format!(
"The field `{}` does not exist in the hashmap.",
key
)));
}
} else {
result.push('{');
}
}
'\\' => {
if let Some(&next) = chars.peek() {
if next == '{' {
chars.next();
result.push('{');
} else if next == '}' {
chars.next();
result.push('}');
} else if next == '\\' {
chars.next();
result.push(c);
} else {
result.push(c);
}
} else {
result.push(c);
}
}
_ => result.push(c),
};
}
Ok(result)
}