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

40 lines
1 KiB
Rust
Raw Normal View History

2020-04-03 11:16:54 +00:00
//! Driver for proc macro server
2020-04-24 02:23:01 +00:00
use crate::ProcMacroSrv;
2020-08-13 10:07:28 +00:00
use proc_macro_api::msg::{self, Message};
2020-04-01 05:11:26 +00:00
use std::io;
2020-04-23 17:38:58 +00:00
pub fn run() -> io::Result<()> {
2020-04-24 02:23:01 +00:00
let mut srv = ProcMacroSrv::default();
2020-04-24 01:27:37 +00:00
while let Some(req) = read_request()? {
2020-04-20 18:26:10 +00:00
let res = match req {
2020-04-24 02:23:01 +00:00
msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro),
2020-04-01 05:11:26 +00:00
msg::Request::ExpansionMacro(task) => {
2020-04-24 02:23:01 +00:00
srv.expand(&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-24 01:27:37 +00:00
Ok(())
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())
}