Skip to content

Commit 7afcfa7

Browse files
committed
adapt proc-macro-srv-cli and test
1 parent 7af8c85 commit 7afcfa7

2 files changed

Lines changed: 37 additions & 46 deletions

File tree

crates/proc-macro-srv-cli/src/main_loop.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! The main loop of the proc-macro server.
22
use proc_macro_api::{
3-
Codec, ProtocolFormat,
4-
bidirectional_protocol::msg as bidirectional,
5-
legacy_protocol::msg as legacy,
6-
transport::codec::{json::JsonProtocol, postcard::PostcardProtocol},
3+
ProtocolFormat, bidirectional_protocol::msg as bidirectional, legacy_protocol::msg as legacy,
74
version::CURRENT_API_VERSION,
85
};
96
use std::{
@@ -40,14 +37,12 @@ pub fn run(
4037
format: ProtocolFormat,
4138
) -> io::Result<()> {
4239
match format {
43-
ProtocolFormat::JsonLegacy => run_old::<JsonProtocol>(stdin, stdout),
44-
ProtocolFormat::BidirectionalPostcardPrototype => {
45-
run_new::<PostcardProtocol>(stdin, stdout)
46-
}
40+
ProtocolFormat::JsonLegacy => run_old(stdin, stdout),
41+
ProtocolFormat::BidirectionalPostcardPrototype => run_new(stdin, stdout),
4742
}
4843
}
4944

50-
fn run_new<C: Codec>(
45+
fn run_new(
5146
stdin: &mut (dyn BufRead + Send + Sync),
5247
stdout: &mut (dyn Write + Send + Sync),
5348
) -> io::Result<()> {
@@ -61,15 +56,15 @@ fn run_new<C: Codec>(
6156
}
6257
}
6358

64-
let mut buf = C::Buf::default();
59+
let mut buf = Vec::default();
6560

6661
let env_snapshot = EnvSnapshot::default();
6762
let srv = proc_macro_srv::ProcMacroSrv::new(&env_snapshot);
6863

6964
let mut span_mode = legacy::SpanMode::Id;
7065

7166
'outer: loop {
72-
let req_opt = bidirectional::BidirectionalMessage::read::<C>(stdin, &mut buf)?;
67+
let req_opt = bidirectional::BidirectionalMessage::read(stdin, &mut buf)?;
7368
let Some(req) = req_opt else {
7469
break 'outer;
7570
};
@@ -84,22 +79,22 @@ fn run_new<C: Codec>(
8479
.collect()
8580
});
8681

87-
send_response::<C>(stdout, bidirectional::Response::ListMacros(res))?;
82+
send_response(stdout, bidirectional::Response::ListMacros(res))?;
8883
}
8984

9085
bidirectional::Request::ApiVersionCheck {} => {
91-
send_response::<C>(
86+
send_response(
9287
stdout,
9388
bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION),
9489
)?;
9590
}
9691

9792
bidirectional::Request::SetConfig(config) => {
9893
span_mode = config.span_mode;
99-
send_response::<C>(stdout, bidirectional::Response::SetConfig(config))?;
94+
send_response(stdout, bidirectional::Response::SetConfig(config))?;
10095
}
10196
bidirectional::Request::ExpandMacro(task) => {
102-
handle_expand::<C>(&srv, stdin, stdout, &mut buf, span_mode, *task)?;
97+
handle_expand(&srv, stdin, stdout, &mut buf, span_mode, *task)?;
10398
}
10499
},
105100
_ => continue,
@@ -109,21 +104,21 @@ fn run_new<C: Codec>(
109104
Ok(())
110105
}
111106

112-
fn handle_expand<C: Codec>(
107+
fn handle_expand(
113108
srv: &proc_macro_srv::ProcMacroSrv<'_>,
114109
stdin: &mut (dyn BufRead + Send + Sync),
115110
stdout: &mut (dyn Write + Send + Sync),
116-
buf: &mut C::Buf,
111+
buf: &mut Vec<u8>,
117112
span_mode: legacy::SpanMode,
118113
task: bidirectional::ExpandMacro,
119114
) -> io::Result<()> {
120115
match span_mode {
121-
legacy::SpanMode::Id => handle_expand_id::<C>(srv, stdout, task),
122-
legacy::SpanMode::RustAnalyzer => handle_expand_ra::<C>(srv, stdin, stdout, buf, task),
116+
legacy::SpanMode::Id => handle_expand_id(srv, stdout, task),
117+
legacy::SpanMode::RustAnalyzer => handle_expand_ra(srv, stdin, stdout, buf, task),
123118
}
124119
}
125120

126-
fn handle_expand_id<C: Codec>(
121+
fn handle_expand_id(
127122
srv: &proc_macro_srv::ProcMacroSrv<'_>,
128123
stdout: &mut dyn Write,
129124
task: bidirectional::ExpandMacro,
@@ -164,34 +159,34 @@ fn handle_expand_id<C: Codec>(
164159
})
165160
.map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default()));
166161

167-
send_response::<C>(stdout, bidirectional::Response::ExpandMacro(res))
162+
send_response(stdout, bidirectional::Response::ExpandMacro(res))
168163
}
169164

170-
struct ProcMacroClientHandle<'a, C: Codec> {
165+
struct ProcMacroClientHandle<'a> {
171166
stdin: &'a mut (dyn BufRead + Send + Sync),
172167
stdout: &'a mut (dyn Write + Send + Sync),
173-
buf: &'a mut C::Buf,
168+
buf: &'a mut Vec<u8>,
174169
}
175170

176-
impl<'a, C: Codec> ProcMacroClientHandle<'a, C> {
171+
impl<'a> ProcMacroClientHandle<'a> {
177172
fn roundtrip(
178173
&mut self,
179174
req: bidirectional::SubRequest,
180175
) -> Option<bidirectional::BidirectionalMessage> {
181176
let msg = bidirectional::BidirectionalMessage::SubRequest(req);
182177

183-
if msg.write::<C>(&mut *self.stdout).is_err() {
178+
if msg.write(&mut *self.stdout).is_err() {
184179
return None;
185180
}
186181

187-
match bidirectional::BidirectionalMessage::read::<C>(&mut *self.stdin, self.buf) {
182+
match bidirectional::BidirectionalMessage::read(&mut *self.stdin, self.buf) {
188183
Ok(Some(msg)) => Some(msg),
189184
_ => None,
190185
}
191186
}
192187
}
193188

194-
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
189+
impl proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_> {
195190
fn file(&mut self, file_id: proc_macro_srv::span::FileId) -> String {
196191
match self.roundtrip(bidirectional::SubRequest::FilePath { file_id: file_id.index() }) {
197192
Some(bidirectional::BidirectionalMessage::SubResponse(
@@ -260,11 +255,11 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
260255
}
261256
}
262257

263-
fn handle_expand_ra<C: Codec>(
258+
fn handle_expand_ra(
264259
srv: &proc_macro_srv::ProcMacroSrv<'_>,
265260
stdin: &mut (dyn BufRead + Send + Sync),
266261
stdout: &mut (dyn Write + Send + Sync),
267-
buf: &mut C::Buf,
262+
buf: &mut Vec<u8>,
268263
task: bidirectional::ExpandMacro,
269264
) -> io::Result<()> {
270265
let bidirectional::ExpandMacro {
@@ -309,7 +304,7 @@ fn handle_expand_ra<C: Codec>(
309304
def_site,
310305
call_site,
311306
mixed_site,
312-
Some(&mut ProcMacroClientHandle::<C> { stdin, stdout, buf }),
307+
Some(&mut ProcMacroClientHandle { stdin, stdout, buf }),
313308
)
314309
.map(|it| {
315310
(
@@ -325,10 +320,10 @@ fn handle_expand_ra<C: Codec>(
325320
.map(|(tree, span_data_table)| bidirectional::ExpandMacroExtended { tree, span_data_table })
326321
.map_err(|e| legacy::PanicMessage(e.into_string().unwrap_or_default()));
327322

328-
send_response::<C>(stdout, bidirectional::Response::ExpandMacroExtended(res))
323+
send_response(stdout, bidirectional::Response::ExpandMacroExtended(res))
329324
}
330325

331-
fn run_old<C: Codec>(
326+
fn run_old(
332327
stdin: &mut (dyn BufRead + Send + Sync),
333328
stdout: &mut (dyn Write + Send + Sync),
334329
) -> io::Result<()> {
@@ -342,9 +337,9 @@ fn run_old<C: Codec>(
342337
}
343338
}
344339

345-
let mut buf = C::Buf::default();
346-
let mut read_request = || legacy::Request::read::<C>(stdin, &mut buf);
347-
let mut write_response = |msg: legacy::Response| msg.write::<C>(stdout);
340+
let mut buf = String::default();
341+
let mut read_request = || legacy::Request::read(stdin, &mut buf);
342+
let mut write_response = |msg: legacy::Response| msg.write(stdout);
348343

349344
let env = EnvSnapshot::default();
350345
let srv = proc_macro_srv::ProcMacroSrv::new(&env);
@@ -473,10 +468,7 @@ fn run_old<C: Codec>(
473468
Ok(())
474469
}
475470

476-
fn send_response<C: Codec>(
477-
stdout: &mut dyn Write,
478-
resp: bidirectional::Response,
479-
) -> io::Result<()> {
471+
fn send_response(stdout: &mut dyn Write, resp: bidirectional::Response) -> io::Result<()> {
480472
let resp = bidirectional::BidirectionalMessage::Response(resp);
481-
resp.write::<C>(stdout)
473+
resp.write(stdout)
482474
}

crates/proc-macro-srv-cli/tests/common/utils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use proc_macro_api::{
1212
BidirectionalMessage, Request as BiRequest, Response as BiResponse, SubRequest, SubResponse,
1313
},
1414
legacy_protocol::msg::{FlatTree, Message, Request, Response, SpanDataIndexMap},
15-
transport::codec::{json::JsonProtocol, postcard::PostcardProtocol},
1615
};
1716
use span::{Edition, EditionedFileId, FileId, Span, SpanAnchor, SyntaxContext, TextRange};
1817
use tt::{Delimiter, DelimiterKind, TopSubtreeBuilder};
@@ -210,12 +209,12 @@ impl TestProtocol for JsonLegacy {
210209
type Response = Response;
211210

212211
fn request(&self, writer: &mut dyn Write, req: Request) {
213-
req.write::<JsonProtocol>(writer).expect("failed to write request");
212+
req.write(writer).expect("failed to write request");
214213
}
215214

216215
fn receive(&self, reader: &mut dyn BufRead, _writer: &mut dyn Write) -> Response {
217216
let mut buf = String::new();
218-
Response::read::<JsonProtocol>(reader, &mut buf)
217+
Response::read(reader, &mut buf)
219218
.expect("failed to read response")
220219
.expect("no response received")
221220
}
@@ -238,14 +237,14 @@ where
238237

239238
fn request(&self, writer: &mut dyn Write, req: BiRequest) {
240239
let msg = BidirectionalMessage::Request(req);
241-
msg.write::<PostcardProtocol>(writer).expect("failed to write request");
240+
msg.write(writer).expect("failed to write request");
242241
}
243242

244243
fn receive(&self, reader: &mut dyn BufRead, writer: &mut dyn Write) -> BiResponse {
245244
let mut buf = Vec::new();
246245

247246
loop {
248-
let msg = BidirectionalMessage::read::<PostcardProtocol>(reader, &mut buf)
247+
let msg = BidirectionalMessage::read(reader, &mut buf)
249248
.expect("failed to read message")
250249
.expect("no message received");
251250

@@ -254,7 +253,7 @@ where
254253
BidirectionalMessage::SubRequest(sr) => {
255254
let reply = (self.callback)(sr).expect("subrequest callback failed");
256255
let msg = BidirectionalMessage::SubResponse(reply);
257-
msg.write::<PostcardProtocol>(writer).expect("failed to write subresponse");
256+
msg.write(writer).expect("failed to write subresponse");
258257
}
259258
other => panic!("unexpected message: {other:?}"),
260259
}

0 commit comments

Comments
 (0)