rust/crates/proc_macro_srv/src/cli.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

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