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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! FSM parser for processing input text.
mod state;
pub use state::ValueState;
use std::collections::HashMap;
use crate::error::ParseError;
use crate::template::Template;
use crate::types::{LineOp, RecordOp, Transition, Value};
/// Parser for processing text with a compiled template.
pub struct Parser<'t> {
/// Reference to the compiled template.
template: &'t Template,
/// Current state name.
current_state: String,
/// Runtime state for each value.
value_states: Vec<ValueState>,
/// Accumulated results.
results: Vec<Vec<Value>>,
}
impl<'t> Parser<'t> {
/// Create a new parser for the given template.
pub fn new(template: &'t Template) -> Self {
let value_states = template
.values()
.iter()
.enumerate()
.map(|(idx, def)| ValueState::new(def.clone(), idx))
.collect();
Self {
template,
current_state: "Start".to_string(),
value_states,
results: Vec::new(),
}
}
/// Reset the parser state for reuse.
pub fn reset(&mut self) {
self.current_state = "Start".to_string();
self.results.clear();
for vs in &mut self.value_states {
vs.clear_all();
}
}
/// Parse text and return list of records.
pub fn parse_text(&mut self, text: &str) -> Result<Vec<Vec<Value>>, ParseError> {
self.parse_text_with_eof(text, true)
}
/// Parse text with explicit EOF control.
pub fn parse_text_with_eof(
&mut self,
text: &str,
eof: bool,
) -> Result<Vec<Vec<Value>>, ParseError> {
for line in text.lines() {
self.process_line(line)?;
if self.current_state == "End" || self.current_state == "EOF" {
break;
}
}
// Implicit EOF behavior
if self.current_state != "End"
&& self.template.get_state("EOF").is_none()
&& eof
{
self.append_record();
}
Ok(std::mem::take(&mut self.results))
}
/// Parse text and return results as list of dicts.
pub fn parse_text_to_dicts(
&mut self,
text: &str,
) -> Result<Vec<HashMap<String, String>>, ParseError> {
let results = self.parse_text(text)?;
let header = self.template.header();
Ok(results
.into_iter()
.map(|row| {
header
.iter()
.zip(row)
.map(|(k, v)| (k.to_lowercase(), v.as_string()))
.collect()
})
.collect())
}
/// Parse text and deserialize results into typed structs.
///
/// This method parses the input text and deserializes each record directly
/// into the specified type `T` using serde.
///
/// # Example
///
/// ```rust,ignore
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Interface {
/// interface: String,
/// status: String,
/// }
///
/// let interfaces: Vec<Interface> = parser.parse_text_into(input)?;
/// ```
#[cfg(feature = "serde")]
pub fn parse_text_into<T>(&mut self, text: &str) -> Result<Vec<T>, ParseError>
where
T: serde::de::DeserializeOwned,
{
let results = self.parse_text(text)?;
// Pre-compute lowercased headers once, not per-record
let header: Vec<String> = self
.template
.header()
.iter()
.map(|s| s.to_lowercase())
.collect();
results
.into_iter()
.map(|record| {
// Use optimized path that borrows pre-lowercased headers
crate::de::from_record_borrowed(&header, record)
.map_err(|e| ParseError::DeserializeError(e.to_string()))
})
.collect()
}
/// Process a single input line.
fn process_line(&mut self, line: &str) -> Result<(), ParseError> {
let state = match self.template.get_state(&self.current_state) {
Some(s) => s,
None => return Ok(()), // End/EOF state
};
for rule in &state.rules {
if let Ok(Some(captures)) = rule.regex.captures(line) {
// Extract matched values.
//
// For each value, if the named group matched, assign the captured
// text. If the group exists in the rule but didn't capture (optional
// group), call assign_none() to clear the value — matching Python's
// behavior where groupdict() yields None and AssignVar(None) is called.
for vs in &mut self.value_states {
if let Some(matched) = captures.name(&vs.def.name) {
vs.assign(matched.as_str().to_string(), &mut self.results);
} else if rule.regex_pattern.contains(&format!("(?P<{}>", vs.def.name)) {
vs.assign_none();
}
}
// Apply record operation
match rule.record_op {
RecordOp::Record => self.append_record(),
RecordOp::Clear => self.clear_values(),
RecordOp::ClearAll => self.clear_all_values(),
RecordOp::NoRecord => {}
}
// Apply line operation
match rule.line_op {
LineOp::Error => {
let message = match &rule.transition {
Transition::State(msg) => msg.clone(),
_ => "state error".into(),
};
return Err(ParseError::RuleError {
rule_line: rule.line_num,
message,
});
}
LineOp::Continue => {
// Don't break, continue checking rules
continue;
}
LineOp::Next => {
// Apply state transition and break
self.apply_transition(&rule.transition);
break;
}
}
}
}
Ok(())
}
/// Append current record to results.
fn append_record(&mut self) {
// Check Required constraints
for vs in &self.value_states {
if !vs.satisfies_required() {
// Skip record
self.clear_values();
return;
}
}
// Build record
let record: Vec<Value> = self
.value_states
.iter_mut()
.map(|vs| vs.take_for_record())
.collect();
// Don't record if all empty
if record.iter().all(|v| v.is_empty()) {
return;
}
self.results.push(record);
self.clear_values();
}
/// Clear non-Filldown values.
fn clear_values(&mut self) {
for vs in &mut self.value_states {
vs.clear();
}
}
/// Clear all values including Filldown.
fn clear_all_values(&mut self) {
for vs in &mut self.value_states {
vs.clear_all();
}
}
/// Apply a state transition.
fn apply_transition(&mut self, transition: &Transition) {
match transition {
Transition::Stay => {}
Transition::State(name) => {
self.current_state = name.clone();
}
Transition::End => {
self.current_state = "End".to_string();
}
Transition::Eof => {
self.current_state = "EOF".to_string();
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::template::Template;
#[test]
fn test_simple_parse() {
let template_str = r#"Value Interface (\S+)
Value Status (up|down)
Start
^Interface: ${Interface} is ${Status} -> Record
"#;
let template = Template::parse_str(template_str).unwrap();
let mut parser = template.parser();
let input = "Interface: eth0 is up\nInterface: eth1 is down\n";
let results = parser.parse_text(input).unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0][0], Value::Single("eth0".into()));
assert_eq!(results[0][1], Value::Single("up".into()));
assert_eq!(results[1][0], Value::Single("eth1".into()));
assert_eq!(results[1][1], Value::Single("down".into()));
}
#[test]
fn test_parse_to_dicts() {
let template_str = r#"Value Name (\S+)
Value Age (\d+)
Start
^Name: ${Name}, Age: ${Age} -> Record
"#;
let template = Template::parse_str(template_str).unwrap();
let mut parser = template.parser();
let input = "Name: Alice, Age: 30\nName: Bob, Age: 25\n";
let results = parser.parse_text_to_dicts(input).unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0].get("name"), Some(&"Alice".to_string()));
assert_eq!(results[0].get("age"), Some(&"30".to_string()));
}
#[test]
fn test_required_skips_empty() {
let template_str = r#"Value Required Name (\S+)
Value Optional (\S+)
Start
^Name: ${Name}
^Optional: ${Optional}
^--- -> Record
"#;
let template = Template::parse_str(template_str).unwrap();
let mut parser = template.parser();
// Record with no Name should be skipped
let input = "Optional: foo\n---\nName: bar\n---\n";
let results = parser.parse_text(input).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0][0], Value::Single("bar".into()));
}
#[test]
fn test_filldown_clears_when_optional_group_unmatched() {
// Simulates the bug with templates like arista_eos_show_ip_bgp_detail
// where a Filldown value with an optional capture should be cleared
// when the group doesn't participate in a match.
let template_str = r#"Value Filldown PREFIX (\S+)
Value Filldown PREFIX_LENGTH (\d+)
Start
^Prefix:\s+${PREFIX}\s*(len:\s*${PREFIX_LENGTH})?
^--- -> Record
EOF
"#;
let template = Template::parse_str(template_str).unwrap();
let mut parser = template.parser();
// First record: both groups match (PREFIX and PREFIX_LENGTH)
// Second record: PREFIX matches but PREFIX_LENGTH optional group doesn't
let input = "\
Prefix: 10.0.0.0 len: 24
---
Prefix: 192.168.1.0
---
";
let results = parser.parse_text(input).unwrap();
assert_eq!(results.len(), 2);
// First record: both values captured
assert_eq!(results[0][0], Value::Single("10.0.0.0".into()));
assert_eq!(results[0][1], Value::Single("24".into()));
// Second record: PREFIX_LENGTH should be Empty (not stale "24" from Filldown)
assert_eq!(results[1][0], Value::Single("192.168.1.0".into()));
assert_eq!(results[1][1], Value::Empty);
}
#[test]
fn test_rule_with_escaped_angle_brackets() {
// Templates like fortinet_get_system_ha_status use \< in rules
let template_str = r#"Value DateTime (\S+)
Start
^\s*<${DateTime}> -> Record
"#;
let template = Template::parse_str(template_str).unwrap();
let mut parser = template.parser();
let input = " <2020/11/18> some text\n";
let results = parser.parse_text(input).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0][0], Value::Single("2020/11/18".into()));
}
}