1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Hash validation utilities for `libvctrl_core`.
//!
//! # Purpose
//! This module provides utility functions to validate the structural integrity
//! of raw cryptographic hashes before they are converted into strongly-typed
//! [`Hash`](libvctrl_handler::Hash) objects.
//!
//! # Design rationale
//! - **Early Failure**: By validating the byte length before attempting to
//! construct a [`Hash`](libvctrl_handler::Hash), the system fails fast and
//! provides clear error messages, preventing panics in downstream code.
//! - **Compile-time Capability**: The validation function is a `const fn`,
//! allowing it to be used in `const` evaluation contexts to verify static
//! hash arrays at compile time.
//! - **Decoupling**: This logic is separated from the `Hash` constructor itself
//! to keep the data type pure and allow callers to perform pre-checks if
//! they are interacting with untrusted byte streams.
use ;
/// Validates that a byte slice is exactly [`HASH_LENGTH`] bytes long.
///
/// # Purpose
/// This function acts as a gatekeeper to ensure that any byte slice intended
/// to represent a [`Hash`](libvctrl_handler::Hash) meets the strict length
/// invariant (64 bytes) required by the system.
///
/// # Design rationale
/// - **`const fn`**: Being a `const fn` allows this check to be evaluated
/// during compilation if the inputs are known constants. This is useful for
/// verifying hardcoded hashes in configuration or test vectors.
/// - **Pre-conditions Check**: It is often used as a pre-check before calling
/// [`Hash::from_bytes`](libvctrl_handler::Hash::from_bytes) to provide custom
/// error handling or logging before the actual conversion.
///
/// # Internal mechanism
/// The function performs an `O(1)` comparison between the length of the
/// provided slice and the constant [`HASH_LENGTH`]. If they differ, it returns
/// a [`VctrlError::InvalidHashLength`] containing the incorrect length.
///
/// # Errors
/// Returns [`VctrlError::InvalidHashLength`](libvctrl_handler::VctrlError::InvalidHashLength)
/// if the length of `bytes` is not exactly equal to [`HASH_LENGTH`] (64).
///
/// # Examples
///
/// Validating a correctly sized slice:
///
/// ```
/// use libvctrl_core::validate::hash::validate_hash_bytes;
/// use libvctrl_handler::HASH_LENGTH;
///
/// let valid_bytes = [0u8; HASH_LENGTH];
/// assert!(validate_hash_bytes(&valid_bytes).is_ok());
/// ```
///
/// Validating an incorrectly sized slice:
///
/// ```
/// use libvctrl_core::validate::hash::validate_hash_bytes;
/// use libvctrl_handler::{HASH_LENGTH, VctrlError};
///
/// let invalid_bytes = [0u8; 32]; // Wrong length
/// let result = validate_hash_bytes(&invalid_bytes);
///
/// assert!(matches!(result, Err(VctrlError::InvalidHashLength(32))));
/// ```
pub const