rskit_messaging/
message.rs1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4use uuid::Uuid;
5
6#[derive(Debug, Clone)]
8pub struct Message<T> {
9 pub topic: String,
11 pub key: Option<String>,
13 pub payload: T,
15 pub headers: HashMap<String, String>,
17 pub timestamp: DateTime<Utc>,
19 pub partition: Option<i32>,
21 pub offset: Option<i64>,
23}
24
25impl<T> Message<T> {
26 pub fn new(topic: impl Into<String>, payload: T) -> Self {
28 Self {
29 topic: topic.into(),
30 key: None,
31 payload,
32 headers: HashMap::new(),
33 timestamp: Utc::now(),
34 partition: None,
35 offset: None,
36 }
37 }
38
39 #[must_use]
41 pub fn with_key(mut self, key: impl Into<String>) -> Self {
42 self.key = Some(key.into());
43 self
44 }
45
46 #[must_use]
48 pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
49 self.headers.insert(key.into(), value.into());
50 self
51 }
52
53 #[must_use]
55 pub fn with_message_id(self) -> Self {
56 self.with_header("message-id", Uuid::new_v4().to_string())
57 }
58}