Make sure all fields are accounted for in encode_fields!

This will make sure the encoder will get updated if any new fields are
added to Diagnostic.
This commit is contained in:
Jeremy Fitzhardinge 2021-02-06 20:29:04 -08:00
parent 50572d6629
commit 91d8c3b521

View File

@ -188,11 +188,24 @@ struct Diagnostic {
}
macro_rules! encode_fields {
($enc:expr, $s:expr, $idx:expr, [ $($name:ident),+$(,)? ]) => {
(
$enc:expr, // encoder
$idx:expr, // starting field index
$struct:expr, // struct we're serializing
$struct_name:ident, // struct name
[ $($name:ident),+$(,)? ], // fields to encode
[ $($ignore:ident),+$(,)? ] // fields we're skipping
) => {
{
// Pattern match to make sure all fields are accounted for
let $struct_name { $($name,)+ $($ignore: _,)+ } = $struct;
let mut idx = $idx;
$(
$enc.emit_struct_field(stringify!($name), idx, |enc| $s.$name.encode(enc))?;
$enc.emit_struct_field(
stringify!($name),
idx,
|enc| $name.encode(enc),
)?;
idx += 1;
)+
idx
@ -206,9 +219,23 @@ 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]);
idx = encode_fields!(
s,
idx,
self,
Self,
[message, code, level, spans, children, rendered],
[tool_metadata]
);
if self.tool_metadata.is_set() {
idx = encode_fields!(s, self, idx, [tool_metadata]);
idx = encode_fields!(
s,
idx,
self,
Self,
[tool_metadata],
[message, code, level, spans, children, rendered]
);
}
let _ = idx;