rust/crates/ra_proc_macro_srv/src/cli.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

2020-04-03 06:16:54 -05:00
//! Driver for proc macro server
use crate::{expand_task, list_macros};
2020-04-01 00:11:26 -05:00
use ra_proc_macro::msg::{self, Message};
use std::io;
pub fn run() {
2020-04-01 00:11:26 -05:00
loop {
let req = match read_request() {
Err(err) => {
// Panic here, as the stdin pipe may be closed.
2020-04-23 11:16:17 -05:00
// Otherwise, client will be restarted the service anyway.
panic!("Read message error on ra_proc_macro_srv: {}", err);
2020-04-01 00:11:26 -05:00
}
Ok(None) => continue,
Ok(Some(req)) => req,
};
2020-04-20 13:26:10 -05:00
let res = match req {
msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))),
2020-04-01 00:11:26 -05:00
msg::Request::ExpansionMacro(task) => {
2020-04-20 13:26:10 -05:00
expand_task(&task).map(msg::Response::ExpansionMacro)
2020-04-01 00:11:26 -05:00
}
2020-04-20 13:26:10 -05: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 00:11:26 -05:00
}
}
}
2020-04-20 13:26:10 -05: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())
}