mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Abstract proc-macro-srv protocol format
This commit is contained in:
parent
7c7c0cbffb
commit
c236190b60
6 changed files with 60 additions and 39 deletions
34
crates/proc-macro-api/src/json.rs
Normal file
34
crates/proc-macro-api/src/json.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use std::io::{self, BufRead, Write};
|
||||
|
||||
pub fn read_json<'a>(
|
||||
inp: &mut impl BufRead,
|
||||
buf: &'a mut String,
|
||||
) -> io::Result<Option<&'a 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_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
|
||||
tracing::debug!("> {}", msg);
|
||||
out.write_all(msg.as_bytes())?;
|
||||
out.write_all(b"\n")?;
|
||||
out.flush()?;
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue