Implement Encoder for Diagnostic manually
...so we can skip serializing `tool_metadata` if it hasn't been set. This makes the output a bit cleaner, and avoiding having to update a bunch of unrelated tests.
This commit is contained in:
parent
82ccb6582a
commit
50572d6629
12 changed files with 84 additions and 52 deletions
|
@ -27,6 +27,7 @@ use std::sync::{Arc, Mutex};
|
|||
use std::vec;
|
||||
|
||||
use rustc_serialize::json::{as_json, as_pretty_json};
|
||||
use rustc_serialize::{Encodable, Encoder};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -169,7 +170,8 @@ impl Emitter for JsonEmitter {
|
|||
|
||||
// The following data types are provided just for serialisation.
|
||||
|
||||
#[derive(Encodable)]
|
||||
// NOTE: this has a manual implementation of Encodable which needs to be updated in
|
||||
// parallel.
|
||||
struct Diagnostic {
|
||||
/// The primary error message.
|
||||
message: String,
|
||||
|
@ -185,6 +187,36 @@ struct Diagnostic {
|
|||
tool_metadata: ToolMetadata,
|
||||
}
|
||||
|
||||
macro_rules! encode_fields {
|
||||
($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
|
||||
{
|
||||
let mut idx = $idx;
|
||||
$(
|
||||
$enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
|
||||
idx += 1;
|
||||
)+
|
||||
idx
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Special-case encoder to skip tool_metadata if not set
|
||||
impl<E: Encoder> Encodable<E> for Diagnostic {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_struct("diagnostic", 7, |s| {
|
||||
let mut idx = 0;
|
||||
|
||||
idx = encode_fields!(s, self, idx, [message, code, level, spans, children, rendered]);
|
||||
if self.tool_metadata.is_set() {
|
||||
idx = encode_fields!(s, self, idx, [tool_metadata]);
|
||||
}
|
||||
|
||||
let _ = idx;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Encodable)]
|
||||
struct DiagnosticSpan {
|
||||
file_name: String,
|
||||
|
|
|
@ -83,6 +83,10 @@ impl ToolMetadata {
|
|||
fn new(json: Json) -> Self {
|
||||
ToolMetadata(Some(json))
|
||||
}
|
||||
|
||||
fn is_set(&self) -> bool {
|
||||
self.0.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for ToolMetadata {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue