Skip to content

Commit

Permalink
Add request handling
Browse files Browse the repository at this point in the history
commit-id:723491f5
  • Loading branch information
Draggu committed Nov 5, 2024
1 parent 58c0e91 commit 8ddf509
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

55 changes: 52 additions & 3 deletions scarb/src/ops/proc_macro_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,64 @@
use crate::compiler::plugin::proc_macro::ProcMacroHost;
use anyhow::Result;
use std::num::NonZero;
use std::thread::available_parallelism;

use anyhow::{anyhow, Result};
use connection::Connection;
use crossbeam_channel::{Receiver, Sender};
use scarb_proc_macro_server_types::jsonrpc::{ResponseError, RpcRequest, RpcResponse};
use serde_json::Value;

use crate::compiler::plugin::proc_macro::ProcMacroHost;

mod connection;

pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> {
let connection = Connection::new();
let available_parallelism = available_parallelism().map(NonZero::get).unwrap_or(4);

for i in 0..available_parallelism {
let receiver = connection.receiver.clone();
let sender = connection.sender.clone();

//TODO
std::thread::Builder::new()
.name(format!("proc-macro-server-worker-thread-{i}"))
.spawn(move || {
handle_requests(receiver, sender);
})
.expect("failed to spawn thread");
}

connection.join();

Ok(())
}

fn handle_requests(receiver: Receiver<RpcRequest>, sender: Sender<RpcResponse>) {
for request in receiver {
let id = request.id;
let response = route_request(request);

let response = match response {
Ok(result) => RpcResponse {
id,
result: Some(result),
error: None,
},
Err(error) => RpcResponse {
id,
result: None,
error: Some(ResponseError {
message: error.to_string(),
}),
},
};

sender.send(response).unwrap();
}
}

fn route_request(request: RpcRequest) -> Result<Value> {
match request.method.as_str() {
//TODO add method handlers
_ => Err(anyhow!("method not found")),
}
}

0 comments on commit 8ddf509

Please sign in to comment.