Skip to content

Commit

Permalink
Add RA_EXPAND_MACRO_RESPONSE_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad20012 committed Dec 5, 2023
1 parent 05df6c5 commit 9a30568
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions crates/proc-macro-api/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub const NO_VERSION_CHECK_VERSION: u32 = 0;
pub const VERSION_CHECK_VERSION: u32 = 1;
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
pub const HAS_GLOBAL_SPANS: u32 = 3;
pub const EXPAND_MACRO_RESPONSE_PREFIX: u32 = 4;

pub const CURRENT_API_VERSION: u32 = HAS_GLOBAL_SPANS;
pub const CURRENT_API_VERSION: u32 = EXPAND_MACRO_RESPONSE_PREFIX;

#[derive(Debug, Serialize, Deserialize)]
pub enum Request {
Expand Down Expand Up @@ -96,13 +97,21 @@ pub trait Message: Serialize + DeserializeOwned {
})
}
fn write(self, out: &mut impl Write) -> io::Result<()> {
let text = serde_json::to_string(&self)?;
write_json(out, &text)
write_message_as_json_to(out, self)
}
}

impl Message for Request {}
impl Message for Response {}

impl Message for Response {
fn write(self, out: &mut impl Write) -> io::Result<()> {
match (&self, std::env::var("RA_EXPAND_MACRO_RESPONSE_PREFIX")) {
(Response::ExpandMacro(..), Ok(prefix)) => out.write_all(prefix.as_bytes())?,
_ => {}
}
write_message_as_json_to(out, self)
}
}

fn read_json<'a>(inp: &mut impl BufRead, buf: &'a mut String) -> io::Result<Option<&'a String>> {
loop {
Expand All @@ -126,6 +135,11 @@ fn read_json<'a>(inp: &mut impl BufRead, buf: &'a mut String) -> io::Result<Opti
}
}

fn write_message_as_json_to(out: &mut impl Write, m: impl Message) -> io::Result<()> {
let text = serde_json::to_string(&m)?;
write_json(out, &text)
}

fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
tracing::debug!("> {}", msg);
out.write_all(msg.as_bytes())?;
Expand Down

0 comments on commit 9a30568

Please sign in to comment.