use serde::{Deserialize, Serialize};
use uuid::Uuid;
use jiff::Timestamp;
use crate::models::Person;
use crate::Result;
pub mod producer;
pub mod consumer;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event_type")]
pub enum PersonEvent {
Created {
person: Person,
timestamp: Timestamp,
},
Updated {
person: Person,
timestamp: Timestamp,
},
Deleted {
person_id: Uuid,
timestamp: Timestamp,
},
Merged {
source_id: Uuid,
target_id: Uuid,
timestamp: Timestamp,
},
Linked {
person_id: Uuid,
linked_id: Uuid,
timestamp: Timestamp,
},
Unlinked {
person_id: Uuid,
unlinked_id: Uuid,
timestamp: Timestamp,
},
}
impl PersonEvent {
pub fn timestamp(&self) -> Timestamp {
match self {
PersonEvent::Created { timestamp, .. } => *timestamp,
PersonEvent::Updated { timestamp, .. } => *timestamp,
PersonEvent::Deleted { timestamp, .. } => *timestamp,
PersonEvent::Merged { timestamp, .. } => *timestamp,
PersonEvent::Linked { timestamp, .. } => *timestamp,
PersonEvent::Unlinked { timestamp, .. } => *timestamp,
}
}
pub fn person_id(&self) -> Uuid {
match self {
PersonEvent::Created { person, .. } => person.id,
PersonEvent::Updated { person, .. } => person.id,
PersonEvent::Deleted { person_id, .. } => *person_id,
PersonEvent::Merged { source_id, .. } => *source_id,
PersonEvent::Linked { person_id, .. } => *person_id,
PersonEvent::Unlinked { person_id, .. } => *person_id,
}
}
}
pub trait EventProducer: Send + Sync {
fn publish(&self, event: PersonEvent) -> Result<()>;
}
pub use producer::InMemoryEventPublisher;
pub trait EventConsumer {
fn subscribe(&mut self) -> Result<()>;
fn next_event(&mut self) -> Result<Option<PersonEvent>>;
}