rust-analyzer/crates/ra_proc_macro_srv/src/cli.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

2020-04-03 11:16:54 +00:00
//! Driver for proc macro server
use crate::{expand_task, list_macros};
2020-04-01 05:11:26 +00:00
use ra_proc_macro::msg::{self, Message};
use std::io;
2020-04-23 17:38:58 +00:00
pub fn run() -> io::Result<()> {
2020-04-01 05:11:26 +00:00
loop {
2020-04-23 17:38:58 +00:00
// bubble up the error for read request,
// as the stdin pipe may be closed.
let req = match read_request()? {
None => continue,
Some(req) => req,
2020-04-01 05:11:26 +00:00
};
2020-04-20 18:26:10 +00:00
let res = match req {
msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))),
2020-04-01 05:11:26 +00:00
msg::Request::ExpansionMacro(task) => {
2020-04-20 18:26:10 +00:00
expand_task(&task).map(msg::Response::ExpansionMacro)
2020-04-01 05:11:26 +00:00
}
2020-04-20 18:26:10 +00:00
};
let msg = res.unwrap_or_else(|err| {
msg::Response::Error(msg::ResponseError {
code: msg::ErrorCode::ExpansionError,
message: err,
})
});
if let Err(err) = write_response(msg) {
eprintln!("Write message error: {}", err);
2020-04-01 05:11:26 +00:00
}
}
}
2020-04-20 18:26:10 +00:00
fn read_request() -> io::Result<Option<msg::Request>> {
msg::Request::read(&mut io::stdin().lock())
}
fn write_response(msg: msg::Response) -> io::Result<()> {
msg.write(&mut io::stdout().lock())
}