use serde::{Deserialize, Serialize};
pub const ID_LEN: usize = 32;
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NodeId([u8; ID_LEN]);
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ContentHash([u8; ID_LEN]);
macro_rules! id_newtype_impls {
($t:ty) => {
impl $t {
#[must_use]
pub const fn from_bytes(bytes: [u8; ID_LEN]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; ID_LEN] {
&self.0
}
#[must_use]
pub const fn to_bytes(self) -> [u8; ID_LEN] {
self.0
}
}
impl From<[u8; ID_LEN]> for $t {
fn from(bytes: [u8; ID_LEN]) -> Self {
Self(bytes)
}
}
impl From<$t> for [u8; ID_LEN] {
fn from(value: $t) -> Self {
value.0
}
}
impl std::fmt::Display for $t {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.iter().try_for_each(|b| write!(f, "{b:02x}"))
}
}
};
}
id_newtype_impls!(NodeId);
id_newtype_impls!(ContentHash);
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests;