Skip to main content

rdesktop_core/
ipc.rs

1use serde::{Deserialize, Serialize};
2
3/// A message sent from the frontend (JavaScript) to the backend (Rust).
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct IpcMessage {
6    /// Unique message ID for request-response correlation
7    pub id: String,
8
9    /// The command/method to invoke
10    pub cmd: String,
11
12    /// JSON payload
13    pub payload: serde_json::Value,
14}
15
16/// A response sent from the backend to the frontend.
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct IpcResponse {
19    /// Correlation ID matching the request
20    pub id: String,
21
22    /// Whether the command succeeded
23    pub success: bool,
24
25    /// Response data (on success) or error message (on failure)
26    pub data: serde_json::Value,
27}
28
29/// Handler for IPC messages from the frontend.
30pub trait IpcHandler: Send + Sync {
31    /// Handle an IPC message and return a response.
32    fn handle(&self, message: IpcMessage) -> IpcResponse;
33}
34
35/// A function-based IPC handler.
36pub 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}