2020-03-28 10:12:51 +00:00
|
|
|
//! Defines messages for cross-process message based on `ndjson` wire protocol
|
2020-03-26 20:26:34 +00:00
|
|
|
|
|
|
|
use std::{
|
2020-03-28 10:12:51 +00:00
|
|
|
convert::TryFrom,
|
2020-03-26 20:26:34 +00:00
|
|
|
io::{self, BufRead, Write},
|
|
|
|
};
|
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
use crate::{
|
|
|
|
rpc::{ListMacrosResult, ListMacrosTask},
|
|
|
|
ExpansionResult, ExpansionTask,
|
|
|
|
};
|
2020-03-26 20:26:34 +00:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-03-28 10:12:51 +00:00
|
|
|
pub enum Request {
|
|
|
|
ListMacro(ListMacrosTask),
|
|
|
|
ExpansionMacro(ExpansionTask),
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-03-28 10:12:51 +00: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),
|
|
|
|
_ => Err("Fail to convert from response"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
impl_try_from_response!(ListMacrosResult, ListMacro);
|
|
|
|
impl_try_from_response!(ExpansionResult, ExpansionMacro);
|
|
|
|
|
2020-03-26 20:26:34 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct ResponseError {
|
2020-03-28 10:12:51 +00:00
|
|
|
pub code: ErrorCode,
|
2020-03-26 20:26:34 +00:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2020-03-28 10:12:51 +00:00
|
|
|
pub enum ErrorCode {
|
|
|
|
ServerErrorEnd,
|
|
|
|
ExpansionError,
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
pub trait Message: Sized + Serialize + DeserializeOwned {
|
|
|
|
fn read(r: &mut impl BufRead) -> io::Result<Option<Self>> {
|
|
|
|
let text = match read_json(r)? {
|
2020-03-26 20:26:34 +00:00
|
|
|
None => return Ok(None),
|
|
|
|
Some(text) => text,
|
|
|
|
};
|
|
|
|
let msg = serde_json::from_str(&text)?;
|
|
|
|
Ok(Some(msg))
|
|
|
|
}
|
2020-03-28 10:12:51 +00:00
|
|
|
fn write(self, w: &mut impl Write) -> io::Result<()> {
|
|
|
|
let text = serde_json::to_string(&self)?;
|
|
|
|
write_json(w, &text)
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
impl Message for Request {}
|
|
|
|
impl Message for Response {}
|
2020-03-26 20:26:34 +00:00
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> {
|
2020-03-26 20:26:34 +00:00
|
|
|
let mut buf = String::new();
|
2020-03-28 10:12:51 +00:00
|
|
|
if inp.read_line(&mut buf)? == 0 {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
// Remove ending '\n'
|
|
|
|
let buf = &buf[..buf.len() - 1];
|
|
|
|
if buf.is_empty() {
|
|
|
|
return Ok(None);
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
2020-03-28 10:12:51 +00:00
|
|
|
Ok(Some(buf.to_string()))
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:12:51 +00:00
|
|
|
fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
|
2020-03-26 20:26:34 +00:00
|
|
|
log::debug!("> {}", msg);
|
|
|
|
out.write_all(msg.as_bytes())?;
|
2020-03-28 10:12:51 +00:00
|
|
|
out.write_all(b"\n")?;
|
2020-03-26 20:26:34 +00:00
|
|
|
out.flush()?;
|
|
|
|
Ok(())
|
|
|
|
}
|