use crate::case::BenchmarkCase;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeSet;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Suite {
#[serde(default)]
pub name: String,
pub cases: Vec<BenchmarkCase>,
}
impl Suite {
pub fn from_json(s: &str) -> Result<Self> {
let suite: Self = serde_json::from_str(s).map_err(|e| Error::BadCase(e.to_string()))?;
suite.validate()?;
Ok(suite)
}
pub fn from_toml(s: &str) -> Result<Self> {
let suite: Self = toml::from_str(s).map_err(|e| Error::Parse(e.to_string()))?;
suite.validate()?;
Ok(suite)
}
pub(crate) fn validate(&self) -> Result<()> {
let mut seen: BTreeSet<&str> = BTreeSet::new();
for case in &self.cases {
case.validate()?;
if !seen.insert(case.id.as_str()) {
return Err(Error::BadCase(format!("duplicate case id: {}", case.id)));
}
}
Ok(())
}
#[must_use]
pub fn case_ids(&self) -> Vec<String> {
let mut ids: Vec<String> = self.cases.iter().map(|c| c.id.clone()).collect();
ids.sort();
ids
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct CaseResult {
pub id: String,
pub passed: bool,
pub hash_match: bool,
pub recomputed: Value,
pub hash: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct SuiteReport {
pub results: Vec<CaseResult>,
pub passed: usize,
pub failed: usize,
}