Unnamed repository; edit this file 'description' to name the repository.
| -rw-r--r-- | crates/proc-macro-api/src/transport/codec/json.rs | 58 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/codec/postcard.rs | 40 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/json.rs | 48 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/transport/postcard.rs | 30 |
4 files changed, 78 insertions, 98 deletions
diff --git a/crates/proc-macro-api/src/transport/codec/json.rs b/crates/proc-macro-api/src/transport/codec/json.rs deleted file mode 100644 index 96db802e0b..0000000000 --- a/crates/proc-macro-api/src/transport/codec/json.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! Protocol functions for json. -use std::io::{self, BufRead, Write}; - -use serde::{Serialize, de::DeserializeOwned}; - -use crate::{Codec, transport::framing::Framing}; - -pub struct JsonProtocol; - -impl Framing for JsonProtocol { - type Buf = String; - - fn read<'a, R: BufRead + ?Sized>( - inp: &mut R, - buf: &'a mut String, - ) -> io::Result<Option<&'a mut String>> { - loop { - buf.clear(); - - inp.read_line(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('{') { - tracing::error!("proc-macro tried to print : {}", buf); - continue; - } - - return Ok(Some(buf)); - } - } - - fn write<W: Write + ?Sized>(out: &mut W, buf: &String) -> io::Result<()> { - tracing::debug!("> {}", buf); - out.write_all(buf.as_bytes())?; - out.write_all(b"\n")?; - out.flush() - } -} - -impl Codec for JsonProtocol { - fn encode<T: Serialize>(msg: &T) -> io::Result<String> { - Ok(serde_json::to_string(msg)?) - } - - fn decode<T: DeserializeOwned>(buf: &mut String) -> io::Result<T> { - let mut deserializer = serde_json::Deserializer::from_str(buf); - // Note that some proc-macro generate very deep syntax tree - // We have to disable the current limit of serde here - deserializer.disable_recursion_limit(); - Ok(T::deserialize(&mut deserializer)?) - } -} diff --git a/crates/proc-macro-api/src/transport/codec/postcard.rs b/crates/proc-macro-api/src/transport/codec/postcard.rs deleted file mode 100644 index 6f5319e75b..0000000000 --- a/crates/proc-macro-api/src/transport/codec/postcard.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! Postcard encode and decode implementations. - -use std::io::{self, BufRead, Write}; - -use serde::{Serialize, de::DeserializeOwned}; - -use crate::{Codec, transport::framing::Framing}; - -pub struct PostcardProtocol; - -impl Framing for PostcardProtocol { - type Buf = Vec<u8>; - - fn read<'a, R: BufRead + ?Sized>( - inp: &mut R, - buf: &'a mut Vec<u8>, - ) -> io::Result<Option<&'a mut Vec<u8>>> { - buf.clear(); - let n = inp.read_until(0, buf)?; - if n == 0 { - return Ok(None); - } - Ok(Some(buf)) - } - - fn write<W: Write + ?Sized>(out: &mut W, buf: &Vec<u8>) -> io::Result<()> { - out.write_all(buf)?; - out.flush() - } -} - -impl Codec for PostcardProtocol { - fn encode<T: Serialize>(msg: &T) -> io::Result<Vec<u8>> { - postcard::to_allocvec_cobs(msg).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - } - - fn decode<T: DeserializeOwned>(buf: &mut Self::Buf) -> io::Result<T> { - postcard::from_bytes_cobs(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) - } -} diff --git a/crates/proc-macro-api/src/transport/json.rs b/crates/proc-macro-api/src/transport/json.rs new file mode 100644 index 0000000000..da79dc5309 --- /dev/null +++ b/crates/proc-macro-api/src/transport/json.rs @@ -0,0 +1,48 @@ +//! Protocol functions for json. +use std::io::{self, BufRead, Write}; + +use serde::{Serialize, de::DeserializeOwned}; + +pub fn read<'a, R: BufRead + ?Sized>( + inp: &mut R, + buf: &'a mut String, +) -> io::Result<Option<&'a mut String>> { + loop { + buf.clear(); + + inp.read_line(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('{') { + tracing::error!("proc-macro tried to print : {}", buf); + continue; + } + + return Ok(Some(buf)); + } +} + +pub fn write<W: Write + ?Sized>(out: &mut W, buf: &String) -> io::Result<()> { + tracing::debug!("> {}", buf); + out.write_all(buf.as_bytes())?; + out.write_all(b"\n")?; + out.flush() +} + +pub fn encode<T: Serialize>(msg: &T) -> io::Result<String> { + Ok(serde_json::to_string(msg)?) +} + +pub fn decode<T: DeserializeOwned>(buf: &mut str) -> io::Result<T> { + let mut deserializer = serde_json::Deserializer::from_str(buf); + // Note that some proc-macro generate very deep syntax tree + // We have to disable the current limit of serde here + deserializer.disable_recursion_limit(); + Ok(T::deserialize(&mut deserializer)?) +} diff --git a/crates/proc-macro-api/src/transport/postcard.rs b/crates/proc-macro-api/src/transport/postcard.rs new file mode 100644 index 0000000000..ddd5f405d5 --- /dev/null +++ b/crates/proc-macro-api/src/transport/postcard.rs @@ -0,0 +1,30 @@ +//! Postcard encode and decode implementations. + +use std::io::{self, BufRead, Write}; + +use serde::{Serialize, de::DeserializeOwned}; + +pub fn read<'a, R: BufRead + ?Sized>( + inp: &mut R, + buf: &'a mut Vec<u8>, +) -> io::Result<Option<&'a mut Vec<u8>>> { + buf.clear(); + let n = inp.read_until(0, buf)?; + if n == 0 { + return Ok(None); + } + Ok(Some(buf)) +} + +pub fn write<W: Write + ?Sized>(out: &mut W, buf: &[u8]) -> io::Result<()> { + out.write_all(buf)?; + out.flush() +} + +pub fn encode<T: Serialize>(msg: &T) -> io::Result<Vec<u8>> { + postcard::to_allocvec_cobs(msg).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} + +pub fn decode<T: DeserializeOwned>(buf: &mut [u8]) -> io::Result<T> { + postcard::from_bytes_cobs(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} |