use uuid::Uuid;
macro_rules! opaque_uuid {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name(Uuid);
impl $name {
#[must_use]
pub fn new() -> Self {
Self(Uuid::new_v4())
}
#[must_use]
pub const fn from_uuid(value: Uuid) -> Self {
Self(value)
}
#[must_use]
pub const fn as_uuid(self) -> Uuid {
self.0
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for $name {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(formatter)
}
}
impl std::str::FromStr for $name {
type Err = uuid::Error;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Uuid::parse_str(value).map(Self)
}
}
};
}
opaque_uuid!(
SessionId
);
opaque_uuid!(
BindingId
);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PipeId {
origin_session_id: SessionId,
connection_id: u64,
}
impl PipeId {
#[must_use]
pub const fn new(origin_session_id: SessionId, connection_id: u64) -> Self {
Self {
origin_session_id,
connection_id,
}
}
#[must_use]
pub const fn origin_session_id(self) -> SessionId {
self.origin_session_id
}
#[must_use]
pub const fn connection_id(self) -> u64 {
self.connection_id
}
}