use super::MessageId;
use mti::prelude::*;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CausationId(MagicTypeId);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidCausationId {
Parse(MagicTypeIdError),
WrongPrefix {
expected: &'static str,
actual: String,
},
}
impl fmt::Display for InvalidCausationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse(e) => write!(f, "invalid causation ID: {e}"),
Self::WrongPrefix { expected, actual } => {
write!(f, "expected prefix '{expected}', got '{actual}'")
}
}
}
}
impl std::error::Error for InvalidCausationId {}
impl CausationId {
pub const PREFIX: &'static str = "cau";
#[must_use]
pub fn new() -> Self {
Self(Self::PREFIX.create_type_id::<V7>())
}
pub fn parse(s: &str) -> Result<Self, InvalidCausationId> {
let id = MagicTypeId::from_str(s).map_err(InvalidCausationId::Parse)?;
let prefix = id.prefix().as_str();
if prefix != Self::PREFIX && prefix != "msg" {
return Err(InvalidCausationId::WrongPrefix {
expected: "cau or msg",
actual: prefix.to_string(),
});
}
Ok(Self(id))
}
#[must_use]
pub fn inner(&self) -> &MagicTypeId {
&self.0
}
#[must_use]
pub fn suffix(&self) -> String {
self.0.suffix().to_string()
}
}
impl Default for CausationId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for CausationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for CausationId {
type Err = InvalidCausationId;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl AsRef<MagicTypeId> for CausationId {
fn as_ref(&self) -> &MagicTypeId {
&self.0
}
}
impl From<MessageId> for CausationId {
fn from(msg_id: MessageId) -> Self {
Self(msg_id.inner().clone())
}
}
impl From<&MessageId> for CausationId {
fn from(msg_id: &MessageId) -> Self {
Self(msg_id.inner().clone())
}
}
impl Serialize for CausationId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.to_string().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for CausationId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::parse(&s).map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_creates_valid_causation_id() {
let id = CausationId::new();
assert!(id.to_string().starts_with("cau_"));
}
#[test]
fn parse_valid_causation_id() {
let id_str = CausationId::new().to_string();
let parsed = CausationId::parse(&id_str);
assert!(parsed.is_ok());
}
#[test]
fn parse_accepts_msg_prefix() {
let result = CausationId::parse("msg_01h455vb4pex5vsknk084sn02q");
assert!(result.is_ok());
}
#[test]
fn parse_wrong_prefix_fails() {
let result = CausationId::parse("cor_01h455vb4pex5vsknk084sn02q");
assert!(matches!(
result,
Err(InvalidCausationId::WrongPrefix { .. })
));
}
#[test]
fn causation_ids_are_unique() {
let id1 = CausationId::new();
let id2 = CausationId::new();
assert_ne!(id1, id2);
}
#[test]
fn from_message_id() {
let msg_id = MessageId::new();
let msg_id_str = msg_id.to_string();
let causation_id: CausationId = msg_id.into();
assert_eq!(causation_id.to_string(), msg_id_str);
}
#[test]
fn from_message_id_ref() {
let msg_id = MessageId::new();
let causation_id: CausationId = (&msg_id).into();
assert_eq!(causation_id.to_string(), msg_id.to_string());
}
#[test]
fn serde_roundtrip() -> Result<(), serde_json::Error> {
let id = CausationId::new();
let json = serde_json::to_string(&id)?;
let restored: CausationId = serde_json::from_str(&json)?;
assert_eq!(id, restored);
Ok(())
}
}