Skip to content
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

Add request handling #1682

Open
wants to merge 1 commit into
base: spr/main/2ceebe3f
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions scarb/src/ops/proc_macro_server/json_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use serde::Serialize;

#[derive(Serialize)]
pub struct ErrResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably reside in proc-macros-server-api crate. I made a related comment about this in that PR: #1678 (comment)

message: String,
}

impl ErrResponse {
pub fn new(message: String) -> Self {
Self { message }
}
}
32 changes: 30 additions & 2 deletions scarb/src/ops/proc_macro_server/mod.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please start adding tests for this system starting with this PR

Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
use crate::compiler::plugin::proc_macro::ProcMacroHost;
use anyhow::Result;
use anyhow::{anyhow, Result};
use connection::Connection;
use json_rpc::ErrResponse;
use proc_macro_server_api::RpcResponse;

mod connection;
mod json_rpc;

pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> {
let connection = Connection::new();

//TODO
for i in 0..4 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use std::thread:available_parallelism. do not hardcode threading amounts

let receiver = connection.receiver.clone();
let sender = connection.sender.clone();

std::thread::Builder::new()
.name(format!("proc-macro-server-worker-thread-{i}"))
.spawn(move || {
for request in receiver {
let response = match request.method.as_str() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indentation is going crazy here, please:

  1. extract worker body to a separate function
  2. extract request method routing to another one

//TODO add method handlers
_ => Err(anyhow!("method not found")),
};

let value = response.unwrap_or_else(|err| {
serde_json::to_value(ErrResponse::new(err.to_string())).unwrap()
});
let res = RpcResponse {
id: request.id,
value,
};

sender.send(res).unwrap();
}
})
.expect("failed to spawn thread");
}

connection.join();

Expand Down
Loading