use std::fmt;
use serde::{Deserialize, Serialize};
use crate::ChannelError;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ChannelId(String);
impl ChannelId {
#[must_use]
pub fn from_static(value: &'static str) -> Self {
Self(value.to_owned())
}
pub fn new(value: impl Into<String>) -> Result<Self, ChannelError> {
let value = value.into();
if value.is_empty()
|| value.len() > 64
|| !value.bytes().all(|byte| {
byte.is_ascii_lowercase()
|| byte.is_ascii_digit()
|| matches!(byte, b'.' | b'-' | b'_')
})
{
return Err(ChannelError::InvalidValue(value));
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ChannelId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ConversationId(String);
impl ConversationId {
pub fn new(value: impl Into<String>) -> Result<Self, ChannelError> {
let value = value.into();
if value.is_empty() || value.len() > 256 || value.contains('\0') {
return Err(ChannelError::InvalidValue(value));
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ConversationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}