1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct IpcMessage {
6 pub id: String,
8
9 pub cmd: String,
11
12 pub payload: serde_json::Value,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct IpcResponse {
19 pub id: String,
21
22 pub success: bool,
24
25 pub data: serde_json::Value,
27}
28
29pub trait IpcHandler: Send + Sync {
31 fn handle(&self, message: IpcMessage) -> IpcResponse;
33}
34
35pub struct FnIpcHandler<F>
37where
38 F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
39{
40 handler: F,
41}
42
43impl<F> FnIpcHandler<F>
44where
45 F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
46{
47 pub fn new(handler: F) -> Self {
48 Self { handler }
49 }
50}
51
52impl<F> IpcHandler for FnIpcHandler<F>
53where
54 F: Fn(IpcMessage) -> IpcResponse + Send + Sync,
55{
56 fn handle(&self, message: IpcMessage) -> IpcResponse {
57 (self.handler)(message)
58 }
59}