use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
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: DateTime<Utc> },
Updated { person: Person, timestamp: DateTime<Utc> },
Deleted { person_id: Uuid, timestamp: DateTime<Utc> },
Merged { source_id: Uuid, target_id: Uuid, timestamp: DateTime<Utc> },
Linked { person_id: Uuid, linked_id: Uuid, timestamp: DateTime<Utc> },
Unlinked { person_id: Uuid, unlinked_id: Uuid, timestamp: DateTime<Utc> },
}
impl PersonEvent {
pub fn timestamp(&self) -> DateTime<Utc> {
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>>;
}