debug3_derive/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use quote::quote;
use syn::Fields;
use synstructure::{decl_derive, AddBounds};

decl_derive!([Debug/*, attributes(debug_skip)*/] => derive_debug);

// Based on
// https://github.com/panicbit/custom_debug/blob/master/custom_debug_derive/src/lib.rs

fn derive_debug(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
    // Handle ZST
    if s.variants().is_empty() {
        return s.gen_impl(quote! {
            gen impl debug3::Debug for @Self {
                fn fmt(&self, f: &mut debug3::Formatter) {
                    match *self {}
                }
            }
        });
    }

    s.add_bounds(AddBounds::Generics);

    let variants = s.each_variant(|variant| {
        let name = variant.ast().ident.to_string();

        let debug_helper = match variant.ast().fields {
            Fields::Named(_) | Fields::Unit => quote! {debug_struct},
            Fields::Unnamed(_) => quote! {debug_tuple},
        };

        let variant_body = variant.bindings().iter().map(|b| {
            let format = quote! {#b};

            if let Some(ref name) = b.ast().ident.as_ref().map(<_>::to_string) {
                quote! {
                    s.field(#name, #format);
                }
            } else {
                quote! {
                    s.field(#format);
                }
            }
        });

        quote! {
            let mut s = f.#debug_helper(#name);
            #(#variant_body)*
            s.finish()
        }
    });

    s.gen_impl(quote! {
        #[automatically_derived]
        gen impl debug3::Debug for @Self {
            fn fmt(&self, f: &mut debug3::Formatter) {
                match self { #variants }
            }
        }
    })
}