2020-04-03 11:16:54 +00:00
|
|
|
//! Driver for proc macro server
|
internal: cleanup proc macro server error handlig
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
2021-08-31 16:01:39 +00:00
|
|
|
use std::io;
|
2020-04-03 11:16:54 +00:00
|
|
|
|
2020-08-13 10:07:28 +00:00
|
|
|
use proc_macro_api::msg::{self, Message};
|
internal: cleanup proc macro server error handlig
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
2021-08-31 16:01:39 +00:00
|
|
|
|
|
|
|
use crate::ProcMacroSrv;
|
2020-04-01 05:11:26 +00:00
|
|
|
|
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();
|
2021-03-23 19:47:08 +00:00
|
|
|
let mut buf = String::new();
|
2020-04-24 02:23:01 +00:00
|
|
|
|
2021-03-23 19:47:08 +00:00
|
|
|
while let Some(req) = read_request(&mut buf)? {
|
2020-04-20 18:26:10 +00:00
|
|
|
let res = match req {
|
internal: cleanup proc macro server error handlig
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
2021-08-31 16:01:39 +00:00
|
|
|
msg::Request::ListMacros { dylib_path } => {
|
|
|
|
msg::Response::ListMacros(srv.list_macros(&dylib_path))
|
2020-04-01 05:11:26 +00:00
|
|
|
}
|
internal: cleanup proc macro server error handlig
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
2021-08-31 16:01:39 +00:00
|
|
|
msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)),
|
2020-04-20 18:26:10 +00:00
|
|
|
};
|
internal: cleanup proc macro server error handlig
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
2021-08-31 16:01:39 +00:00
|
|
|
write_response(res)?
|
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
|
|
|
|
2021-03-23 19:47:08 +00:00
|
|
|
fn read_request(buf: &mut String) -> io::Result<Option<msg::Request>> {
|
|
|
|
msg::Request::read(&mut io::stdin().lock(), buf)
|
2020-04-20 18:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_response(msg: msg::Response) -> io::Result<()> {
|
|
|
|
msg.write(&mut io::stdout().lock())
|
|
|
|
}
|