diff options
Diffstat (limited to 'crates/proc_macro_srv/src/cli.rs')
-rw-r--r-- | crates/proc_macro_srv/src/cli.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/crates/proc_macro_srv/src/cli.rs b/crates/proc_macro_srv/src/cli.rs new file mode 100644 index 000000000..d428b9567 --- /dev/null +++ b/crates/proc_macro_srv/src/cli.rs | |||
@@ -0,0 +1,39 @@ | |||
1 | //! Driver for proc macro server | ||
2 | |||
3 | use crate::ProcMacroSrv; | ||
4 | use proc_macro_api::msg::{self, Message}; | ||
5 | use std::io; | ||
6 | |||
7 | pub fn run() -> io::Result<()> { | ||
8 | let mut srv = ProcMacroSrv::default(); | ||
9 | |||
10 | while let Some(req) = read_request()? { | ||
11 | let res = match req { | ||
12 | msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), | ||
13 | msg::Request::ExpansionMacro(task) => { | ||
14 | srv.expand(&task).map(msg::Response::ExpansionMacro) | ||
15 | } | ||
16 | }; | ||
17 | |||
18 | let msg = res.unwrap_or_else(|err| { | ||
19 | msg::Response::Error(msg::ResponseError { | ||
20 | code: msg::ErrorCode::ExpansionError, | ||
21 | message: err, | ||
22 | }) | ||
23 | }); | ||
24 | |||
25 | if let Err(err) = write_response(msg) { | ||
26 | eprintln!("Write message error: {}", err); | ||
27 | } | ||
28 | } | ||
29 | |||
30 | Ok(()) | ||
31 | } | ||
32 | |||
33 | fn read_request() -> io::Result<Option<msg::Request>> { | ||
34 | msg::Request::read(&mut io::stdin().lock()) | ||
35 | } | ||
36 | |||
37 | fn write_response(msg: msg::Response) -> io::Result<()> { | ||
38 | msg.write(&mut io::stdout().lock()) | ||
39 | } | ||