Skip to content

Commit e43ae97

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

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,37 @@ 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+
// Check environment variable to determine which protocol to use
147+
let protocol = std::env::var("RUST_ANALYZER_PROC_MACRO_PROTOCOL")
148+
.unwrap_or_else(|_| "json".to_string());
149149

150-
client.send_task(req).map_err(|e| {
150+
let result = match protocol.as_str() {
151+
"postcard" => {
152+
// For now, still use JSON even when postcard is requested
153+
// TODO: Implement full postcard protocol support
154+
tracing::warn!("Postcard protocol requested but not fully implemented, using JSON");
155+
156+
let mut buf = String::new();
157+
let mut client = JsonTaskClient {
158+
writer: &mut state.stdin,
159+
reader: &mut state.stdout,
160+
buf: &mut buf,
161+
};
162+
client.send_task(req)
163+
}
164+
_ => {
165+
// Default to JSON protocol
166+
let mut buf = String::new();
167+
let mut client = JsonTaskClient {
168+
writer: &mut state.stdin,
169+
reader: &mut state.stdout,
170+
buf: &mut buf,
171+
};
172+
client.send_task(req)
173+
}
174+
};
175+
176+
result.map_err(|e| {
151177
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
152178
match state.process.child.try_wait() {
153179
Ok(None) | Err(_) => e,
@@ -214,6 +240,26 @@ fn mk_child<'a>(
214240
) -> io::Result<Child> {
215241
#[allow(clippy::disallowed_methods)]
216242
let mut cmd = Command::new(path);
243+
244+
// Check for protocol selection environment variable
245+
if let Ok(protocol) = std::env::var("RUST_ANALYZER_PROC_MACRO_PROTOCOL") {
246+
match protocol.as_str() {
247+
"postcard" => {
248+
cmd.args(["--format", "postcard"]);
249+
}
250+
"json" => {
251+
cmd.args(["--format", "json"]);
252+
}
253+
_ => {
254+
tracing::warn!("Unknown protocol '{}', defaulting to json", protocol);
255+
cmd.args(["--format", "json"]);
256+
}
257+
}
258+
} else {
259+
// Default to JSON protocol for backward compatibility
260+
cmd.args(["--format", "json"]);
261+
}
262+
217263
for env in extra_env {
218264
match env {
219265
(key, Some(val)) => cmd.env(key, val),
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
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, json_task_executor::JsonTaskExecutor,
6+
postcard_task_executor::PostcardTaskExecutor,
7+
};
58

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

0 commit comments

Comments
 (0)