ceylon_runtime/core/
message.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use uuid::Uuid;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Message {
7 pub id: String,
8 pub topic: String,
9 pub payload: Vec<u8>,
10 pub sender: String,
11 pub metadata: HashMap<String, String>,
12 pub created_at: i64,
13}
14
15impl Message {
16 pub fn new(topic: impl Into<String>, payload: Vec<u8>, sender: impl Into<String>) -> Self {
17 Self {
18 id: Uuid::new_v4().to_string(),
19 topic: topic.into(),
20 payload,
21 sender: sender.into(),
22 metadata: HashMap::new(),
23 created_at: chrono::Utc::now().timestamp_micros(),
24 }
25 }
26
27 pub fn correlation_id(&self) -> Option<String> {
28 self.metadata.get("correlation_id").cloned()
29 }
30
31 pub fn set_correlation_id(&mut self, id: &str) {
32 self.metadata
33 .insert("correlation_id".to_string(), id.to_string());
34 }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct GenericMessage {
40 pub content: String,
41 pub metadata: std::collections::HashMap<String, String>,
42}
43
44impl GenericMessage {
45 pub fn new(content: impl Into<String>) -> Self {
46 Self {
47 content: content.into(),
48 metadata: std::collections::HashMap::new(),
49 }
50 }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct GenericResponse {
56 pub content: String,
57}
58
59impl GenericResponse {
60 pub fn new(content: impl Into<String>) -> Self {
61 Self {
62 content: content.into(),
63 }
64 }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct Envelope {
69 pub message: Message,
70 pub target_agent: Option<String>,
71 pub target_node: Option<String>,
72}