use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct NodeId(u128);
impl NodeId {
pub const fn from_u128(val: u128) -> Self {
Self(val)
}
pub fn as_u128(&self) -> u128 {
self.0
}
pub fn explicit(key: &str) -> Self {
let mut hasher = blake3::Hasher::new();
hasher.update(b"explicit:");
hasher.update(key.as_bytes());
let hash = hasher.finalize();
Self(u128::from_le_bytes(
hash.as_bytes()[0..16].try_into().unwrap(),
))
}
pub fn derived(parent: u128, path: &[u32]) -> Self {
let mut hasher = blake3::Hasher::new();
hasher.update(b"derived:");
hasher.update(&parent.to_le_bytes());
for index in path {
hasher.update(&index.to_le_bytes());
}
let hash = hasher.finalize();
Self(u128::from_le_bytes(
hash.as_bytes()[0..16].try_into().unwrap(),
))
}
}
impl fmt::Debug for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NodeId({:032x})", self.0)
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:032x}", self.0)
}
}