use std::collections::BTreeMap;
use crate::prelude::*;
pub const FILE: &str = ".wsb/test.json";
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct TestConfig {
#[serde(default)]
parity_families: BTreeMap<String, Vec<String>>,
}
pub fn declared(root: &Path) -> Result<BTreeMap<String, Vec<String>>> {
let path = root.join(FILE);
let text = match std::fs::read_to_string(&path) {
Ok(text) => text,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()),
Err(error) => return Err(error).with_context(|| format!("cannot read {}", path.display())),
};
let config: TestConfig = serde_json::from_str(&text)
.with_context(|| format!("{} is not a valid test configuration", path.display()))?;
Ok(config.parity_families)
}
pub fn members(root: &Path, family: &str) -> Result<Vec<String>> {
let families = declared(root)?;
match families.get(family) {
Some(members) => Ok(members.clone()),
None if families.is_empty() => bail!(
"no parity families are declared: {} names none",
root.join(FILE).display()
),
None => bail!(
"no family '{family}' is declared in {} (declared: {})",
root.join(FILE).display(),
families.keys().cloned().collect::<Vec<_>>().join(", ")
),
}
}
#[cfg(test)]
#[path = "families_tests.rs"]
mod tests;