Skip to main content

scene_corpus/
scene_corpus.rs

1//! Ad-hoc corpus runner for the M0 `.tscn`/`.tres` parser โ€” parses every scene/resource under a
2//! directory and reports panics + a problem summary (the robustness gate, Playbook ยง8.2).
3//!
4//! Usage: `cargo run -p gdscript-scene --example scene_corpus -- <dir> [--show]`
5
6use std::path::{Path, PathBuf};
7
8use gdscript_scene::parse_scene;
9
10fn collect(dir: &Path, out: &mut Vec<PathBuf>) {
11    let Ok(entries) = std::fs::read_dir(dir) else {
12        return;
13    };
14    for entry in entries.flatten() {
15        let path = entry.path();
16        if path.is_dir() {
17            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
18            if matches!(name, ".godot" | ".git" | "target" | "node_modules") {
19                continue;
20            }
21            collect(&path, out);
22        } else if matches!(
23            path.extension().and_then(|e| e.to_str()),
24            Some("tscn" | "tres")
25        ) {
26            out.push(path);
27        }
28    }
29}
30
31fn main() {
32    let args: Vec<String> = std::env::args().skip(1).collect();
33    let dir = args
34        .first()
35        .cloned()
36        .expect("usage: scene_corpus <dir> [--show]");
37    let show = args.iter().any(|a| a == "--show");
38
39    let mut files = Vec::new();
40    collect(Path::new(&dir), &mut files);
41    files.sort();
42
43    let (mut clean, mut with_problems, mut total_problems, mut nodes_total) = (0usize, 0, 0, 0);
44    let mut panics = Vec::new();
45    for path in &files {
46        let Ok(src) = std::fs::read_to_string(path) else {
47            continue; // non-UTF-8 / unreadable โ€” not a text scene
48        };
49        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| parse_scene(&src))) {
50            Ok(m) => {
51                nodes_total += m.nodes.len();
52                if m.problems.is_empty() {
53                    clean += 1;
54                } else {
55                    with_problems += 1;
56                    total_problems += m.problems.len();
57                    if show {
58                        println!("\n{}  ({} problems)", path.display(), m.problems.len());
59                        for p in &m.problems {
60                            println!("  {p:?}");
61                        }
62                    }
63                }
64            }
65            Err(_) => panics.push(path.clone()),
66        }
67    }
68
69    println!(
70        "\n=== scene corpus: {dir} ===\n  files:        {}\n  clean:        {clean}\n  with problems: {with_problems} ({total_problems} problems)\n  nodes parsed:  {nodes_total}\n  panics:        {}",
71        files.len(),
72        panics.len()
73    );
74    for p in &panics {
75        println!("  PANIC: {}", p.display());
76    }
77}