use core::fmt;
use sedimentree_core::id::SedimentreeId;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Topic([u8; 32]);
impl Topic {
#[must_use]
pub const fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl fmt::Debug for Topic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Topic({:02x}{:02x}…)", self.0[0], self.0[1])
}
}
impl fmt::Display for Topic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in &self.0[..4] {
write!(f, "{byte:02x}")?;
}
write!(f, "…")
}
}
impl From<SedimentreeId> for Topic {
fn from(id: SedimentreeId) -> Self {
Self(*id.as_bytes())
}
}
impl From<Topic> for SedimentreeId {
fn from(topic: Topic) -> Self {
SedimentreeId::new(topic.0)
}
}