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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Runtime context for tag values.
//! Stores resolved tag -> value mapping and handles {tag} expansion with pipe modifiers.
use anyhow::Result;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use crate::models::context::ContextModel;
/// Result of expanding a template with context values.
pub struct ExpandedTemplate {
/// Expanded template strings (one per combination if list tags were present).
pub items: Vec<String>,
/// Whether the expansion involved list tags (loop mode).
pub is_list: bool,
}
/// Runtime context holding resolved tag values.
pub struct Context {
values: HashMap<String, ContextModel>,
}
impl Context {
/// Create empty context.
pub fn new() -> Self {
Self {
values: HashMap::new(),
}
}
/// Store a resolved tag value.
pub fn set(&mut self, tag: &str, value: ContextModel) {
self.values.insert(tag.to_string(), value);
}
/// Get a resolved tag value.
pub fn get(&self, tag: &str) -> Option<&ContextModel> {
self.values.get(tag)
}
/// Fill {tag} placeholders and return ExpandedTemplate.
pub fn fill(&self, text: &str, escape: bool) -> Result<ExpandedTemplate> {
// Supports modifiers with special chars: {tag|trim:-}, {tag|join}, {tag|upper}
let re = Regex::new(r"\{(\w+)(?:\|([^}]+))?\}").unwrap();
let clean_text = Self::strip_all_template_quotes(text, escape);
let mut processed_tags = HashSet::new();
let mut results = vec![clean_text.clone()];
let mut is_list = false;
for cap in re.captures_iter(&clean_text) {
let placeholder = cap.get(0).unwrap().as_str();
let tag_name = cap.get(1).unwrap().as_str();
if processed_tags.contains(tag_name) {
continue;
}
if let Some(context_value) = self.values.get(tag_name) {
let modifier = cap.get(2).map(|m| m.as_str()).unwrap_or_default();
let processed_value = self.apply_modifier(modifier, context_value, escape)?;
match processed_value {
ContextModel::String(s) => {
let mut final_string = s;
if escape {
final_string = shell_words::quote(&final_string).to_string();
}
for current in results.iter_mut() {
*current = current.replace(placeholder, &final_string);
}
processed_tags.insert(tag_name.to_string());
}
ContextModel::List(items) => {
is_list = true;
let mut next = Vec::new();
for item in items {
let mut s = item.to_string();
if escape {
s = shell_words::quote(&s).to_string();
}
for current in &results {
next.push(current.replace(placeholder, &s));
}
}
results = next;
processed_tags.insert(tag_name.to_string());
}
_ => {}
}
}
}
Ok(ExpandedTemplate {
items: results,
is_list,
})
}
/// Apply pipe modifier to a resolved ContextModel value.
fn apply_modifier(
&self,
modifier: &str,
value: &ContextModel,
escape: bool,
) -> Result<ContextModel> {
let (name, arg) = modifier.split_once(':').unwrap_or((modifier, ""));
match name {
"join" => {
if let ContextModel::List(items) = value {
let mut buffer = String::new();
let mut seen = std::collections::HashSet::new();
let is_uniq = arg == "uniq";
for item in items {
let s = item.to_string();
if s.is_empty() {
continue;
}
if is_uniq && !seen.insert(s.clone()) {
continue;
}
if !buffer.is_empty() {
buffer.push('\n');
}
if escape {
write!(buffer, "{}", shell_words::quote(&s))?;
} else {
buffer.push_str(&s);
}
}
Ok(ContextModel::String(buffer))
} else {
anyhow::bail!("Modifier 'join' expects a list, but got a scalar value")
}
}
"upper" => {
if let ContextModel::String(s) = value {
Ok(ContextModel::String(s.to_uppercase()))
} else {
anyhow::bail!("Modifier 'upper' expects a string")
}
}
"lower" => {
if let ContextModel::String(s) = value {
Ok(ContextModel::String(s.to_lowercase()))
} else {
anyhow::bail!("Modifier 'lower' expects a string")
}
}
"trim" => {
let chars: Vec<char> = arg.chars().collect();
match value {
ContextModel::String(s) => {
let trimmed = s
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
if arg.is_empty() {
Ok(ContextModel::String(trimmed))
} else if trimmed == arg {
Ok(ContextModel::String(String::new()))
} else {
Ok(ContextModel::String(trimmed))
}
}
ContextModel::List(items) => {
let filtered: Vec<ContextModel> = items
.iter()
.filter(|i| {
let s = i
.to_string()
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
if arg.is_empty() {
!s.is_empty()
} else {
s != arg
}
})
.map(|i| {
let s = i
.to_string()
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
ContextModel::String(s)
})
.collect();
Ok(ContextModel::List(filtered))
}
_ => anyhow::bail!("Modifier 'trim' expects a string or list"),
}
}
_ => Ok(value.clone()),
}
}
/// Removes surrounding single or double quotes from any {tag} placeholder.
fn strip_all_template_quotes(text: &str, escape: bool) -> String {
if !escape {
return text.to_string();
}
let re_clean = regex::Regex::new(r#"(['"])\{(\w+)(?:\|([^}]+))?\}(['"])"#).unwrap();
re_clean
.replace_all(text, |caps: ®ex::Captures| {
let left = caps.get(1).unwrap().as_str();
let tag = caps.get(2).unwrap().as_str();
let modifier = caps
.get(3)
.map(|m| format!("|{}", m.as_str()))
.unwrap_or_default();
let right = caps.get(4).unwrap().as_str();
if left == right {
format!("{{{}{}}}", tag, modifier)
} else {
caps.get(0).unwrap().as_str().to_string()
}
})
.to_string()
}
}