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