#![forbid(unsafe_code)]
use core::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
macro_rules! ulid_newtype {
(
$(#[$doc:meta])*
$name:ident
) => {
$(#[$doc])*
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Serialize,
Deserialize,
)]
#[serde(transparent)]
pub struct $name(pub Ulid);
impl $name {
#[must_use]
#[allow(
clippy::new_without_default,
reason = "Default::default() returns Ulid::nil() — a zero ULID — \
which is observably different from a freshly generated ULID; \
we deliberately do not derive Default to avoid that surprise."
)]
pub fn new() -> Self {
Self(Ulid::new())
}
#[must_use]
pub const fn as_ulid(&self) -> &Ulid {
&self.0
}
#[must_use]
pub const fn into_ulid(self) -> Ulid {
self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl FromStr for $name {
type Err = ulid::DecodeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ulid::from_string(s).map(Self)
}
}
};
}
ulid_newtype! {
PeerId
}
ulid_newtype! {
NodeId
}
ulid_newtype! {
CohortId
}
ulid_newtype! {
ChainId
}