Skip to main content

swarm_engine_eval/reporter/
json.rs

1//! JSON reporter
2
3use crate::error::Result;
4use crate::reporter::{EvalReport, Reporter};
5
6/// JSON reporter
7pub struct JsonReporter {
8    /// Pretty print
9    pretty: bool,
10}
11
12impl JsonReporter {
13    /// Create new JSON reporter
14    pub fn new() -> Self {
15        Self { pretty: true }
16    }
17
18    /// Set pretty printing
19    pub fn with_pretty(mut self, pretty: bool) -> Self {
20        self.pretty = pretty;
21        self
22    }
23}
24
25impl Default for JsonReporter {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl Reporter for JsonReporter {
32    fn generate(&self, report: &EvalReport) -> Result<String> {
33        let json = if self.pretty {
34            serde_json::to_string_pretty(report)?
35        } else {
36            serde_json::to_string(report)?
37        };
38        Ok(json)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::aggregator::AggregatedResults;
46    use crate::reporter::{ConfigSummary, SeedInfo};
47
48    #[test]
49    fn test_json_reporter() {
50        let report = EvalReport {
51            config_summary: ConfigSummary {
52                scenario_name: "test".to_string(),
53                scenario_id: "test:v1".to_string(),
54                worker_count: 2,
55                max_ticks: 100,
56                run_count: 10,
57            },
58            seed_info: SeedInfo {
59                base_seed: 12345,
60                run_seeds: vec![12345],
61            },
62            runs: vec![],
63            aggregated: AggregatedResults::default(),
64            assertion_results: vec![],
65        };
66
67        let reporter = JsonReporter::new();
68        let json = reporter.generate(&report).unwrap();
69        assert!(json.contains("config_summary"));
70        assert!(json.contains("aggregated"));
71    }
72}