Mountain/IPC/Common/
MessageType.rs1use std::collections::HashMap;
7
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct IPCMessage {
13 pub id:String,
15 pub command:String,
17 pub payload:serde_json::Value,
19 pub timestamp:u64,
21 pub correlation_id:Option<String>,
23 pub priority:MessagePriority,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
29pub enum MessagePriority {
30 Low = 0,
32 Normal = 1,
34 High = 2,
36 Critical = 3,
38}
39
40impl Default for MessagePriority {
41 fn default() -> Self { Self::Normal }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct IPCCommand {
47 pub command:String,
49 pub args:Vec<String>,
51 pub params:HashMap<String, serde_json::Value>,
53 pub priority:MessagePriority,
55}
56
57impl IPCCommand {
58 pub fn new(command:impl Into<String>) -> Self {
60 Self {
61 command:command.into(),
62 args:Vec::new(),
63 params:HashMap::new(),
64 priority:MessagePriority::Normal,
65 }
66 }
67
68 pub fn with_arg(mut self, arg:impl Into<String>) -> Self {
70 self.args.push(arg.into());
71 self
72 }
73
74 pub fn with_param(mut self, key:impl Into<String>, value:serde_json::Value) -> Self {
76 self.params.insert(key.into(), value);
77 self
78 }
79
80 pub fn with_priority(mut self, priority:MessagePriority) -> Self {
82 self.priority = priority;
83 self
84 }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct IPCResponse {
90 pub correlation_id:String,
92 pub data:serde_json::Value,
94 pub success:bool,
96 pub error:Option<String>,
98 pub timestamp:u64,
100}
101
102impl IPCResponse {
103 pub fn success(correlation_id:impl Into<String>, data:serde_json::Value) -> Self {
105 Self {
106 correlation_id:correlation_id.into(),
107 data,
108 success:true,
109 error:None,
110 timestamp:chrono::Utc::now().timestamp_millis() as u64,
111 }
112 }
113
114 pub fn error(correlation_id:impl Into<String>, error:impl Into<String>) -> Self {
116 Self {
117 correlation_id:correlation_id.into(),
118 data:serde_json::Value::Null,
119 success:false,
120 error:Some(error.into()),
121 timestamp:chrono::Utc::now().timestamp_millis() as u64,
122 }
123 }
124}
125
126impl IPCMessage {
127 pub fn new(command:impl Into<String>) -> Self {
129 Self {
130 id:uuid::Uuid::new_v4().to_string(),
131 command:command.into(),
132 payload:serde_json::Value::Null,
133 timestamp:chrono::Utc::now().timestamp_millis() as u64,
134 correlation_id:None,
135 priority:MessagePriority::Normal,
136 }
137 }
138
139 pub fn with_payload(mut self, payload:serde_json::Value) -> Self {
141 self.payload = payload;
142 self
143 }
144
145 pub fn with_correlation_id(mut self, correlation_id:impl Into<String>) -> Self {
147 self.correlation_id = Some(correlation_id.into());
148 self
149 }
150
151 pub fn with_priority(mut self, priority:MessagePriority) -> Self {
153 self.priority = priority;
154 self
155 }
156}