use crate::error::{Error, Result};
pub type Bytes32 = [u8; 32];
pub fn decode_hex(s: &str) -> Result<Bytes32> {
let s = s.strip_prefix("0x").unwrap_or(s);
let bytes = hex::decode(s)?;
let len = bytes.len();
bytes.try_into().map_err(|_| Error::InvalidNodeLength(len))
}
#[must_use]
pub fn encode_hex(bytes: &Bytes32) -> String {
format!("0x{}", hex::encode(bytes))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_roundtrip() {
let original = [0xab; 32];
let hex = encode_hex(&original);
assert!(hex.starts_with("0x"), "should have 0x prefix");
let recovered = decode_hex(&hex).unwrap();
assert_eq!(original, recovered);
}
#[test]
fn hex_without_prefix() {
let hex = "0000000000000000000000000000000000000000000000000000000000000001";
let bytes = decode_hex(hex).unwrap();
assert_eq!(bytes[31], 1);
}
#[test]
fn hex_with_prefix() {
let hex = "0x0000000000000000000000000000000000000000000000000000000000000001";
let bytes = decode_hex(hex).unwrap();
assert_eq!(bytes[31], 1);
}
#[test]
fn invalid_hex_length() {
let result = decode_hex("0x00");
assert!(matches!(result, Err(Error::InvalidNodeLength(1))));
}
}