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
use super::output_format::OutputFormat;
use super::parsing_context::ParsingContext;
use super::parsing_task::ParsingTask;
use super::peek_char_iterator::PeekCharIterator;
use std::collections::HashMap;
pub struct ParsingTaskReplacePlaceholders;
impl ParsingTask for ParsingTaskReplacePlaceholders {
type Item = char;
type Output = String;
/// Called in case the context should be initialized
fn init<'a>(
inp: &'a str,
key_value: &'a HashMap<&'a str, String>,
) -> ParsingContext<'a, Self::Item> {
let vec: Vec<_> = inp.chars().collect();
ParsingContext::<'_, Self::Item> {
key_value,
iter: PeekCharIterator::new(vec),
vout: Vec::<char>::new(),
format: OutputFormat::None,
}
}
fn error(context: &mut ParsingContext<'_, Self::Item>) {
context.vout.extend(context.iter.get_mark2cur().unwrap());
}
fn process_char(context: &mut ParsingContext<'_, Self::Item>, ch: char) {
context.vout.push(ch);
}
fn process_char_placeholder(context: &mut ParsingContext<'_, Self::Item>, ch: char) {
context.vout.push(ch);
}
fn process_str_placeholder(context: &mut ParsingContext<'_, Self::Item>, arg: String) {
let Some(repl_str) = context.key_value.get(arg.as_str()) else {
Self::error(context);
return;
};
let repl = repl_str.chars();
match context.format {
OutputFormat::None => {
context.vout.extend(repl);
}
OutputFormat::LeftAlign(la) => {
context.vout.extend(repl.clone());
let value_len = repl.into_iter().count();
let len_diff = (la as i32) - (value_len as i32);
if len_diff > 0 {
for _i in 0..len_diff {
context.vout.push(' ');
}
}
}
OutputFormat::LeftAlignTrunc(la) => {
let value_len = repl.clone().count();
let len_diff = (la as i32) - (value_len as i32);
match len_diff {
_ if len_diff > 0 => {
context.vout.extend(repl);
for _i in 0..len_diff {
context.vout.push(' ');
}
}
_ if len_diff < 0 => {
// -1 due to …
let let_cmp = (value_len as i32) + len_diff - 1;
for (idx, ch) in repl.into_iter().enumerate() {
if idx >= let_cmp as usize {
break;
}
context.vout.push(ch);
}
context.vout.push('…');
}
_ => {
// len_diff ==0
context.vout.extend(repl);
}
}
}
OutputFormat::LeftAlignLTrunc(ra) => {
let value_len = repl.clone().count();
let len_diff = (ra as i32) - (value_len as i32);
match len_diff {
_ if len_diff > 0 => {
context.vout.extend(repl);
for _i in 0..len_diff {
context.vout.push(' ');
}
}
_ if len_diff < 0 => {
context.vout.push('…');
let mut iter = repl.into_iter();
for _ in 0..-len_diff + 1 {
// +1 due to …
iter.next();
}
context.vout.extend(iter);
}
_ => {
// len_diff ==0
context.vout.extend(repl);
}
}
}
OutputFormat::RightAlign(ra) => {
let value_len = repl.clone().count();
let len_diff = (ra as i32) - (value_len as i32);
if len_diff > 0 {
for _i in 0..len_diff {
context.vout.push(' ');
}
}
context.vout.extend(repl);
}
OutputFormat::RightAlignTrunc(ra) => {
let value_len = repl.clone().count();
let len_diff = (ra as i32) - (value_len as i32);
match len_diff {
_ if len_diff > 0 => {
for _i in 0..len_diff {
context.vout.push(' ');
}
context.vout.extend(repl);
}
_ if len_diff < 0 => {
// -1 due to …
let let_cmp = (value_len as i32) + len_diff - 1;
for (idx, ch) in repl.into_iter().enumerate() {
if idx >= let_cmp as usize {
break;
}
context.vout.push(ch);
}
context.vout.push('…');
}
_ => {
// len_diff ==0
context.vout.extend(repl);
}
}
}
OutputFormat::RightAlignLTrunc(ra) => {
let value_len = repl.clone().count();
let len_diff = (ra as i32) - (value_len as i32);
match len_diff {
_ if len_diff > 0 => {
for _i in 0..len_diff {
context.vout.push(' ');
}
context.vout.extend(repl);
}
_ if len_diff < 0 => {
context.vout.push('…');
let mut iter = repl.into_iter();
for _ in 0..-len_diff + 1 {
// +1 due to …
iter.next();
}
context.vout.extend(iter);
}
_ => {
// len_diff ==0
context.vout.extend(repl);
}
}
}
}
}
fn done(context: ParsingContext<'_, Self::Item>) -> Self::Output {
context.vout.into_iter().collect()
}
}