rust/crates/proc_macro_api/src/msg.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

2020-04-20 13:26:10 -05:00
//! Defines messages for cross-process message passing based on `ndjson` wire protocol
2020-03-26 15:26:34 -05:00
use std::{
2020-03-28 05:12:51 -05:00
convert::TryFrom,
2020-03-26 15:26:34 -05:00
io::{self, BufRead, Write},
};
2020-08-12 09:46:20 -05:00
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2020-03-28 05:12:51 -05:00
use crate::{
rpc::{ListMacrosResult, ListMacrosTask},
ExpansionResult, ExpansionTask,
};
2020-03-26 15:26:34 -05:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2020-03-28 05:12:51 -05:00
pub enum Request {
ListMacro(ListMacrosTask),
ExpansionMacro(ExpansionTask),
2020-03-26 15:26:34 -05:00
}
#[derive(Debug, Serialize, Deserialize, Clone)]
2020-03-28 05:12:51 -05:00
pub enum Response {
Error(ResponseError),
ListMacro(ListMacrosResult),
ExpansionMacro(ExpansionResult),
}
macro_rules! impl_try_from_response {
($ty:ty, $tag:ident) => {
impl TryFrom<Response> for $ty {
type Error = &'static str;
fn try_from(value: Response) -> Result<Self, Self::Error> {
match value {
Response::$tag(res) => Ok(res),
2020-04-20 13:26:10 -05:00
_ => Err(concat!("Failed to convert response to ", stringify!($tag))),
2020-03-28 05:12:51 -05:00
}
}
}
};
2020-03-26 15:26:34 -05:00
}
2020-03-28 05:12:51 -05:00
impl_try_from_response!(ListMacrosResult, ListMacro);
impl_try_from_response!(ExpansionResult, ExpansionMacro);
2020-03-26 15:26:34 -05:00
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ResponseError {
2020-03-28 05:12:51 -05:00
pub code: ErrorCode,
2020-03-26 15:26:34 -05:00
pub message: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
2020-03-28 05:12:51 -05:00
pub enum ErrorCode {
ServerErrorEnd,
ExpansionError,
2020-03-26 15:26:34 -05:00
}
2020-04-20 13:26:10 -05:00
pub trait Message: Serialize + DeserializeOwned {
fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> {
Ok(match read_json(inp)? {
None => None,
Some(text) => Some(serde_json::from_str(&text)?),
})
2020-03-26 15:26:34 -05:00
}
2020-04-20 13:26:10 -05:00
fn write(self, out: &mut impl Write) -> io::Result<()> {
2020-03-28 05:12:51 -05:00
let text = serde_json::to_string(&self)?;
2020-04-20 13:26:10 -05:00
write_json(out, &text)
2020-03-26 15:26:34 -05:00
}
}
2020-03-28 05:12:51 -05:00
impl Message for Request {}
impl Message for Response {}
2020-03-26 15:26:34 -05:00
2020-03-28 05:12:51 -05:00
fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> {
2020-03-26 15:26:34 -05:00
let mut buf = String::new();
2020-04-20 13:26:10 -05:00
inp.read_line(&mut buf)?;
buf.pop(); // Remove traling '\n'
Ok(match buf.len() {
0 => None,
_ => Some(buf),
})
2020-03-26 15:26:34 -05:00
}
2020-03-28 05:12:51 -05:00
fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
2020-03-26 15:26:34 -05:00
log::debug!("> {}", msg);
out.write_all(msg.as_bytes())?;
2020-03-28 05:12:51 -05:00
out.write_all(b"\n")?;
2020-03-26 15:26:34 -05:00
out.flush()?;
Ok(())
}