use crate::error::Result;
use crate::reporter::{EvalReport, Reporter};
pub struct JsonReporter {
pretty: bool,
}
impl JsonReporter {
pub fn new() -> Self {
Self { pretty: true }
}
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"));
}
}