Skip to main content

relaygate_protocol/
identity.rs

1use uuid::Uuid;
2
3macro_rules! opaque_uuid {
4    ($(#[$meta:meta])* $name:ident) => {
5        $(#[$meta])*
6        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7        pub struct $name(Uuid);
8
9        impl $name {
10            #[must_use]
11            pub fn new() -> Self {
12                Self(Uuid::new_v4())
13            }
14
15            #[must_use]
16            pub const fn from_uuid(value: Uuid) -> Self {
17                Self(value)
18            }
19
20            #[must_use]
21            pub const fn as_uuid(self) -> Uuid {
22                self.0
23            }
24        }
25
26        impl Default for $name {
27            fn default() -> Self {
28                Self::new()
29            }
30        }
31
32        impl std::fmt::Display for $name {
33            fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34                self.0.fmt(formatter)
35            }
36        }
37
38        impl std::str::FromStr for $name {
39            type Err = uuid::Error;
40
41            fn from_str(value: &str) -> Result<Self, Self::Err> {
42                Uuid::parse_str(value).map(Self)
43            }
44        }
45    };
46}
47
48opaque_uuid!(
49    /// Identifies one SDK-Gateway transport-session incarnation.
50    ///
51    /// Gateways issue a fresh UUIDv4 for every established session and the
52    /// RelayGate cluster treats the value as globally unique.
53    SessionId
54);
55opaque_uuid!(
56    /// Identifies one live route binding.
57    BindingId
58);
59/// Identifies one Pipe as its origin Relay session plus a session-local counter.
60///
61/// `connection_id` is monotonic only within its origin session. Combining
62/// it with the cluster-unique session incarnation makes the `PipeId` globally
63/// unique without exposing a Gateway identifier to the SDK wire contract.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub struct PipeId {
66    origin_session_id: SessionId,
67    connection_id: u64,
68}
69
70impl PipeId {
71    #[must_use]
72    pub const fn new(origin_session_id: SessionId, connection_id: u64) -> Self {
73        Self {
74            origin_session_id,
75            connection_id,
76        }
77    }
78
79    #[must_use]
80    pub const fn origin_session_id(self) -> SessionId {
81        self.origin_session_id
82    }
83
84    #[must_use]
85    pub const fn connection_id(self) -> u64 {
86        self.connection_id
87    }
88}