swarm-engine-eval 0.1.6

Evaluation framework for SwarmEngine
Documentation
//! JSON reporter

use crate::error::Result;
use crate::reporter::{EvalReport, Reporter};

/// JSON reporter
pub struct JsonReporter {
    /// Pretty print
    pretty: bool,
}

impl JsonReporter {
    /// Create new JSON reporter
    pub fn new() -> Self {
        Self { pretty: true }
    }

    /// Set pretty printing
    pub fn with_pretty(mut self, pretty: bool) -> Self {
        self.pretty = pretty;
        self
    }
}

impl Default for JsonReporter {
    fn default() -> Self {
        Self::new()
    }
}

impl Reporter for JsonReporter {
    fn generate(&self, report: &EvalReport) -> Result<String> {
        let json = if self.pretty {
            serde_json::to_string_pretty(report)?
        } else {
            serde_json::to_string(report)?
        };
        Ok(json)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregator::AggregatedResults;
    use crate::reporter::{ConfigSummary, SeedInfo};

    #[test]
    fn test_json_reporter() {
        let report = EvalReport {
            config_summary: ConfigSummary {
                scenario_name: "test".to_string(),
                scenario_id: "test:v1".to_string(),
                worker_count: 2,
                max_ticks: 100,
                run_count: 10,
            },
            seed_info: SeedInfo {
                base_seed: 12345,
                run_seeds: vec![12345],
            },
            runs: vec![],
            aggregated: AggregatedResults::default(),
            assertion_results: vec![],
        };

        let reporter = JsonReporter::new();
        let json = reporter.generate(&report).unwrap();
        assert!(json.contains("config_summary"));
        assert!(json.contains("aggregated"));
    }
}