1use uuid::Uuid;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct MessageId(Uuid);
7
8impl MessageId {
9 #[must_use]
11 pub fn new() -> Self {
12 Self(Uuid::new_v4())
13 }
14
15 #[must_use]
17 pub fn as_uuid(&self) -> &Uuid {
18 &self.0
19 }
20}
21
22impl Default for MessageId {
23 fn default() -> Self {
24 Self::new()
25 }
26}
27
28impl std::fmt::Display for MessageId {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 self.0.fmt(f)
31 }
32}
33
34impl From<Uuid> for MessageId {
35 fn from(uuid: Uuid) -> Self {
36 Self(uuid)
37 }
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
43pub struct CorrelationId(Uuid);
44
45impl CorrelationId {
46 #[must_use]
48 pub fn new() -> Self {
49 Self(Uuid::new_v4())
50 }
51
52 #[must_use]
54 pub fn as_uuid(&self) -> &Uuid {
55 &self.0
56 }
57}
58
59impl Default for CorrelationId {
60 fn default() -> Self {
61 Self::new()
62 }
63}
64
65impl std::fmt::Display for CorrelationId {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 self.0.fmt(f)
68 }
69}
70
71impl From<Uuid> for CorrelationId {
72 fn from(uuid: Uuid) -> Self {
73 Self(uuid)
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn message_id_new_is_unique() {
83 assert_ne!(MessageId::new(), MessageId::new());
84 }
85
86 #[test]
87 fn correlation_id_new_is_unique() {
88 assert_ne!(CorrelationId::new(), CorrelationId::new());
89 }
90
91 #[test]
92 fn message_id_display_is_uuid_format() {
93 let id = MessageId::new();
94 let s = id.to_string();
95 assert_eq!(s.len(), 36);
96 assert!(s.contains('-'));
97 }
98
99 #[test]
100 fn message_id_and_correlation_id_are_distinct_types() {
101 let msg = MessageId::new();
102 let corr = CorrelationId::new();
103 assert_ne!(msg.to_string(), corr.to_string());
104 }
105
106 #[test]
107 fn from_uuid_roundtrip() {
108 let uuid = Uuid::new_v4();
109 let msg = MessageId::from(uuid);
110 assert_eq!(msg.as_uuid(), &uuid);
111 }
112}