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
59
use serde::{Deserialize, Serialize};
/// A message sent from the frontend (JavaScript) to the backend (Rust).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IpcMessage {
/// Unique message ID for request-response correlation
pub id: String,
/// The command/method to invoke
pub cmd: String,
/// JSON payload
pub payload: serde_json::Value,
}
/// A response sent from the backend to the frontend.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IpcResponse {
/// Correlation ID matching the request
pub id: String,
/// Whether the command succeeded
pub success: bool,
/// Response data (on success) or error message (on failure)
pub data: serde_json::Value,
}
/// Handler for IPC messages from the frontend.
pub trait IpcHandler: Send + Sync {
/// Handle an IPC message and return a response.
fn handle(&self, message: IpcMessage) -> IpcResponse;
}
/// A function-based IPC handler.
pub struct FnIpcHandler<F>
where
F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
{
handler: F,
}
impl<F> FnIpcHandler<F>
where
F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
{
pub fn new(handler: F) -> Self {
Self { handler }
}
}
impl<F> IpcHandler for FnIpcHandler<F>
where
F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
{
fn handle(&self, message: IpcMessage) -> IpcResponse {
(self.handler)(message)
}
}