use core::fmt;
use crate::layout::DIGEST_SIZE;
#[derive(Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Digest(pub [u8; DIGEST_SIZE]);
impl Digest {
#[must_use]
pub fn of(bytes: &[u8]) -> Self {
Self(*blake3::hash(bytes).as_bytes())
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; DIGEST_SIZE] {
&self.0
}
#[must_use]
pub fn short(&self) -> String {
let full = self.to_string();
full[..16].to_owned()
}
}
impl fmt::Display for Digest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
impl fmt::Debug for Digest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Digest({})", self.short())
}
}