invoance 0.2.0

Official Rust SDK for the Invoance compliance API
Documentation
//! Shared client-side input validators.

use crate::error::Error;

/// Validate that a string is a 64-character lowercase hex SHA-256 digest.
///
/// Returns a [`ValidationError`](crate::error::ApiErrorKind::Validation) with a
/// helpful message when it isn't. Applied to `document_hash`, `payload_hash`,
/// and `content_hash` before they are sent to the API.
pub(crate) fn assert_sha256_hex(field_name: &str, value: &str) -> Result<(), Error> {
    if value.len() != 64 {
        return Err(Error::validation(format!(
            "{field_name} must be 64 hex chars (got {} chars)",
            value.len()
        )));
    }
    if !value
        .bytes()
        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
    {
        let prefix: String = value.chars().take(16).collect();
        return Err(Error::validation(format!(
            "{field_name} must be lowercase hex [0-9a-f]; \"{prefix}\" is not"
        )));
    }
    Ok(())
}