use datumesh_core::json;
use datumesh_core::lexical::{check_hex, check_instant};
use datumesh_core::ErrorCode;
use crate::error::{Error, Result};
use crate::FunctionTest;
const RESULTS: &[&str] = &["pass", "fail", "error"];
fn invalid(path: &str, detail: &str) -> Error {
Error::Invalid(datumesh_core::Error::new(ErrorCode::Schema, path, detail))
}
pub(crate) fn check_case(case: &str) -> Result<()> {
check_hex(case, 16, "case")?;
Ok(())
}
pub(crate) fn check_result(result: &str) -> Result<()> {
if RESULTS.contains(&result) {
Ok(())
} else {
Err(invalid("result", "a result is pass, fail or error"))
}
}
pub(crate) fn branch_counts(total: u32, covered: u32) -> Result<(i32, i32)> {
if covered > total {
return Err(invalid("branches_covered", "more than branches_total"));
}
let as_int = |n: u32, path: &str| i32::try_from(n).map_err(|_| invalid(path, "more than 2147483647"));
Ok((as_int(total, "branches_total")?, as_int(covered, "branches_covered")?))
}
pub(crate) fn check_test(t: &FunctionTest) -> Result<(i32, i32)> {
check_hex(&t.fn_hash, 64, "fn_hash")?;
check_case(&t.case)?;
if json::parse(&t.test_json)?.to_jcs() != t.test_json {
return Err(invalid("test_json", "the test line is not canonical JSON"));
}
if t.signed_by.is_empty() || t.signed_by.len() > 256 {
return Err(invalid("signed_by", "a signer of 1 to 256 bytes"));
}
if let Some(at) = &t.last_run_at {
check_instant(at, "last_run_at")?;
}
if let Some(r) = &t.last_result {
check_result(r)?;
}
branch_counts(t.branches_total, t.branches_covered)
}
pub(crate) fn result_counts(result: Option<&str>) -> [i64; 4] {
match result {
Some("pass") => [1, 0, 0, 0],
Some("fail") => [0, 1, 0, 0],
Some("error") => [0, 0, 1, 0],
_ => [0, 0, 0, 1],
}
}