textfsm-rust 0.3.1

Template-based state machine for parsing semi-formatted text based on Google's TextFSM
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Strict NTC Templates test harness for textfsm-rs.
//!
//! Discovers all test cases in the ntc-templates repository, runs them
//! through textfsm-rs, and reports results with strict field-by-field
//! comparison against the YAML expected output.
//!
//! This harness uses identical discovery and comparison logic to
//! `tests/python_ntc_harness.py` so that pass/fail/error counts are
//! directly comparable between the Python and Rust implementations.
//!
//! Usage:
//!     cargo run --example strict_ntc_test --features serde -- /path/to/ntc-templates

use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use textfsm_rust::{ListItem, Template, Value};
use walkdir::WalkDir;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

struct TestCase {
    raw_path: PathBuf,
    yml_path: PathBuf,
    template_path: PathBuf,
    template_name: String,
    test_name: String,
}

enum TestResult {
    Pass,
    Fail(String),
    Error(String),
}

// ---------------------------------------------------------------------------
// Discovery
// ---------------------------------------------------------------------------

/// Discover all .raw/.yml test case pairs and their templates.
///
/// Test layout: `tests/{platform}/{command}/{platform}_{command}[suffix].raw`
/// Template:    `ntc_templates/templates/{platform}_{command}.textfsm`
///
/// The template is derived from the directory path, not the filename,
/// so that suffixed test files (e.g. `cisco_ios_show_arp2.raw`) correctly
/// map to the base template (`cisco_ios_show_arp.textfsm`).
fn discover_test_cases(tests_dir: &Path, templates_dir: &Path) -> Vec<TestCase> {
    let mut cases = Vec::new();

    // Collect and sort all .yml files
    let mut yml_paths: Vec<PathBuf> = WalkDir::new(tests_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.path()
                .extension()
                .and_then(|ext| ext.to_str())
                .is_some_and(|ext| ext == "yml")
        })
        .map(|e| e.path().to_path_buf())
        .collect();

    yml_paths.sort();

    for yml_path in yml_paths {
        let raw_path = yml_path.with_extension("raw");
        if !raw_path.exists() {
            continue;
        }

        // Extract platform/command from directory structure
        let rel = match yml_path.strip_prefix(tests_dir) {
            Ok(r) => r,
            Err(_) => continue,
        };

        let parts: Vec<&str> = rel
            .components()
            .filter_map(|c| c.as_os_str().to_str())
            .collect();

        if parts.len() < 3 {
            continue;
        }

        let platform = parts[0];
        let command = parts[1];
        let template_name = format!("{}_{}.textfsm", platform, command);
        let template_path = templates_dir.join(&template_name);

        if !template_path.exists() {
            continue;
        }

        let test_name = yml_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("")
            .to_string();

        cases.push(TestCase {
            raw_path,
            yml_path,
            template_path,
            template_name,
            test_name,
        });
    }

    cases
}

// ---------------------------------------------------------------------------
// Value comparison
// ---------------------------------------------------------------------------

/// Compare an actual textfsm-rs Value with an expected serde_yaml Value.
///
/// Normalization rules (matching the Python harness):
///   - Empty / null → empty string
///   - YAML bool → "True" / "False" (Python's str(bool))
///   - YAML int/float → string
///   - Lists → element-wise comparison
///   - Dicts in lists → key-value comparison
fn values_match(actual: &Value, expected: &serde_yaml::Value) -> bool {
    match (actual, expected) {
        // Empty matches null or empty string
        (Value::Empty, serde_yaml::Value::Null) => true,
        (Value::Empty, serde_yaml::Value::String(s)) => s.is_empty(),

        // Single string matches various YAML scalars
        (Value::Single(s), serde_yaml::Value::String(e)) => s == e,
        (Value::Single(s), serde_yaml::Value::Number(n)) => *s == n.to_string(),
        (Value::Single(s), serde_yaml::Value::Bool(b)) => {
            *s == if *b { "True" } else { "False" }
        }
        (Value::Single(s), serde_yaml::Value::Null) => s.is_empty(),

        // List matches sequence
        (Value::List(items), serde_yaml::Value::Sequence(expected_items)) => {
            if items.len() != expected_items.len() {
                return false;
            }
            items
                .iter()
                .zip(expected_items.iter())
                .all(|(a, e)| list_item_matches(a, e))
        }
        // Empty list matches null or empty sequence
        (Value::List(items), serde_yaml::Value::Null) => items.is_empty(),

        _ => false,
    }
}

/// Compare a single list item with a YAML value.
fn list_item_matches(actual: &ListItem, expected: &serde_yaml::Value) -> bool {
    match (actual, expected) {
        (ListItem::String(s), serde_yaml::Value::String(e)) => s == e,
        (ListItem::String(s), serde_yaml::Value::Number(n)) => *s == n.to_string(),
        (ListItem::String(s), serde_yaml::Value::Bool(b)) => {
            *s == if *b { "True" } else { "False" }
        }
        (ListItem::String(s), serde_yaml::Value::Null) => s.is_empty(),
        (ListItem::Dict(d), serde_yaml::Value::Mapping(m)) => {
            if d.len() != m.len() {
                return false;
            }
            m.iter().all(|(k, v)| {
                let key = match k.as_str() {
                    Some(s) => s,
                    None => return false,
                };
                match d.get(key) {
                    Some(actual_val) => match v {
                        serde_yaml::Value::String(ev) => actual_val == ev,
                        serde_yaml::Value::Number(n) => *actual_val == n.to_string(),
                        serde_yaml::Value::Bool(b) => {
                            *actual_val == if *b { "True" } else { "False" }
                        }
                        serde_yaml::Value::Null => actual_val.is_empty(),
                        _ => false,
                    },
                    None => false,
                }
            })
        }
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Display helpers
// ---------------------------------------------------------------------------

fn format_value(val: &Value) -> String {
    match val {
        Value::Empty => "\"\"".to_string(),
        Value::Single(s) => format!("\"{}\"", s),
        Value::List(items) => {
            let items_str: Vec<String> = items
                .iter()
                .map(|item| match item {
                    ListItem::String(s) => format!("\"{}\"", s),
                    ListItem::Dict(d) => {
                        let pairs: Vec<String> =
                            d.iter().map(|(k, v)| format!("'{}': '{}'", k, v)).collect();
                        format!("{{{}}}", pairs.join(", "))
                    }
                })
                .collect();
            format!("[{}]", items_str.join(", "))
        }
    }
}

fn format_yaml_value(val: &serde_yaml::Value) -> String {
    match val {
        serde_yaml::Value::Null => "\"\"".to_string(),
        serde_yaml::Value::Bool(b) => {
            format!("\"{}\"", if *b { "True" } else { "False" })
        }
        serde_yaml::Value::Number(n) => format!("\"{}\"", n),
        serde_yaml::Value::String(s) => format!("\"{}\"", s),
        serde_yaml::Value::Sequence(items) => {
            let items_str: Vec<String> = items.iter().map(format_yaml_value).collect();
            format!("[{}]", items_str.join(", "))
        }
        serde_yaml::Value::Mapping(m) => {
            let items_str: Vec<String> = m
                .iter()
                .map(|(k, v)| format!("{}: {}", format_yaml_value(k), format_yaml_value(v)))
                .collect();
            format!("{{{}}}", items_str.join(", "))
        }
        _ => format!("{:?}", val),
    }
}

// ---------------------------------------------------------------------------
// Test runner
// ---------------------------------------------------------------------------

fn run_test(tc: &TestCase) -> TestResult {
    // Load expected YAML
    let yml_content = match fs::read_to_string(&tc.yml_path) {
        Ok(c) => c,
        Err(e) => return TestResult::Error(format!("Cannot read YAML: {}", e)),
    };

    let yaml_data: serde_yaml::Value = match serde_yaml::from_str(&yml_content) {
        Ok(d) => d,
        Err(e) => return TestResult::Error(format!("YAML parse error: {}", e)),
    };

    let expected: &Vec<serde_yaml::Value> = match yaml_data.get("parsed_sample") {
        Some(serde_yaml::Value::Sequence(items)) => items,
        Some(serde_yaml::Value::Null) | None => {
            // No expected output — treat as expecting 0 records.
            // Fall through to an empty reference to simplify the comparison below.
            &Vec::new()
        }
        _ => return TestResult::Error("Invalid parsed_sample format".into()),
    };

    // Parse template
    let template_content = match fs::read_to_string(&tc.template_path) {
        Ok(c) => c,
        Err(e) => return TestResult::Error(format!("Cannot read template: {}", e)),
    };

    let template = match Template::parse_str(&template_content) {
        Ok(t) => t,
        Err(e) => return TestResult::Error(format!("Template parse error: {}", e)),
    };

    // Parse raw input
    let raw_content = match fs::read_to_string(&tc.raw_path) {
        Ok(c) => c,
        Err(e) => return TestResult::Error(format!("Cannot read raw input: {}", e)),
    };

    let mut parser = template.parser();
    let results = match parser.parse_text(&raw_content) {
        Ok(r) => r,
        Err(e) => return TestResult::Error(format!("Parse error: {}", e)),
    };

    let header: Vec<String> = template.header().iter().map(|s| s.to_lowercase()).collect();

    // Compare row counts
    if results.len() != expected.len() {
        return TestResult::Fail(format!(
            "row count: got {}, expected {}",
            results.len(),
            expected.len()
        ));
    }

    // Compare each row field-by-field
    for (row_idx, (actual_row, expected_item)) in
        results.iter().zip(expected.iter()).enumerate()
    {
        let expected_map = match expected_item.as_mapping() {
            Some(m) => m,
            None => {
                return TestResult::Error(format!(
                    "Expected row {} is not a mapping",
                    row_idx
                ))
            }
        };

        for (yml_key, yml_val) in expected_map {
            let col_name = match yml_key.as_str() {
                Some(s) => s.to_lowercase(),
                None => continue,
            };

            // Find column index in header.
            // If the column doesn't exist in the template, treat as Empty
            // (matching Python's dict.get(col, "") behavior).
            let actual_val = match header.iter().position(|h| *h == col_name) {
                Some(idx) => &actual_row[idx],
                None => &Value::Empty,
            };

            if !values_match(actual_val, yml_val) {
                return TestResult::Fail(format!(
                    "row {}, col '{}': got {}, expected {}",
                    row_idx,
                    col_name,
                    format_value(actual_val),
                    format_yaml_value(yml_val),
                ));
            }
        }
    }

    TestResult::Pass
}

// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: strict_ntc_test /path/to/ntc-templates");
        std::process::exit(1);
    }

    let ntc_dir = PathBuf::from(&args[1]);
    let templates_dir = ntc_dir.join("ntc_templates").join("templates");
    let tests_dir = ntc_dir.join("tests");

    if !templates_dir.exists() {
        eprintln!("Error: Templates directory not found: {:?}", templates_dir);
        std::process::exit(1);
    }
    if !tests_dir.exists() {
        eprintln!("Error: Tests directory not found: {:?}", tests_dir);
        std::process::exit(1);
    }

    // Discover test cases
    let test_cases = discover_test_cases(&tests_dir, &templates_dir);
    println!("Discovered {} test cases\n", test_cases.len());

    // Run tests
    let mut passed: Vec<&TestCase> = Vec::new();
    let mut failed: Vec<(&TestCase, String)> = Vec::new();
    let mut errors: Vec<(&TestCase, String)> = Vec::new();

    for tc in &test_cases {
        match run_test(tc) {
            TestResult::Pass => passed.push(tc),
            TestResult::Fail(msg) => failed.push((tc, msg)),
            TestResult::Error(msg) => errors.push((tc, msg)),
        }
    }

    let total = test_cases.len();
    let pass_rate = if total > 0 {
        passed.len() as f64 / total as f64 * 100.0
    } else {
        0.0
    };

    // Report
    println!("{}", "=".repeat(60));
    println!(" Strict NTC Template Test Results (Rust textfsm-rs)");
    println!("{}", "=".repeat(60));
    println!("Total:     {}", total);
    println!("Passed:    {}", passed.len());
    println!("Failed:    {}", failed.len());
    println!("Errors:    {}", errors.len());
    println!("Pass Rate: {:.2}%", pass_rate);
    println!();

    if !errors.is_empty() {
        println!("--- Errors ({}) ---", errors.len());
        for (tc, msg) in &errors {
            println!("  {} [{}]: {}", tc.template_name, tc.test_name, msg);
        }
        println!();
    }

    if !failed.is_empty() {
        println!("--- Failures ({}) ---", failed.len());
        for (tc, msg) in &failed {
            println!("  {} [{}]: {}", tc.template_name, tc.test_name, msg);
        }
        println!();
    }

    println!("{}", "=".repeat(60));

    if !failed.is_empty() || !errors.is_empty() {
        std::process::exit(1);
    }
}