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