Mountain/IPC/Common/
MessageType.rs

1//! # Message Type Definitions
2//!
3//! Provides core message structures for IPC communication.
4//! Used for all IPC message passing between Wind and Mountain.
5
6use std::collections::HashMap;
7
8use serde::{Deserialize, Serialize};
9
10/// Standard IPC message format
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct IPCMessage {
13	/// Unique message identifier
14	pub id:String,
15	/// Message type/command
16	pub command:String,
17	/// Message payload
18	pub payload:serde_json::Value,
19	/// Timestamp when message was created
20	pub timestamp:u64,
21	/// Optional correlation ID for request-response patterns
22	pub correlation_id:Option<String>,
23	/// Message priority
24	pub priority:MessagePriority,
25}
26
27/// Message priority levels
28#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
29pub enum MessagePriority {
30	/// Low priority, can be delayed
31	Low = 0,
32	/// Normal priority, standard processing
33	Normal = 1,
34	/// High priority, should be processed quickly
35	High = 2,
36	/// Critical priority, immediate processing required
37	Critical = 3,
38}
39
40impl Default for MessagePriority {
41	fn default() -> Self { Self::Normal }
42}
43
44/// IPC command request
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct IPCCommand {
47	/// Command identifier
48	pub command:String,
49	/// Command arguments
50	pub args:Vec<String>,
51	/// Command parameters as key-value pairs
52	pub params:HashMap<String, serde_json::Value>,
53	/// Message priority
54	pub priority:MessagePriority,
55}
56
57impl IPCCommand {
58	/// Create a new IPC command
59	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	/// Add an argument to the command
69	pub fn with_arg(mut self, arg:impl Into<String>) -> Self {
70		self.args.push(arg.into());
71		self
72	}
73
74	/// Add a parameter to the command
75	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	/// Set the message priority
81	pub fn with_priority(mut self, priority:MessagePriority) -> Self {
82		self.priority = priority;
83		self
84	}
85}
86
87/// IPC response
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct IPCResponse {
90	/// Correlation ID matching the original request
91	pub correlation_id:String,
92	/// Response data
93	pub data:serde_json::Value,
94	/// Whether the response indicates success
95	pub success:bool,
96	/// Error message if the request failed
97	pub error:Option<String>,
98	/// Response timestamp
99	pub timestamp:u64,
100}
101
102impl IPCResponse {
103	/// Create a successful response
104	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	/// Create an error response
115	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	/// Create a new IPC message
128	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	/// Set the message payload
140	pub fn with_payload(mut self, payload:serde_json::Value) -> Self {
141		self.payload = payload;
142		self
143	}
144
145	/// Set the correlation ID
146	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	/// Set the message priority
152	pub fn with_priority(mut self, priority:MessagePriority) -> Self {
153		self.priority = priority;
154		self
155	}
156}