use serde::{Deserialize, Serialize};
use std::fmt;
use ulid::Ulid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CorrelationId(Ulid);
impl CorrelationId {
pub fn new() -> Self {
CorrelationId(Ulid::new())
}
pub fn from_ulid(ulid: Ulid) -> Self {
CorrelationId(ulid)
}
pub fn as_ulid(&self) -> &Ulid {
&self.0
}
pub fn into_ulid(self) -> Ulid {
self.0
}
}
impl Default for CorrelationId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for CorrelationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<Ulid> for CorrelationId {
fn from(ulid: Ulid) -> Self {
CorrelationId(ulid)
}
}
impl From<CorrelationId> for Ulid {
fn from(id: CorrelationId) -> Self {
id.0
}
}
impl AsRef<Ulid> for CorrelationId {
fn as_ref(&self) -> &Ulid {
&self.0
}
}