1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use uuid::Uuid;
/// A generic message that can be sent to or received from a message broker.
#[derive(Debug, Clone)]
pub struct Message<T> {
/// The topic or channel the message belongs to.
pub topic: String,
/// Optional partitioning key.
pub key: Option<String>,
/// The message payload.
pub payload: T,
/// Arbitrary key-value headers attached to the message.
pub headers: HashMap<String, String>,
/// Timestamp when the message was created.
pub timestamp: DateTime<Utc>,
/// The partition the message was sent to or read from.
pub partition: Option<i32>,
/// The offset within the partition.
pub offset: Option<i64>,
}
impl<T> Message<T> {
/// Create a new message for the given topic with the provided payload.
pub fn new(topic: impl Into<String>, payload: T) -> Self {
Self {
topic: topic.into(),
key: None,
payload,
headers: HashMap::new(),
timestamp: Utc::now(),
partition: None,
offset: None,
}
}
/// Set the partitioning key.
#[must_use]
pub fn with_key(mut self, key: impl Into<String>) -> Self {
self.key = Some(key.into());
self
}
/// Attach a single header.
#[must_use]
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(key.into(), value.into());
self
}
/// Attach a unique message-id header.
#[must_use]
pub fn with_message_id(self) -> Self {
self.with_header("message-id", Uuid::new_v4().to_string())
}
}