Skip to content

Commit 46ac215

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

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,35 @@ 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_owned());
149149

150-
client.send_task(req).map_err(|e| {
150+
let result = match protocol.as_str() {
151+
"postcard" => {
152+
tracing::warn!("Postcard protocol requested but not fully implemented, using JSON");
153+
154+
let mut buf = String::new();
155+
let mut client = JsonTaskClient {
156+
writer: &mut state.stdin,
157+
reader: &mut state.stdout,
158+
buf: &mut buf,
159+
};
160+
client.send_task(req)
161+
}
162+
_ => {
163+
// Default to JSON protocol
164+
let mut buf = String::new();
165+
let mut client = JsonTaskClient {
166+
writer: &mut state.stdin,
167+
reader: &mut state.stdout,
168+
buf: &mut buf,
169+
};
170+
client.send_task(req)
171+
}
172+
};
173+
174+
result.map_err(|e| {
151175
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
152176
match state.process.child.try_wait() {
153177
Ok(None) | Err(_) => e,
@@ -214,6 +238,26 @@ fn mk_child<'a>(
214238
) -> io::Result<Child> {
215239
#[allow(clippy::disallowed_methods)]
216240
let mut cmd = Command::new(path);
241+
242+
// Check for protocol selection environment variable
243+
if let Ok(protocol) = std::env::var("RUST_ANALYZER_PROC_MACRO_PROTOCOL") {
244+
match protocol.as_str() {
245+
"postcard" => {
246+
cmd.args(["--format", "postcard"]);
247+
}
248+
"json" => {
249+
cmd.args(["--format", "json"]);
250+
}
251+
_ => {
252+
tracing::warn!("Unknown protocol '{}', defaulting to json", protocol);
253+
cmd.args(["--format", "json"]);
254+
}
255+
}
256+
} else {
257+
// Default to JSON protocol for backward compatibility
258+
cmd.args(["--format", "json"]);
259+
}
260+
217261
for env in extra_env {
218262
match env {
219263
(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)