Skip to main content

voce_validator/
inspect.rs

1//! IR inspection — human-readable summary of an IR file.
2//!
3//! Does not run validation passes. Only reads and summarizes structure.
4
5use std::collections::HashMap;
6
7use crate::ir::{ChildNode, VoceIr};
8
9/// Summary of an IR document.
10pub struct IrSummary {
11    pub schema_version: String,
12    pub language: Option<String>,
13    pub total_nodes: usize,
14    pub max_depth: usize,
15    pub node_counts: HashMap<String, usize>,
16    pub state_machines: Vec<StateMachineSummary>,
17    pub has_routes: bool,
18    pub route_count: usize,
19    pub has_i18n: bool,
20    pub has_theme: bool,
21    pub has_metadata: bool,
22}
23
24pub struct StateMachineSummary {
25    pub name: String,
26    pub state_count: usize,
27    pub transition_count: usize,
28}
29
30/// Build a summary from a deserialized IR document.
31pub fn summarize(ir: &VoceIr) -> IrSummary {
32    let mut summary = IrSummary {
33        schema_version: format!("{}.{}", ir.schema_version_major, ir.schema_version_minor),
34        language: None,
35        total_nodes: 0,
36        max_depth: 0,
37        node_counts: HashMap::new(),
38        state_machines: Vec::new(),
39        has_routes: ir.routes.is_some(),
40        route_count: ir
41            .routes
42            .as_ref()
43            .and_then(|r| r.routes.as_ref())
44            .map_or(0, |r| r.len()),
45        has_i18n: ir.i18n.is_some(),
46        has_theme: ir.theme.is_some(),
47        has_metadata: false,
48    };
49
50    if let Some(ref root) = ir.root {
51        summary.total_nodes += 1; // ViewRoot
52        *summary
53            .node_counts
54            .entry("ViewRoot".to_string())
55            .or_default() += 1;
56        summary.language = root.document_language.clone();
57        summary.has_metadata = root.metadata.is_some();
58
59        if let Some(ref sems) = root.semantic_nodes {
60            summary.total_nodes += sems.len();
61            *summary
62                .node_counts
63                .entry("SemanticNode".to_string())
64                .or_default() += sems.len();
65        }
66
67        if let Some(ref children) = root.children {
68            count_nodes(children, 1, &mut summary);
69        }
70    }
71
72    summary
73}
74
75fn count_nodes(children: &[ChildNode], depth: usize, summary: &mut IrSummary) {
76    if depth > summary.max_depth {
77        summary.max_depth = depth;
78    }
79
80    for child in children {
81        summary.total_nodes += 1;
82        let type_name = child.type_name().to_string();
83        *summary.node_counts.entry(type_name.clone()).or_default() += 1;
84
85        // Collect StateMachine details
86        if type_name == "StateMachine" {
87            if let Some(sm) = child.as_type::<crate::ir::StateMachine>() {
88                summary.state_machines.push(StateMachineSummary {
89                    name: sm
90                        .name
91                        .or(sm.node_id)
92                        .unwrap_or_else(|| "<unnamed>".to_string()),
93                    state_count: sm.states.as_ref().map_or(0, |s| s.len()),
94                    transition_count: sm.transitions.as_ref().map_or(0, |t| t.len()),
95                });
96            }
97        }
98
99        if let Some(grandchildren) = child.children() {
100            count_nodes(&grandchildren, depth + 1, summary);
101        }
102    }
103}
104
105/// Print the summary to stdout.
106pub fn print_summary(file: &str, summary: &IrSummary) {
107    println!("voce inspect: {file}");
108    println!();
109    println!("  Schema:     v{}", summary.schema_version);
110    if let Some(ref lang) = summary.language {
111        println!("  Language:   {lang}");
112    }
113    println!("  Nodes:      {} total", summary.total_nodes);
114    println!("  Tree depth: {} levels", summary.max_depth);
115    println!();
116
117    // Node type counts (sorted by count descending)
118    let mut counts: Vec<_> = summary.node_counts.iter().collect();
119    counts.sort_by(|a, b| b.1.cmp(a.1));
120
121    println!("  Node types:");
122    for (type_name, count) in &counts {
123        println!("    {type_name:<24} {count}");
124    }
125    println!();
126
127    // State machines
128    if !summary.state_machines.is_empty() {
129        println!("  State machines: {}", summary.state_machines.len());
130        for sm in &summary.state_machines {
131            println!(
132                "    - {} ({} states, {} transitions)",
133                sm.name, sm.state_count, sm.transition_count
134            );
135        }
136        println!();
137    }
138
139    // Features
140    println!(
141        "  Routes:   {}",
142        if summary.has_routes {
143            format!("{}", summary.route_count)
144        } else {
145            "none".to_string()
146        }
147    );
148    println!(
149        "  i18n:     {}",
150        if summary.has_i18n {
151            "configured"
152        } else {
153            "not used"
154        }
155    );
156    println!(
157        "  Theme:    {}",
158        if summary.has_theme { "yes" } else { "none" }
159    );
160    println!(
161        "  Metadata: {}",
162        if summary.has_metadata { "yes" } else { "none" }
163    );
164}