use crate::error::{MemoryError, Result};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as _};
use std::{fmt, str::FromStr, sync::Arc};
macro_rules! text_id {
($name:ident, $kind:literal) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name(Arc<str>);
impl $name {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value = value.into();
if value.is_empty() || value.trim() != value {
return Err(MemoryError::InvalidId { kind: $kind, value });
}
Ok(Self(value.into()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_inner(self) -> String {
self.0.to_string()
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
impl FromStr for $name {
type Err = MemoryError;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(D::Error::custom)
}
}
};
}
text_id!(EventId, "event");
text_id!(StreamId, "stream");
text_id!(EntityId, "entity");
text_id!(FactId, "fact");
text_id!(AgentId, "agent");
text_id!(SessionId, "session");