Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/aleph-types/src/item_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ impl ItemHash {

#[derive(Error, Debug)]
pub enum ItemHashError {
#[error("invalid hash length, expected 64 hex characters")]
InvalidLength,
#[error("invalid hex digit in hash string")]
InvalidHexDigit,
#[error("{0}: invalid hash length, expected 64 hex characters")]
InvalidLength(String),
#[error("invalid hex digit in hash string: {0}")]
InvalidHexDigit(String),
}

impl TryFrom<&str> for ItemHash {
type Error = ItemHashError;

fn try_from(hex: &str) -> Result<Self, Self::Error> {
if hex.len() != 2 * HASH_LENGTH {
return Err(ItemHashError::InvalidLength);
return Err(ItemHashError::InvalidLength(hex.to_string()));
}
let mut bytes = [0u8; HASH_LENGTH];
for i in 0..HASH_LENGTH {
bytes[i] = u8::from_str_radix(&hex[i * 2..i * 2 + 2], 16)
.map_err(|_| ItemHashError::InvalidHexDigit)?;
.map_err(|_| ItemHashError::InvalidHexDigit(hex.to_string()))?;
}
Ok(Self { bytes })
}
Expand Down