Skip to main content

looprs_core/ports/
message_broker.rs

1//! MessageBroker port — fan-out pub/sub message routing.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use tokio::sync::broadcast;
6
7// ── Domain type ─────────────────────────────────────────────────────────
8
9/// A message routed through the pub/sub broker.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Message {
12    pub source: String,
13    pub timestamp: DateTime<Utc>,
14    pub topic: String,
15    pub schema_version: u32,
16    pub payload: serde_json::Value,
17}
18
19impl Message {
20    pub fn new(
21        source: impl Into<String>,
22        topic: impl Into<String>,
23        schema_version: u32,
24        payload: serde_json::Value,
25    ) -> Self {
26        Self {
27            source: source.into(),
28            timestamp: Utc::now(),
29            topic: topic.into(),
30            schema_version,
31            payload,
32        }
33    }
34}
35
36// ── Port ─────────────────────────────────────────────────────────────────
37
38/// Port: fan-out message broker for inter-component pub/sub.
39///
40/// Implementations must be cheaply cloneable (`Arc`-backed) so callers
41/// can hold a handle without worrying about lifetimes.
42pub trait MessageBroker: Send + Sync {
43    fn publish(&self, msg: Message) -> usize;
44    fn subscribe(&self, topic: &str) -> broadcast::Receiver<Message>;
45    fn close(&self);
46}