mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-19 10:00:27 +00:00

When dealing with proc macros, there are two very different kinds of errors: * first, usual errors of "proc macro panicked on this particular input" * second, the proc macro server might day if the user, eg, kills it First kind of errors are expected and are a normal output, while the second kind are genuine IO-errors. For this reason, we use a curious nested result here: `Result<Result<T, E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
31 lines
833 B
Rust
31 lines
833 B
Rust
//! Driver for proc macro server
|
|
use std::io;
|
|
|
|
use proc_macro_api::msg::{self, Message};
|
|
|
|
use crate::ProcMacroSrv;
|
|
|
|
pub fn run() -> io::Result<()> {
|
|
let mut srv = ProcMacroSrv::default();
|
|
let mut buf = String::new();
|
|
|
|
while let Some(req) = read_request(&mut buf)? {
|
|
let res = match req {
|
|
msg::Request::ListMacros { dylib_path } => {
|
|
msg::Response::ListMacros(srv.list_macros(&dylib_path))
|
|
}
|
|
msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)),
|
|
};
|
|
write_response(res)?
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn read_request(buf: &mut String) -> io::Result<Option<msg::Request>> {
|
|
msg::Request::read(&mut io::stdin().lock(), buf)
|
|
}
|
|
|
|
fn write_response(msg: msg::Response) -> io::Result<()> {
|
|
msg.write(&mut io::stdout().lock())
|
|
}
|