30 lines
897 B
Rust
30 lines
897 B
Rust
use crate::common::errors::DomainError;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct Checksum(String);
|
|
|
|
impl Checksum {
|
|
pub fn new(hex: impl Into<String>) -> Result<Self, DomainError> {
|
|
let hex = hex.into().to_lowercase();
|
|
if hex.len() != 64 {
|
|
return Err(DomainError::Validation(
|
|
format!("Checksum must be 64 hex characters, got {}", hex.len()),
|
|
));
|
|
}
|
|
if !hex.chars().all(|c| c.is_ascii_hexdigit()) {
|
|
return Err(DomainError::Validation(
|
|
"Checksum contains non-hex characters".to_string(),
|
|
));
|
|
}
|
|
Ok(Self(hex))
|
|
}
|
|
|
|
pub fn as_str(&self) -> &str { &self.0 }
|
|
}
|
|
|
|
impl std::fmt::Display for Checksum {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|