use crate::{Error, Result};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
pub(crate) fn decode_hex<const N: usize>(what: &'static str, s: &str) -> Result<[u8; N]> {
let raw = hex::decode(s).map_err(|_| Error::InvalidKey(format!("{what} is not hex")))?;
raw.try_into().map_err(|_| Error::InvalidKey(format!("{what} must be {N} bytes")))
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Hash32(pub [u8; 32]);
impl Hash32 {
pub fn of(data: &[u8]) -> Self {
Self(*blake3::hash(data).as_bytes())
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn from_hex(s: &str) -> Result<Self> {
Ok(Self(decode_hex("hash", s)?))
}
}
impl From<blake3::Hash> for Hash32 {
fn from(h: blake3::Hash) -> Self {
Self(*h.as_bytes())
}
}
impl fmt::Display for Hash32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
impl fmt::Debug for Hash32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Hash32({})", self.to_hex())
}
}
impl Serialize for Hash32 {
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
s.serialize_str(&self.to_hex())
}
}
impl<'de> Deserialize<'de> for Hash32 {
fn deserialize<D: Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
let s = String::deserialize(d)?;
Self::from_hex(&s).map_err(serde::de::Error::custom)
}
}