rust/crates/proc_macro_api/src/msg.rs

111 lines
2.9 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)]
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)]
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, buf: &mut String) -> io::Result<Option<Self>> {
Ok(match read_json(inp, buf)? {
2020-04-20 13:26:10 -05:00
None => None,
Some(text) => {
2021-06-12 22:54:16 -05:00
let mut deserializer = serde_json::Deserializer::from_str(text);
// Note that some proc-macro generate very deep syntax tree
// We have to disable the current limit of serde here
deserializer.disable_recursion_limit();
Some(Self::deserialize(&mut deserializer)?)
}
2020-04-20 13:26:10 -05:00
})
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
fn read_json<'a>(
inp: &mut impl BufRead,
mut buf: &'a mut String,
) -> io::Result<Option<&'a String>> {
loop {
buf.clear();
inp.read_line(&mut buf)?;
buf.pop(); // Remove trailing '\n'
if buf.is_empty() {
return Ok(None);
}
// Some ill behaved macro try to use stdout for debugging
// We ignore it here
if !buf.starts_with('{') {
log::error!("proc-macro tried to print : {}", buf);
continue;
}
return Ok(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(())
}