Skip to main content

ghidra_cli/ipc/
protocol.rs

1//! IPC protocol types for bridge communication.
2//!
3//! Defines the request/response format for CLI ↔ Java bridge communication.
4//! Uses simple JSON: {"command":"...", "args":{...}} → {"status":"...", "data":{...}}
5
6use serde::{Deserialize, Serialize};
7
8/// Request to the Java bridge.
9#[derive(Debug, Serialize)]
10pub struct BridgeRequest {
11    pub command: String,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub args: Option<serde_json::Value>,
14}
15
16/// Response from the Java bridge.
17#[derive(Debug, Deserialize)]
18pub struct BridgeResponse<T = serde_json::Value> {
19    pub status: String,
20    pub data: Option<T>,
21    #[serde(default)]
22    pub message: Option<String>,
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn test_request_serialization() {
31        let request = BridgeRequest {
32            command: "ping".to_string(),
33            args: None,
34        };
35        let json = serde_json::to_string(&request).unwrap();
36        assert!(json.contains("ping"));
37        assert!(!json.contains("args"));
38    }
39
40    #[test]
41    fn test_request_with_args() {
42        let request = BridgeRequest {
43            command: "list_functions".to_string(),
44            args: Some(serde_json::json!({"limit": 100})),
45        };
46        let json = serde_json::to_string(&request).unwrap();
47        assert!(json.contains("list_functions"));
48        assert!(json.contains("100"));
49    }
50
51    #[test]
52    fn test_response_deserialization() {
53        let json = r#"{"status":"success","data":{"count":42}}"#;
54        let response: BridgeResponse = serde_json::from_str(json).unwrap();
55        assert_eq!(response.status, "success");
56        assert!(response.data.is_some());
57    }
58
59    #[test]
60    fn test_error_response() {
61        let json = r#"{"status":"error","message":"Something went wrong"}"#;
62        let response: BridgeResponse = serde_json::from_str(json).unwrap();
63        assert_eq!(response.status, "error");
64        assert_eq!(response.message.as_ref().unwrap(), "Something went wrong");
65    }
66}