aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro/src/msg.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro/src/msg.rs')
-rw-r--r--crates/ra_proc_macro/src/msg.rs88
1 files changed, 0 insertions, 88 deletions
diff --git a/crates/ra_proc_macro/src/msg.rs b/crates/ra_proc_macro/src/msg.rs
deleted file mode 100644
index 95d9b8804..000000000
--- a/crates/ra_proc_macro/src/msg.rs
+++ /dev/null
@@ -1,88 +0,0 @@
1//! Defines messages for cross-process message passing based on `ndjson` wire protocol
2
3use std::{
4 convert::TryFrom,
5 io::{self, BufRead, Write},
6};
7
8use crate::{
9 rpc::{ListMacrosResult, ListMacrosTask},
10 ExpansionResult, ExpansionTask,
11};
12use serde::{de::DeserializeOwned, Deserialize, Serialize};
13
14#[derive(Debug, Serialize, Deserialize, Clone)]
15pub enum Request {
16 ListMacro(ListMacrosTask),
17 ExpansionMacro(ExpansionTask),
18}
19
20#[derive(Debug, Serialize, Deserialize, Clone)]
21pub enum Response {
22 Error(ResponseError),
23 ListMacro(ListMacrosResult),
24 ExpansionMacro(ExpansionResult),
25}
26
27macro_rules! impl_try_from_response {
28 ($ty:ty, $tag:ident) => {
29 impl TryFrom<Response> for $ty {
30 type Error = &'static str;
31 fn try_from(value: Response) -> Result<Self, Self::Error> {
32 match value {
33 Response::$tag(res) => Ok(res),
34 _ => Err(concat!("Failed to convert response to ", stringify!($tag))),
35 }
36 }
37 }
38 };
39}
40
41impl_try_from_response!(ListMacrosResult, ListMacro);
42impl_try_from_response!(ExpansionResult, ExpansionMacro);
43
44#[derive(Debug, Serialize, Deserialize, Clone)]
45pub struct ResponseError {
46 pub code: ErrorCode,
47 pub message: String,
48}
49
50#[derive(Debug, Serialize, Deserialize, Clone)]
51pub enum ErrorCode {
52 ServerErrorEnd,
53 ExpansionError,
54}
55
56pub trait Message: Serialize + DeserializeOwned {
57 fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> {
58 Ok(match read_json(inp)? {
59 None => None,
60 Some(text) => Some(serde_json::from_str(&text)?),
61 })
62 }
63 fn write(self, out: &mut impl Write) -> io::Result<()> {
64 let text = serde_json::to_string(&self)?;
65 write_json(out, &text)
66 }
67}
68
69impl Message for Request {}
70impl Message for Response {}
71
72fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> {
73 let mut buf = String::new();
74 inp.read_line(&mut buf)?;
75 buf.pop(); // Remove traling '\n'
76 Ok(match buf.len() {
77 0 => None,
78 _ => Some(buf),
79 })
80}
81
82fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
83 log::debug!("> {}", msg);
84 out.write_all(msg.as_bytes())?;
85 out.write_all(b"\n")?;
86 out.flush()?;
87 Ok(())
88}