Skip to content

Commit 9038dfe

Browse files
feat: able to choose between two protocols
1 parent 1de1219 commit 9038dfe

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

crates/proc-macro-api/src/process.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,38 @@ impl ProcMacroServerProcess {
143143
}
144144

145145
let state = &mut *self.state.lock().unwrap();
146-
let mut buf = String::new();
147-
let mut client =
148-
JsonTaskClient { writer: &mut state.stdin, reader: &mut state.stdout, buf: &mut buf };
146+
147+
// Check environment variable to determine which protocol to use
148+
let protocol = std::env::var("RUST_ANALYZER_PROC_MACRO_PROTOCOL")
149+
.unwrap_or_else(|_| "json".to_string());
150+
151+
let result = match protocol.as_str() {
152+
"postcard" => {
153+
// For now, still use JSON even when postcard is requested
154+
// TODO: Implement full postcard protocol support
155+
tracing::warn!("Postcard protocol requested but not fully implemented, using JSON");
156+
157+
let mut buf = String::new();
158+
let mut client = JsonTaskClient {
159+
writer: &mut state.stdin,
160+
reader: &mut state.stdout,
161+
buf: &mut buf
162+
};
163+
client.send_task(req)
164+
}
165+
_ => {
166+
// Default to JSON protocol
167+
let mut buf = String::new();
168+
let mut client = JsonTaskClient {
169+
writer: &mut state.stdin,
170+
reader: &mut state.stdout,
171+
buf: &mut buf
172+
};
173+
client.send_task(req)
174+
}
175+
};
149176

150-
client.send_task(req).map_err(|e| {
177+
result.map_err(|e| {
151178
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
152179
match state.process.child.try_wait() {
153180
Ok(None) | Err(_) => e,
@@ -214,6 +241,26 @@ fn mk_child<'a>(
214241
) -> io::Result<Child> {
215242
#[allow(clippy::disallowed_methods)]
216243
let mut cmd = Command::new(path);
244+
245+
// Check for protocol selection environment variable
246+
if let Ok(protocol) = std::env::var("RUST_ANALYZER_PROC_MACRO_PROTOCOL") {
247+
match protocol.as_str() {
248+
"postcard" => {
249+
cmd.args(["--format", "postcard"]);
250+
}
251+
"json" => {
252+
cmd.args(["--format", "json"]);
253+
}
254+
_ => {
255+
tracing::warn!("Unknown protocol '{}', defaulting to json", protocol);
256+
cmd.args(["--format", "json"]);
257+
}
258+
}
259+
} else {
260+
// Default to JSON protocol for backward compatibility
261+
cmd.args(["--format", "json"]);
262+
}
263+
217264
for env in extra_env {
218265
match env {
219266
(key, Some(val)) => cmd.env(key, val),
Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
//! The main loop of the proc-macro server.
22
use std::io;
33

4-
use crate::task_executor::{TaskExecutor, json_task_executor::JsonTaskExecutor};
4+
use crate::task_executor::{
5+
TaskExecutor,
6+
json_task_executor::JsonTaskExecutor,
7+
postcard_task_executor::PostcardTaskExecutor,
8+
};
59

610
pub(crate) fn run() -> io::Result<()> {
7-
// Use JsonTaskExecutor for legacy protocol support
8-
let executor = JsonTaskExecutor;
9-
executor.run()
11+
// Check environment variable or command line args to determine protocol
12+
let args: Vec<String> = std::env::args().collect();
13+
let mut format = None;
14+
15+
// Parse --format argument
16+
for i in 1..args.len() {
17+
if args[i] == "--format" && i + 1 < args.len() {
18+
format = Some(args[i + 1].as_str());
19+
break;
20+
}
21+
}
22+
23+
// Default to JSON for backward compatibility
24+
let protocol = format.unwrap_or("json");
25+
26+
match protocol {
27+
"json" => {
28+
let executor = JsonTaskExecutor;
29+
executor.run()
30+
}
31+
"postcard" => {
32+
let executor = PostcardTaskExecutor;
33+
executor.run()
34+
}
35+
_ => {
36+
Err(io::Error::new(
37+
io::ErrorKind::InvalidInput,
38+
format!("Unsupported protocol format: {}. Supported formats: json, postcard", protocol)
39+
))
40+
}
41+
}
1042
}

0 commit comments

Comments
 (0)