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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use serde::{Deserialize, Serialize};
/// Inbound request envelope.
///
/// Two-stage parse: deserialize this first to get `id` + `command`, then
/// dispatch on `command` and pull specific params from the flattened `params`.
#[derive(Debug, Deserialize)]
pub struct RawRequest {
pub id: String,
pub command: String,
/// Optional LSP hints from the plugin (R031 forward compatibility).
#[serde(default)]
pub lsp_hints: Option<serde_json::Value>,
/// All remaining fields are captured here for per-command deserialization.
#[serde(flatten)]
pub params: serde_json::Value,
}
/// Outbound response envelope.
///
/// `data` is flattened into the top-level JSON object, so a response like
/// `Response { id: "1", success: true, data: json!({"command": "pong"}) }`
/// serializes to `{"id":"1","success":true,"command":"pong"}`.
#[derive(Debug, Serialize)]
pub struct Response {
pub id: String,
pub success: bool,
#[serde(flatten)]
pub data: serde_json::Value,
}
/// Parameters for the `echo` command.
#[derive(Debug, Deserialize)]
pub struct EchoParams {
pub message: String,
}
impl Response {
/// Build a success response with arbitrary data merged at the top level.
pub fn success(id: impl Into<String>, data: serde_json::Value) -> Self {
Response {
id: id.into(),
success: true,
data,
}
}
/// Build an error response with `code` and `message` fields.
pub fn error(id: impl Into<String>, code: &str, message: impl Into<String>) -> Self {
Response {
id: id.into(),
success: false,
data: serde_json::json!({
"code": code,
"message": message.into(),
}),
}
}
/// Build an error response with `code`, `message`, and additional structured data.
///
/// The `extra` fields are merged into the top-level response alongside `code` and `message`.
pub fn error_with_data(
id: impl Into<String>,
code: &str,
message: impl Into<String>,
extra: serde_json::Value,
) -> Self {
let mut data = serde_json::json!({
"code": code,
"message": message.into(),
});
if let (Some(base), Some(ext)) = (data.as_object_mut(), extra.as_object()) {
for (k, v) in ext {
base.insert(k.clone(), v.clone());
}
}
Response {
id: id.into(),
success: false,
data,
}
}
}