ralph_proto/
event.rs

1//! Event types for pub/sub messaging.
2
3use crate::{HatId, Topic};
4use serde::{Deserialize, Serialize};
5
6/// An event in the pub/sub system.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Event {
9    /// The routing topic for this event.
10    pub topic: Topic,
11
12    /// The content/payload of the event.
13    pub payload: String,
14
15    /// The hat that published this event (if any).
16    pub source: Option<HatId>,
17
18    /// Optional target hat for direct handoff.
19    pub target: Option<HatId>,
20}
21
22impl Event {
23    /// Creates a new event with the given topic and payload.
24    pub fn new(topic: impl Into<Topic>, payload: impl Into<String>) -> Self {
25        Self {
26            topic: topic.into(),
27            payload: payload.into(),
28            source: None,
29            target: None,
30        }
31    }
32
33    /// Sets the source hat for this event.
34    #[must_use]
35    pub fn with_source(mut self, source: impl Into<HatId>) -> Self {
36        self.source = Some(source.into());
37        self
38    }
39
40    /// Sets the target hat for direct handoff.
41    #[must_use]
42    pub fn with_target(mut self, target: impl Into<HatId>) -> Self {
43        self.target = Some(target.into());
44        self
45    }
46}