aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro_srv/src/main.rs
blob: 70743c1f40eabcda3ea5022049f564c84b5e4fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Driver for proc macro server

use ra_proc_macro::msg::{self, Message};
use ra_proc_macro_srv::{expand_task, list_macros};

use std::io;

fn read_request() -> Result<Option<msg::Request>, io::Error> {
    let stdin = io::stdin();
    let mut stdin = stdin.lock();
    msg::Request::read(&mut stdin)
}

fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
    let msg: msg::Response = match res {
        Ok(res) => res,
        Err(err) => msg::Response::Error(msg::ResponseError {
            code: msg::ErrorCode::ExpansionError,
            message: err,
        }),
    };

    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    msg.write(&mut stdout)
}
fn main() {
    loop {
        let req = match read_request() {
            Err(err) => {
                eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string());
                continue;
            }
            Ok(None) => continue,
            Ok(Some(req)) => req,
        };

        match req {
            msg::Request::ListMacro(task) => {
                if let Err(err) =
                    write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it)))
                {
                    eprintln!("Write message error on list macro: {}", err);
                }
            }
            msg::Request::ExpansionMacro(task) => {
                if let Err(err) =
                    write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it)))
                {
                    eprintln!("Write message error on expansion macro: {}", err);
                }
            }
        }
    }
}