use core::fmt;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IdError {
Empty,
TooLong {
len: usize,
max: usize,
},
ControlCharacter,
}
impl fmt::Display for IdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("value must not be empty"),
Self::TooLong { len, max } => {
write!(f, "value length {len} exceeds the maximum of {max}")
}
Self::ControlCharacter => f.write_str("value must not contain a control character"),
}
}
}
impl std::error::Error for IdError {}
pub(crate) fn contains_control_char(s: &str) -> bool {
s.chars().any(char::is_control)
}
macro_rules! uuid_id {
($(#[$meta:meta])* $name:ident) => {
uuid_id!(@base $(#[$meta])* $name);
uuid_id!(@mint $name);
};
($(#[$meta:meta])* $name:ident, no_mint) => {
uuid_id!(@base $(#[$meta])* $name);
};
(@base $(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct $name(Uuid);
impl $name {
#[must_use]
pub const fn from_uuid(id: Uuid) -> Self {
Self(id)
}
#[must_use]
pub const fn as_uuid(&self) -> Uuid {
self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
};
(@mint $name:ident) => {
impl $name {
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
};
}
uuid_id!(
MessageId
);
uuid_id!(
ConversationId,
no_mint
);
uuid_id!(
RequestId,
no_mint
);
impl ConversationId {
pub const UNSET: Self = Self::from_uuid(Uuid::nil());
#[must_use]
pub const fn is_unset(&self) -> bool {
self.0.is_nil()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CorrelationId(String);
impl CorrelationId {
pub const MAX_LEN: usize = 256;
pub fn parse(s: impl Into<String>) -> Result<Self, IdError> {
let s = s.into();
if s.is_empty() {
return Err(IdError::Empty);
}
if contains_control_char(&s) {
return Err(IdError::ControlCharacter);
}
if s.len() > Self::MAX_LEN {
return Err(IdError::TooLong {
len: s.len(),
max: Self::MAX_LEN,
});
}
Ok(Self(s))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for CorrelationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
mod serde_impls {
use serde::{Deserialize, Serialize, de::Error as _};
use uuid::Uuid;
use super::{ConversationId, CorrelationId, MessageId, RequestId};
macro_rules! uuid_id_serde {
($name:ident) => {
impl Serialize for $name {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(&self.0)
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let raw = String::deserialize(d)?;
Uuid::parse_str(&raw).map(Self).map_err(D::Error::custom)
}
}
};
}
uuid_id_serde!(MessageId);
uuid_id_serde!(ConversationId);
uuid_id_serde!(RequestId);
impl Serialize for CorrelationId {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(&self.0)
}
}
impl<'de> Deserialize<'de> for CorrelationId {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let raw = String::deserialize(d)?;
Self::parse(raw).map_err(D::Error::custom)
}
}
}