use serde::{Deserialize, Serialize};
use std::fmt;
use ulid::Ulid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct EventId(Ulid);
impl EventId {
pub fn new() -> Self {
EventId(Ulid::new())
}
pub fn from_string(id: &str) -> Result<Self, ulid::DecodeError> {
Ok(EventId(Ulid::from_string(id)?))
}
pub fn as_str(&self) -> String {
self.0.to_string()
}
pub fn timestamp_ms(&self) -> u64 {
self.0.timestamp_ms()
}
pub fn as_ulid(&self) -> Ulid {
self.0
}
}
impl Default for EventId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for EventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Ulid> for EventId {
fn from(ulid: Ulid) -> Self {
EventId(ulid)
}
}
impl TryFrom<String> for EventId {
type Error = ulid::DecodeError;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::from_string(&s)
}
}
impl TryFrom<&str> for EventId {
type Error = ulid::DecodeError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::from_string(s)
}
}