Skip to content

[Draft] feat: new protocol for proc-macro-api #19978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/proc-macro-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust-version.workspace = true
[dependencies]
serde.workspace = true
serde_derive.workspace = true
postcard = { version = "1.1.1", features = ["alloc"] }
serde_json = { workspace = true, features = ["unbounded_depth"] }
tracing.workspace = true
rustc-hash.workspace = true
Expand All @@ -24,7 +25,7 @@ paths = { workspace = true, features = ["serde1"] }
tt.workspace = true
stdx.workspace = true
# span = {workspace = true, default-features = false} does not work
span = { path = "../span", version = "0.0.0", default-features = false}
span = { path = "../span", version = "0.0.0", default-features = false }

intern.workspace = true

Expand Down
64 changes: 64 additions & 0 deletions crates/proc-macro-api/src/legacy_protocol/task_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Implement how to send and receive legacy protocol message on stdio stream(protocol layer)
//!
//! The implementation follows:
//! 1. Send Request
//! 2. Receive Response

use std::{
io::{self, BufRead, Write},
sync::Arc,
};

use crate::{
ServerError,
legacy_protocol::{
json::{read_json, write_json},
msg::{Request, Response},
},
task::TaskClient,
};

/// Sends a request to the server and reads the response.
fn send_request(
mut writer: &mut dyn Write,
mut reader: &mut dyn BufRead,
req: Request,
buf: &mut String,
) -> Result<Option<Response>, ServerError> {
use crate::legacy_protocol::msg::Message;

req.write(write_json, &mut writer).map_err(|err| ServerError {
message: "failed to write request".into(),
io: Some(Arc::new(err)),
})?;
let res = Response::read(read_json, &mut reader, buf).map_err(|err| ServerError {
message: "failed to read response".into(),
io: Some(Arc::new(err)),
})?;
Ok(res)
}

pub struct JsonTaskClient<'a> {
pub writer: &'a mut dyn Write,
pub reader: &'a mut dyn BufRead,
pub buf: &'a mut String,
}

impl TaskClient for JsonTaskClient<'_> {
type Task = Request;
type TaskResult = Response;

// Implement send_task for Json Legacy Protocol
// Basically what we have done in process.rs
fn send_task(&mut self, task: Self::Task) -> Result<Self::TaskResult, ServerError> {
send_request(self.writer, self.reader, task, self.buf).and_then(|res| {
res.ok_or_else(|| {
let message = "proc-macro server did not respond with data".to_owned();
ServerError {
io: Some(Arc::new(io::Error::new(io::ErrorKind::BrokenPipe, message.clone()))),
message,
}
})
})
}
}
12 changes: 12 additions & 0 deletions crates/proc-macro-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
pub mod legacy_protocol {
pub mod json;
pub mod msg;
pub mod task_impl;
}

#[allow(dead_code)]
pub mod new_protocol {
pub mod msg;
pub mod postcard;
pub mod task_impl;
}

#[allow(dead_code)]
pub mod task;

mod process;

use paths::{AbsPath, AbsPathBuf};
Expand Down
Loading