clnrm_core/reporting/
json.rs1use crate::error::{CleanroomError, Result};
6use crate::validation::ValidationReport;
7use serde::Serialize;
8use std::path::Path;
9
10#[derive(Debug, Serialize)]
12pub struct JsonReport {
13 pub passed: bool,
15 pub total_passes: usize,
17 pub total_failures: usize,
19 pub passes: Vec<String>,
21 pub failures: Vec<FailureDetail>,
23}
24
25#[derive(Debug, Serialize)]
27pub struct FailureDetail {
28 pub name: String,
30 pub error: String,
32}
33
34pub struct JsonReporter;
36
37impl JsonReporter {
38 pub fn write(path: &Path, report: &ValidationReport) -> Result<()> {
52 let json_report = Self::convert_report(report);
53 let json_str = Self::serialize(&json_report)?;
54 Self::write_file(path, &json_str)
55 }
56
57 fn convert_report(report: &ValidationReport) -> JsonReport {
59 JsonReport {
60 passed: report.is_success(),
61 total_passes: report.passes().len(),
62 total_failures: report.failures().len(),
63 passes: report.passes().to_vec(),
64 failures: report
65 .failures()
66 .iter()
67 .map(|(name, error)| FailureDetail {
68 name: name.clone(),
69 error: error.clone(),
70 })
71 .collect(),
72 }
73 }
74
75 fn serialize(json_report: &JsonReport) -> Result<String> {
77 serde_json::to_string_pretty(json_report).map_err(|e| {
78 CleanroomError::serialization_error(format!("JSON serialization failed: {}", e))
79 })
80 }
81
82 fn write_file(path: &Path, content: &str) -> Result<()> {
84 std::fs::write(path, content).map_err(|e| {
85 CleanroomError::report_error(format!("Failed to write JSON report: {}", e))
86 })
87 }
88}