1use std::fmt;
7use std::str::FromStr;
8
9use serde::{Deserialize, Serialize};
10use uuid::Uuid;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
21#[serde(transparent)]
22pub struct ConversationId(u64);
23
24impl ConversationId {
25 #[must_use]
33 pub fn mint() -> Self {
34 let bytes = *Uuid::new_v4().as_bytes();
35 let mut raw = [0u8; 8];
36 raw.copy_from_slice(&bytes[..8]);
37 Self(u64::from_le_bytes(raw))
38 }
39
40 pub(crate) const fn to_wire(self) -> u64 {
41 self.0
42 }
43}
44
45impl fmt::Display for ConversationId {
46 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(formatter, "{}", self.0)
48 }
49}
50
51impl FromStr for ConversationId {
52 type Err = std::num::ParseIntError;
53
54 fn from_str(value: &str) -> Result<Self, Self::Err> {
55 Ok(Self(value.parse()?))
56 }
57}
58
59#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(transparent)]
66pub struct CorrelationId(Uuid);
67
68impl CorrelationId {
69 #[must_use]
71 pub fn mint() -> Self {
72 Self(Uuid::new_v4())
73 }
74}
75
76impl fmt::Display for CorrelationId {
77 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(formatter, "{}", self.0)
79 }
80}
81
82impl FromStr for CorrelationId {
83 type Err = uuid::Error;
84
85 fn from_str(value: &str) -> Result<Self, Self::Err> {
86 Ok(Self(Uuid::parse_str(value)?))
87 }
88}
89
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
101#[serde(transparent)]
102pub struct PublicationId(Uuid);
103
104impl PublicationId {
105 #[must_use]
107 pub const fn from_bytes(bytes: [u8; 16]) -> Self {
108 Self(Uuid::from_bytes(bytes))
109 }
110
111 #[must_use]
113 pub const fn to_bytes(self) -> [u8; 16] {
114 *self.0.as_bytes()
115 }
116
117 #[must_use]
119 pub fn mint() -> Self {
120 Self(Uuid::new_v4())
121 }
122
123 pub(crate) const fn to_correlation(self) -> CorrelationId {
125 CorrelationId(self.0)
126 }
127
128 pub(crate) const fn from_correlation(correlation: CorrelationId) -> Self {
130 Self(correlation.0)
131 }
132}
133
134impl fmt::Display for PublicationId {
135 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
136 write!(formatter, "{}", self.0)
137 }
138}
139
140#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
143#[serde(transparent)]
144pub struct ParticipantRef(u64);
145
146impl ParticipantRef {
147 pub(crate) const fn from_wire(raw: u64) -> Self {
148 Self(raw)
149 }
150
151 pub(crate) const fn to_wire(self) -> u64 {
152 self.0
153 }
154}
155
156impl fmt::Display for ParticipantRef {
157 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(formatter, "{}", self.0)
159 }
160}
161
162#[derive(
170 Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
171)]
172#[serde(transparent)]
173pub struct ConversationSeq(u64);
174
175impl ConversationSeq {
176 #[must_use]
179 pub const fn new(value: u64) -> Self {
180 Self(value)
181 }
182
183 #[must_use]
185 pub const fn value(self) -> u64 {
186 self.0
187 }
188}
189
190impl fmt::Display for ConversationSeq {
191 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
192 write!(formatter, "{}", self.0)
193 }
194}
195
196#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
200pub enum PatternId {
201 RequestResponse,
203 Subscription,
205 PubSub,
207}
208
209impl PatternId {
210 #[must_use]
212 pub const fn as_str(self) -> &'static str {
213 match self {
214 Self::RequestResponse => "frame:conv-request@v1",
215 Self::Subscription => "frame:conv-subscription@v1",
216 Self::PubSub => "frame:conv-pubsub@v1",
217 }
218 }
219
220 #[must_use]
222 pub fn parse_exact(value: &str) -> Option<Self> {
223 match value {
224 "frame:conv-request@v1" => Some(Self::RequestResponse),
225 "frame:conv-subscription@v1" => Some(Self::Subscription),
226 "frame:conv-pubsub@v1" => Some(Self::PubSub),
227 _ => None,
228 }
229 }
230
231 #[must_use]
233 pub const fn allows(self, kind: MessageKind) -> bool {
234 match self {
235 Self::RequestResponse => matches!(kind, MessageKind::Request | MessageKind::Reply),
236 Self::Subscription | Self::PubSub => matches!(kind, MessageKind::Event),
237 }
238 }
239}
240
241impl fmt::Display for PatternId {
242 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
243 formatter.write_str(self.as_str())
244 }
245}
246
247#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
249pub enum MessageKind {
250 Request,
252 Reply,
254 Event,
256}
257
258impl MessageKind {
259 #[must_use]
261 pub const fn as_str(self) -> &'static str {
262 match self {
263 Self::Request => "request",
264 Self::Reply => "reply",
265 Self::Event => "event",
266 }
267 }
268
269 #[must_use]
271 pub fn parse_exact(value: &str) -> Option<Self> {
272 match value {
273 "request" => Some(Self::Request),
274 "reply" => Some(Self::Reply),
275 "event" => Some(Self::Event),
276 _ => None,
277 }
278 }
279}
280
281impl fmt::Display for MessageKind {
282 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
283 formatter.write_str(self.as_str())
284 }
285}