diff options
Diffstat (limited to 'crates/ra_proc_macro_srv/src/cli.rs')
-rw-r--r-- | crates/ra_proc_macro_srv/src/cli.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs new file mode 100644 index 000000000..c771f2b38 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/cli.rs | |||
@@ -0,0 +1,56 @@ | |||
1 | //! Driver for proc macro server | ||
2 | |||
3 | use crate::{expand_task, list_macros}; | ||
4 | use ra_proc_macro::msg::{self, Message}; | ||
5 | |||
6 | use std::io; | ||
7 | |||
8 | fn read_request() -> Result<Option<msg::Request>, io::Error> { | ||
9 | let stdin = io::stdin(); | ||
10 | let mut stdin = stdin.lock(); | ||
11 | msg::Request::read(&mut stdin) | ||
12 | } | ||
13 | |||
14 | fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> { | ||
15 | let msg: msg::Response = match res { | ||
16 | Ok(res) => res, | ||
17 | Err(err) => msg::Response::Error(msg::ResponseError { | ||
18 | code: msg::ErrorCode::ExpansionError, | ||
19 | message: err, | ||
20 | }), | ||
21 | }; | ||
22 | |||
23 | let stdout = io::stdout(); | ||
24 | let mut stdout = stdout.lock(); | ||
25 | msg.write(&mut stdout) | ||
26 | } | ||
27 | |||
28 | pub fn run() { | ||
29 | loop { | ||
30 | let req = match read_request() { | ||
31 | Err(err) => { | ||
32 | eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string()); | ||
33 | continue; | ||
34 | } | ||
35 | Ok(None) => continue, | ||
36 | Ok(Some(req)) => req, | ||
37 | }; | ||
38 | |||
39 | match req { | ||
40 | msg::Request::ListMacro(task) => { | ||
41 | if let Err(err) = | ||
42 | write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it))) | ||
43 | { | ||
44 | eprintln!("Write message error on list macro: {}", err); | ||
45 | } | ||
46 | } | ||
47 | msg::Request::ExpansionMacro(task) => { | ||
48 | if let Err(err) = | ||
49 | write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it))) | ||
50 | { | ||
51 | eprintln!("Write message error on expansion macro: {}", err); | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | } | ||