Skip to content

Commit fada836

Browse files
feat: new protocol for proc-macro-api
1 parent 5b2c8bc commit fada836

File tree

14 files changed

+701
-186
lines changed

14 files changed

+701
-186
lines changed

Cargo.lock

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/proc-macro-api/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version.workspace = true
1414
[dependencies]
1515
serde.workspace = true
1616
serde_derive.workspace = true
17+
postcard = { version = "1.1.1", features = ["alloc"] }
1718
serde_json = { workspace = true, features = ["unbounded_depth"] }
1819
tracing.workspace = true
1920
rustc-hash.workspace = true
@@ -24,7 +25,7 @@ paths = { workspace = true, features = ["serde1"] }
2425
tt.workspace = true
2526
stdx.workspace = true
2627
# span = {workspace = true, default-features = false} does not work
27-
span = { path = "../span", version = "0.0.0", default-features = false}
28+
span = { path = "../span", version = "0.0.0", default-features = false }
2829

2930
intern.workspace = true
3031

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::{
2+
io::{self, BufRead, Write},
3+
sync::Arc,
4+
};
5+
6+
use crate::{
7+
ServerError,
8+
legacy_protocol::{
9+
json::{read_json, write_json},
10+
msg::{Request, Response},
11+
},
12+
task::TaskClient,
13+
};
14+
15+
/// Sends a request to the server and reads the response.
16+
fn send_request(
17+
mut writer: &mut dyn Write,
18+
mut reader: &mut dyn BufRead,
19+
req: Request,
20+
buf: &mut String,
21+
) -> Result<Option<Response>, ServerError> {
22+
use crate::legacy_protocol::msg::Message;
23+
24+
req.write(write_json, &mut writer).map_err(|err| ServerError {
25+
message: "failed to write request".into(),
26+
io: Some(Arc::new(err)),
27+
})?;
28+
let res = Response::read(read_json, &mut reader, buf).map_err(|err| ServerError {
29+
message: "failed to read response".into(),
30+
io: Some(Arc::new(err)),
31+
})?;
32+
Ok(res)
33+
}
34+
35+
pub struct JsonTaskClient<'a> {
36+
pub writer: &'a mut dyn Write,
37+
pub reader: &'a mut dyn BufRead,
38+
pub buf: &'a mut String,
39+
}
40+
41+
impl TaskClient for JsonTaskClient<'_> {
42+
type Task = Request;
43+
type TaskResult = Response;
44+
45+
// Implement send_task for Json Legacy Protocol
46+
// Basically what we have done in process.rs
47+
fn send_task(&mut self, task: Self::Task) -> Result<Self::TaskResult, ServerError> {
48+
send_request(self.writer, self.reader, task, self.buf).and_then(|res| {
49+
res.ok_or_else(|| {
50+
let message = "proc-macro server did not respond with data".to_owned();
51+
ServerError {
52+
io: Some(Arc::new(io::Error::new(io::ErrorKind::BrokenPipe, message.clone()))),
53+
message,
54+
}
55+
})
56+
})
57+
}
58+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
pub mod legacy_protocol {
99
pub mod json;
1010
pub mod msg;
11+
pub mod task_impl;
1112
}
13+
14+
#[allow(dead_code)]
15+
pub mod new_protocol {
16+
pub mod msg;
17+
pub mod proto;
18+
pub mod task_impl;
19+
}
20+
21+
#[allow(dead_code)]
22+
pub mod task;
23+
1224
mod process;
1325

1426
use paths::{AbsPath, AbsPathBuf};

0 commit comments

Comments
 (0)