icydb 0.77.2

IcyDB — A type-safe, embedded ORM and schema system for the Internet Computer
Documentation
use crate::{design::prelude::*, traits::Validator};

///
/// Sha256
///
/// Validates canonical SHA-256 hex digests.
/// Accepted values are exactly 64 ASCII hexadecimal characters.
///

#[validator]
pub struct Sha256;

impl Validator<str> for Sha256 {
    fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
        // length check
        if s.len() != 64 {
            ctx.issue(format!("must be 64 characters, got {}", s.len()));
            return;
        }

        // hex characters
        if !s.chars().all(|c| c.is_ascii_hexdigit()) {
            ctx.issue("must contain only hexadecimal characters (0-9, a-f)".to_string());
        }
    }
}