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