-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: omit spans from debug prints
Spans are rarely useful in the `Debug` representation of a struct and clutter the snapshots a lot. A custom derive macro mimics the stdlib's macro but filters out any fields that use the `Span` struct.
- Loading branch information
Showing
29 changed files
with
142 additions
and
2,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use proc_macro2::{Literal, Span, TokenStream}; | ||
use quote::quote; | ||
use syn::{spanned::Spanned, Data, DeriveInput, Field, Fields, Ident, Type, TypePath}; | ||
|
||
macro_rules! bail { | ||
($tokens:expr, $($arg:tt)*) => { | ||
return Err(syn::Error::new($tokens.span(), format!($($arg)*))) | ||
}; | ||
} | ||
|
||
pub fn expand(derive: DeriveInput) -> syn::Result<TokenStream> { | ||
let ident = &derive.ident; | ||
let generics = &derive.generics; | ||
let name = Literal::string(&ident.to_string()); | ||
|
||
match derive.data { | ||
Data::Struct(data) => { | ||
let fields= expand_fields(name, &data.fields); | ||
|
||
Ok(quote! { | ||
impl #generics ::core::fmt::Debug for #ident #generics { | ||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { | ||
match self { | ||
Self #fields | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
Data::Enum(data) => { | ||
let variants = data.variants.iter().map(|variant| { | ||
let ident = &variant.ident; | ||
let fields = expand_fields(Literal::string(&ident.to_string()), &variant.fields); | ||
|
||
quote! { | ||
Self::#ident #fields | ||
} | ||
}); | ||
|
||
Ok(quote! { | ||
impl #generics ::core::fmt::Debug for #ident #generics { | ||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { | ||
match self { | ||
#(#variants,)* | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
Data::Union(_) => bail!(derive, "unions not supported"), | ||
} | ||
} | ||
|
||
fn expand_fields(name: Literal, fields: &Fields) -> TokenStream { | ||
match fields { | ||
Fields::Named(fields) => { | ||
let names = fields.named.iter().map(|field| { | ||
let ident = field.ident.as_ref().unwrap(); | ||
if filter_span(&field.ty) { | ||
quote! { #ident: _ } | ||
} else { | ||
quote! { #ident } | ||
} | ||
}); | ||
|
||
let fields = fields | ||
.named | ||
.iter() | ||
.filter(|&field| !filter_span(&field.ty)) | ||
.map(expand_named_field); | ||
|
||
quote! { | ||
{#(#names,)*} => f.debug_struct(#name) | ||
#(#fields)* | ||
.finish() | ||
} | ||
} | ||
Fields::Unnamed(fields) => { | ||
let names = fields.unnamed.iter().enumerate().map(|(i, field)| { | ||
if filter_span(&field.ty) { | ||
quote! { _ } | ||
} else { | ||
let ident = Ident::new(&format!("n{i}"), Span::call_site()); | ||
quote! { #ident } | ||
} | ||
}); | ||
|
||
let fields = fields | ||
.unnamed | ||
.iter() | ||
.enumerate() | ||
.filter(|(_, field)| !filter_span(&field.ty)) | ||
.map(expand_unnamed_field); | ||
|
||
quote! { | ||
(#(#names,)*) => f.debug_tuple(#name) | ||
#(#fields)* | ||
.finish() | ||
} | ||
} | ||
Fields::Unit => quote! { => f.write_str(#name) }, | ||
} | ||
} | ||
|
||
fn expand_named_field(field: &Field) -> TokenStream { | ||
let ident = field.ident.as_ref().unwrap(); | ||
let name = Literal::string(&ident.to_string()); | ||
|
||
quote! { | ||
.field(#name, &#ident) | ||
} | ||
} | ||
|
||
fn expand_unnamed_field((i, _field): (usize, &Field)) -> TokenStream { | ||
let ident = Ident::new(&format!("n{i}"), Span::call_site()); | ||
|
||
quote! { | ||
.field(&#ident) | ||
} | ||
} | ||
|
||
fn filter_span(ty: &Type) -> bool { | ||
let Type::Path(TypePath { path, .. }) = ty else { | ||
return false; | ||
}; | ||
|
||
path.segments | ||
.last() | ||
.map_or(false, |segment| segment.ident == "Span") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.