Skip to main content

voce_validator/
report.rs

1//! Report generation — compilation quality metrics.
2//!
3//! Combines validation results, compilation output analysis, and IR
4//! summary into a single human-readable or JSON report.
5
6use crate::errors::ValidationResult;
7use crate::inspect;
8use crate::ir::VoceIr;
9
10/// A full compilation report.
11#[derive(Debug)]
12pub struct CompilationReport {
13    pub file: String,
14    pub validation_errors: usize,
15    pub validation_warnings: usize,
16    pub total_nodes: usize,
17    pub node_types: usize,
18    pub state_machines: usize,
19    pub has_metadata: bool,
20    pub has_theme: bool,
21    pub has_i18n: bool,
22    pub compiled_size: Option<usize>,
23}
24
25/// Generate a report from IR and validation results.
26pub fn generate_report(
27    file: &str,
28    ir: &VoceIr,
29    validation: &ValidationResult,
30    compiled_size: Option<usize>,
31) -> CompilationReport {
32    let summary = inspect::summarize(ir);
33
34    CompilationReport {
35        file: file.to_string(),
36        validation_errors: validation.error_count(),
37        validation_warnings: validation.warning_count(),
38        total_nodes: summary.total_nodes,
39        node_types: summary.node_counts.len(),
40        state_machines: summary.state_machines.len(),
41        has_metadata: summary.has_metadata,
42        has_theme: summary.has_theme,
43        has_i18n: summary.has_i18n,
44        compiled_size,
45    }
46}
47
48/// Print the report to stdout.
49pub fn print_report(report: &CompilationReport) {
50    println!("voce report: {}", report.file);
51    println!();
52    println!("  Validation:");
53    println!("    Errors:   {}", report.validation_errors);
54    println!("    Warnings: {}", report.validation_warnings);
55    let status = if report.validation_errors == 0 {
56        "PASS"
57    } else {
58        "FAIL"
59    };
60    println!("    Status:   {status}");
61    println!();
62    println!("  Structure:");
63    println!("    Nodes:          {}", report.total_nodes);
64    println!("    Node types:     {}", report.node_types);
65    println!("    State machines: {}", report.state_machines);
66    println!();
67    println!("  Features:");
68    println!(
69        "    SEO metadata: {}",
70        if report.has_metadata { "yes" } else { "no" }
71    );
72    println!(
73        "    Theme:        {}",
74        if report.has_theme { "yes" } else { "no" }
75    );
76    println!(
77        "    i18n:         {}",
78        if report.has_i18n { "yes" } else { "no" }
79    );
80
81    if let Some(size) = report.compiled_size {
82        println!();
83        println!("  Output:");
84        println!("    Size: {} bytes ({:.1} KB)", size, size as f64 / 1024.0);
85        let under_target = size < 10_000;
86        println!(
87            "    <10KB target: {}",
88            if under_target { "PASS" } else { "FAIL" }
89        );
90    }
91}
92
93/// Print report as JSON.
94pub fn print_report_json(report: &CompilationReport) {
95    let json = serde_json::json!({
96        "file": report.file,
97        "validation": {
98            "errors": report.validation_errors,
99            "warnings": report.validation_warnings,
100            "status": if report.validation_errors == 0 { "pass" } else { "fail" },
101        },
102        "structure": {
103            "total_nodes": report.total_nodes,
104            "node_types": report.node_types,
105            "state_machines": report.state_machines,
106        },
107        "features": {
108            "seo_metadata": report.has_metadata,
109            "theme": report.has_theme,
110            "i18n": report.has_i18n,
111        },
112        "output": {
113            "size_bytes": report.compiled_size,
114            "under_10kb": report.compiled_size.map(|s| s < 10_000),
115        },
116    });
117    println!("{}", serde_json::to_string_pretty(&json).unwrap());
118}