vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
//! Shared test harness for committed KAT fixtures.

use super::case::{Case, Cases};

/// Parse `toml_contents` and run `run_case` for every `[[case]]` entry.
///
/// The closure is responsible for filtering by `case.op` if the fixture file
/// contains cases for multiple operations.
pub fn run_committed_kats<F>(toml_contents: &str, mut run_case: F) -> Result<(), String>
where
    F: FnMut(&Case) -> Result<(), String>,
{
    let cases: Cases = toml::from_str(toml_contents)
        .map_err(|error| format!("Fix: keep fixture TOML valid: {error}"))?;
    for case in cases.case {
        run_case(&case)?;
    }
    Ok(())
}

/// Decode an even-length lowercase hex string into a `Vec<u8>`.
///
/// # Errors
///
/// Returns an actionable error string when the hex string is malformed.
pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, String> {
    if hex.len() % 2 != 0 {
        return Err(format!(
            "Fix: fixture hex strings must contain an even number of digits, got {}",
            hex.len()
        ));
    }
    (0..hex.len())
        .step_by(2)
        .map(|idx| {
            u8::from_str_radix(&hex[idx..idx + 2], 16).map_err(|error| {
                format!("Fix: fixture hex byte at offset {idx} must be valid hexadecimal: {error}")
            })
        })
        .collect()
}