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
//! Hash validation utilities.
//!
//! # Architecture
//! This module provides standalone validation for byte slices intended to be used
//! as Git object hashes. It ensures that data read from untrusted sources (like
//! network packfiles) is the correct length before attempting to construct a
//! [`Hash`](crate::Hash) type.
//!
//! # Design Rationale: Compile-Time Evaluation
//! The primary validation function is implemented as a `const fn`. This is a
//! critical architectural decision: it allows validation to occur at compile time
//! if the input byte slice is a known constant. This shifts the computational
//! overhead to the compiler, achieving true zero-cost runtime validation for
//! static data.
use crateHASH_LENGTH;
use crateVctrlError;
/// Validates that a byte slice is exactly `HASH_LENGTH` bytes long.
///
/// # Why this exists
/// Git's SHA-512 implementation requires exactly 64 bytes. Passing a slice of
/// incorrect length to a hash constructor would either cause a runtime panic
/// (if using fixed-size array conversion) or silently produce an invalid hash.
/// This function provides a safe, fallible boundary to verify length before
/// memory allocation or cryptographic processing.
///
/// # How it works
/// As a `const fn`, this can be evaluated by the compiler. If the input is a
/// static byte array (e.g., `b"..."`), the compiler can resolve the `Result`
/// at compile time, eliminating the runtime branch entirely.
///
/// # Errors
///
/// Returns [`VctrlError::InvalidHashLength`] if the slice length does not match
/// [`HASH_LENGTH`].
///
/// # Examples
///
/// Validating a correctly sized slice:
///
/// ```
/// # use libvctrl_handler::validation::validate_hash_bytes;
/// let valid_hash = [0_u8; 64];
/// assert!(validate_hash_bytes(&valid_hash).is_ok());
/// ```
///
/// Handling an invalid slice:
///
/// ```
/// # use libvctrl_handler::validation::validate_hash_bytes;
/// # use libvctrl_handler::VctrlError;
/// let invalid_hash = [0_u8; 32];
/// let result = validate_hash_bytes(&invalid_hash);
/// assert!(matches!(result, Err(VctrlError::InvalidHashLength(32))));
/// ```
pub const