Skip to main content

paykit_lib/
event.rs

1use crate::{validation::validate_uuid_v4, Result};
2
3/// UUID-v4 identifier for one Event Message.
4#[derive(Clone, Debug, PartialEq, Eq, Hash)]
5pub struct EventId(String);
6
7impl EventId {
8    /// Create an Event ID from a UUID-v4 string.
9    pub fn new(id: impl Into<String>) -> Result<Self> {
10        validate_uuid_v4(id.into(), "Event ID").map(Self)
11    }
12
13    /// Generate a fresh Event ID.
14    pub fn new_v4() -> Self {
15        Self(uuid::Uuid::new_v4().to_string())
16    }
17
18    /// Access the canonical UUID string.
19    pub fn as_str(&self) -> &str {
20        &self.0
21    }
22}
23
24impl std::fmt::Display for EventId {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        f.write_str(&self.0)
27    }
28}
29
30impl AsRef<str> for EventId {
31    fn as_ref(&self) -> &str {
32        &self.0
33    }
34}