awsim_core/events.rs
1use serde::{Deserialize, Serialize};
2use tokio::sync::broadcast;
3
4/// An internal event emitted by a service to signal something happened.
5///
6/// Consumers (e.g. the background event router in `awsim`) subscribe to the
7/// bus and perform cross-service fan-out (e.g. SNS → SQS delivery).
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct InternalEvent {
10 /// Originating service: "s3", "sns", "eventbridge", etc.
11 pub source: String,
12 /// Fine-grained event type: "s3:ObjectCreated:Put", "sns:Publish", etc.
13 pub event_type: String,
14 /// AWS region the event occurred in.
15 pub region: String,
16 /// AWS account ID the event occurred in.
17 pub account_id: String,
18 /// Event-specific payload (free-form JSON).
19 pub detail: serde_json::Value,
20}
21
22/// A cheap-to-clone handle to the shared broadcast channel used as an
23/// internal event bus between services.
24#[derive(Clone, Debug)]
25pub struct EventBus {
26 sender: broadcast::Sender<InternalEvent>,
27}
28
29impl EventBus {
30 /// Create a new event bus with a buffer of 1 024 events.
31 pub fn new() -> Self {
32 let (sender, _) = broadcast::channel(1024);
33 Self { sender }
34 }
35
36 /// Publish an event to all current subscribers.
37 ///
38 /// Silently drops the event if there are no active subscribers (the
39 /// broadcast channel returns `SendError` in that case, which we ignore).
40 pub fn publish(&self, event: InternalEvent) {
41 let _ = self.sender.send(event);
42 }
43
44 /// Subscribe to the event stream. Each call returns an independent
45 /// receiver that starts from the next event published after the call.
46 pub fn subscribe(&self) -> broadcast::Receiver<InternalEvent> {
47 self.sender.subscribe()
48 }
49}
50
51impl Default for EventBus {
52 fn default() -> Self {
53 Self::new()
54 }
55}