debugify 0.2.0

Derive macro for `std::fmt::Debug` focused on reducing boilerplate.Supports both format strings and formatter functions.
Documentation
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::collections::HashMap;

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{
    parse::{Parse, ParseStream},
    ConstParam, Fields, GenericParam, LifetimeParam, TypeParam,
};

/// # debugify
///
/// Derive macro for `std::fmt::Debug` focused on reducing boilerplate. Supports both format strings and formatter functions.
///
/// ## Formats
///
/// Formats can be specified either as a format string or as the path to a formatter function.
/// A formatter function must adhere to the following signature: `fn(&T) -> String`.
///
/// ## Attributes
///
/// In case of a conflict between attributes, the order of precedence is
///
/// 1. field attribute
/// 2. field name
/// 3. field type
///
/// If no format is specified, the default format is used.
///
/// ### Item attributes
///
/// These attributes are applied to a struct or enum.
///
/// #### `field_name`
///
/// Applies to the formatting of all fields with the given names inside the item.
///
/// ```rust
/// use debugify::Debugify;
///
/// #[derive(Debugify)]
/// #[debugify(field_name(
///     [bar, biz] = "foobar{}",
///     baz = "foobaz{}",
/// ))]
/// struct Foo {
///     bar: i32,
///     baz: String,
///     biz: &'static str,
///     qux: i64,
///
/// }
///
/// let foo = Foo {
///     bar: 123,
///     baz: "hello".to_string(),
///     biz: "world",
///     qux: 456,
/// };
///
/// let foo_debug = format!("{foo:?}");
/// assert_eq!(foo_debug, "Foo { bar: foobar123, baz: foobazhello, biz: foobarworld, qux: 456 }");
/// ```
///
/// #### `field_type`
///
/// Applies to the formatting of all fields with the given types inside the item.
///
/// ```rust
/// use debugify::Debugify;
///
/// #[derive(Debugify)]
/// #[debugify(field_type(
///     [i32, &'static str] = "foobar{}",
///     String = "foobaz{}",
/// ))]
/// struct Foo {
///     bar: i32,
///     baz: String,
///     biz: &'static str,
///     qux: i64,
/// }
///
/// let foo = Foo {
///     bar: 123,
///     baz: "hello".to_string(),
///     biz: "world",
///     qux: 456,
/// };
///
/// let foo_debug = format!("{foo:?}");
/// assert_eq!(foo_debug, "Foo { bar: foobar123, baz: foobazhello, biz: foobarworld, qux: 456 }");
/// ```
///
/// ### Field attributes
///
/// Currently the only field attribute support is a format specifier.
///
/// ```rust
/// use debugify::Debugify;
///
/// #[derive(Debugify)]
/// #[debugify(field_name(bar = "foo{}"))]
/// struct Foo {
///     #[debugify("bar{}")]
///     bar: i32,
///     baz: String,
/// }
///
/// let foo = Foo {
///     bar: 123,
///     baz: "hello".to_string(),
/// };
///
/// let foo_debug = format!("{foo:?}");
/// assert_eq!(foo_debug, "Foo { bar: bar123, baz: \"hello\" }");
/// ```
///
/// Field attributes take precedence over item attributes.
///
/// ## Enums
///
/// Enums are supported as well. Item attributes are apply to all
/// variants, and each variant is treated essentially as a struct.
///
/// ```rust
/// use debugify::Debugify;
///
/// #[derive(Debugify)]
/// #[debugify(field_name([biz, qux] = "foo{}"))]
/// enum Foo {
///     Bar {
///         biz: i32,
///         qux: String,
///     },
///     Baz {
///         biz: i32,
///         #[debugify("qux{}")]
///         qux: String,
///         quant: i64,
///     }
/// }
///
/// let foo_1 = Foo::Bar {
///     biz: 123,
///     qux: "hello".to_string(),
/// };
/// let foo_2 = Foo::Baz {
///     biz: 456,
///     qux: "world".to_string(),
///     quant: 789,
/// };
///
/// let foo_1_debug = format!("{foo_1:?}");
/// assert_eq!(foo_1_debug, "Bar { biz: foo123, qux: foohello }");
///
/// let foo_2_debug = format!("{foo_2:?}");
/// assert_eq!(foo_2_debug, "Baz { biz: foo456, qux: quxworld, quant: 789 }");
/// ```
///
/// ## Tuple and unit structs and variants
/// Tuple structs and variants also support field format attributes. Of course, these don't interact at all with the field name rules.
///
/// Unit structs and variants are formatted as normal.
///
/// ```rust
/// use debugify::Debugify;
///
/// #[derive(Debugify)]
/// #[debugify(field_type(String = "foo{}"))]
/// struct Foo(
///     #[debugify("number{}")]
///     i32,
///     String,
///     i32
/// );
///
/// let foo = Foo(64, "bar".into(), 128);
/// let foo_debug = format!("{foo:?}");
/// assert_eq!(foo_debug, "Foo(number64, foobar, 128)")
/// ```
#[proc_macro_derive(Debugify, attributes(debugify))]
pub fn debugify(tokens: TokenStream) -> TokenStream {
    let item = syn::parse_macro_input!(tokens as syn::Item);
    match item {
        syn::Item::Enum(item) => debugify_enum(item),
        syn::Item::Struct(item) => debugify_struct(item),
        _ => syn::Error::new_spanned(item, "expected enum or struct")
            .to_compile_error()
            .into(),
    }
}

fn debugify_enum(item: syn::ItemEnum) -> TokenStream {
    let item_ident = item.ident;

    // Parse item attributes into rule maps
    let rules = aggregate_format_rules(&item.attrs);

    // Early-return if there was an error while parsing the item attributes
    let (field_name_rules, field_type_rules) = match rules {
        Ok(rules) => rules,
        Err(e) => return e.to_compile_error().into(),
    };

    // Parse generic parameters
    let generics = &item.generics;
    let generic_parameters = generic_params(generics);

    // Parse variants
    let variants = item
        .variants
        .into_iter()
        .map(|variant| {
            fmt_impl_fragment(
                &variant.fields,
                &variant.ident,
                &field_name_rules,
                &field_type_rules,
                false,
            )
        })
        .collect::<syn::Result<Vec<_>>>();

    // Early-return if there was an error parsing the item attributes
    let variants = match variants {
        Ok(variants) => variants,
        Err(e) => return e.to_compile_error().into(),
    };

    // Generate the debug impl
    quote! {
        impl #generics std::fmt::Debug for #item_ident <#(#generic_parameters),*> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                match self {
                    #(#variants)*
                }
            }
        }
    }
    .into()
}

fn debugify_struct(item: syn::ItemStruct) -> TokenStream {
    let item_ident = item.ident;

    // Parse item attributes to rules
    let rules = aggregate_format_rules(&item.attrs);

    // Early-return if there was an error parsing the item attributes
    let (field_name_rules, field_type_rules) = match rules {
        Ok(rules) => rules,
        Err(e) => return e.to_compile_error().into(),
    };

    // Parse generic parameters
    let generics = &item.generics;
    let generic_parameters = generic_params(generics);

    // Generate the debug impl
    let fmt_impl = fmt_impl_fragment(
        &item.fields,
        &item_ident,
        &field_name_rules,
        &field_type_rules,
        true,
    );

    // Early-return if there was an error parsing the field attributes
    let fmt_impl = match fmt_impl {
        Ok(fmt_impl) => fmt_impl,
        Err(e) => return e.to_compile_error().into(),
    };

    quote! {
        impl #generics std::fmt::Debug for #item_ident <#(#generic_parameters),*> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                #fmt_impl
            }
        }
    }
    .into()
}

fn fmt_impl_fragment(
    fields: &Fields,
    item_ident: &syn::Ident,
    field_name_rules: &HashMap<syn::Ident, Format>,
    field_type_rules: &HashMap<syn::Type, Format>,
    is_struct: bool,
) -> syn::Result<proc_macro2::TokenStream> {
    let field_idents = fields.iter().enumerate().map(|(field_nb, field)| {
        field
            .ident
            .clone()
            .unwrap_or_else(|| quote::format_ident!("field_{field_nb}"))
    });
    let field_fragments = field_attributes(fields, field_name_rules, field_type_rules)?;
    Ok(match fields {
        syn::Fields::Named(_) => {
            let fragment = quote! {
                f.debug_struct(
                    stringify!(#item_ident)
                )
                #(#field_fragments)*
                .finish()
            };
            if is_struct {
                quote! {
                    let Self { #(#field_idents),* } = self;
                    #fragment
                }
            } else {
                quote! {
                    Self::#item_ident { #(#field_idents),* } => {
                        #fragment
                    }
                }
            }
        }
        syn::Fields::Unnamed(_) => {
            let fragment = quote! {
                f.debug_tuple(
                    stringify!(#item_ident)
                )
                #(#field_fragments)*
                .finish()
            };
            if is_struct {
                quote! {
                    let Self ( #(#field_idents),* ) = self;
                    #fragment
                }
            } else {
                quote! {
                    Self::#item_ident ( #(#field_idents),* ) => {
                        #fragment
                    }
                }
            }
        }
        syn::Fields::Unit => {
            let fragment = quote! {
                f.debug_struct(
                    stringify!(#item_ident)
                )
                .finish()
            };
            if is_struct {
                quote! {
                    #fragment
                }
            } else {
                quote! {
                    Self::#item_ident => {
                        #fragment
                    }
                }
            }
        }
    })
}

fn aggregate_format_rules(
    attrs: &[syn::Attribute],
) -> syn::Result<(HashMap<syn::Ident, Format>, HashMap<syn::Type, Format>)> {
    // Build maps out of attributes
    attrs.iter().try_fold(
        (
            HashMap::<syn::Ident, Format>::new(),
            HashMap::<syn::Type, Format>::new(),
        ),
        |(mut field_names, mut field_types), attr| {
            // If the attribute's path is not "debugify", don't aggregate it
            if !attr.path().is_ident("debugify") {
                return syn::Result::Ok((field_names, field_types));
            };

            // Parse the attribute's content and append the rules to the maps
            append_item_format_rules(attr, &mut field_names, &mut field_types)?;

            Ok((field_names, field_types))
        },
    )
}

/// Parses the content of an item attribute and appends the rules to the maps
fn append_item_format_rules(
    attr: &syn::Attribute,
    field_name: &mut HashMap<syn::Ident, Format>,
    field_type: &mut HashMap<syn::Type, Format>,
) -> syn::Result<()> {
    attr.parse_nested_meta(|meta| {
        let content;
        syn::parenthesized!(content in meta.input);
        if meta.path.is_ident("field_name") {
            insert_rules(content, field_name)?;
        } else if meta.path.is_ident("field_type") {
            insert_rules(content, field_type)?;
        } else {
            return Err(syn::Error::new_spanned(
                meta.path,
                "expected `field_name` or `field_type`",
            ));
        };
        Ok(())
    })
}

/// Parses a comma separated list of rules and inserts them into the map
fn insert_rules<T: syn::parse::Parse + std::hash::Hash + Eq>(
    content: syn::parse::ParseBuffer,
    rules: &mut HashMap<T, Format>,
) -> syn::Result<()> {
    let meta_items = content.parse_terminated(ItemAttributeMetaItem::<T>::parse, syn::Token![,])?;
    for meta_item in meta_items {
        match meta_item.values {
            ItemAttributeValues::Single(ty) => {
                rules.insert(ty.value, meta_item.format);
            }
            ItemAttributeValues::Multiple(tys) => {
                for ty in tys.values {
                    rules.insert(ty, meta_item.format.clone());
                }
            }
        }
    }
    Ok(())
}

/// Generates the generic parameters for the debug impl
fn generic_params(generics: &syn::Generics) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
    generics.params.iter().map(|param| match param {
        GenericParam::Lifetime(LifetimeParam { lifetime, .. }) => quote! {#lifetime},
        GenericParam::Type(TypeParam { ident, .. }) => quote! {#ident},
        GenericParam::Const(ConstParam { ident, .. }) => quote! {#ident},
    })
}

/// Generates the debug fields for the debug impl
fn field_attributes(
    fields: &syn::Fields,
    field_name: &HashMap<syn::Ident, Format>,
    field_type: &HashMap<syn::Type, Format>,
) -> Result<Vec<proc_macro2::TokenStream>, syn::Error> {
    fields
        .iter()
        .enumerate()
        .map(|(field_nb, field)| {
            let field_ident = &field.ident;
            // Get format from
            //     1. last attribute, or
            //     2. field name rule, or
            //     3. field type rule, or
            //     4. default format
            let format = field
                .attrs
                .iter()
                .rev()
                .find(|attr| attr.path().is_ident("debugify"))
                .map(|attr| attr.parse_args::<Format>())
                .transpose()?
                .or_else(|| {
                    field_ident
                        .as_ref()
                        .and_then(|field_ident| field_name.get(field_ident).cloned())
                })
                .or_else(|| field_type.get(&field.ty).cloned());

            Ok(debug_field(
                // Struct variants
                field_ident
                    .clone()
                    // Tuple variants
                    .unwrap_or_else(|| quote::format_ident!("field_{field_nb}")),
                format,
                field.ident.is_none(),
            ))
        })
        .collect::<syn::Result<Vec<_>>>()
}

/// Generates the debug field call for a field
fn debug_field<T: ToTokens>(
    field_ident: T,
    format: Option<Format>,
    tuple: bool,
) -> proc_macro2::TokenStream {
    let value = match format {
        Some(Format::Function(format)) => quote! {
            &std::format_args!("{}", #format(#field_ident))
        },
        Some(Format::String(format)) => quote! {
            &std::format_args!(#format, #field_ident)
        },
        None => quote! { #field_ident },
    };

    if tuple {
        quote! {
            .field(
                #value
            )
        }
    } else {
        quote! {
            .field(
                stringify!(#field_ident),
                #value
            )
        }
    }
}

#[derive(Clone)]
enum Format {
    String(syn::LitStr),
    Function(syn::Path),
}

impl syn::parse::Parse for Format {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(syn::LitStr) {
            input.parse::<syn::LitStr>().map(Format::String)
        } else if lookahead.peek(syn::Ident) {
            input.parse::<syn::Path>().map(Format::Function)
        } else {
            Err(lookahead.error())
        }
    }
}

struct ItemAttributeMetaItem<T: Parse> {
    values: ItemAttributeValues<T>,
    _eq: syn::token::Eq,
    format: Format,
}

impl<T: Parse> Parse for ItemAttributeMetaItem<T> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        Ok(Self {
            values: input.parse()?,
            _eq: input.parse()?,
            format: input.parse()?,
        })
    }
}

enum ItemAttributeValues<T: Parse> {
    Single(ItemAttributeIdentsSingle<T>),
    Multiple(ItemAttributeIdentsMultiple<T>),
}

impl<T: Parse> Parse for ItemAttributeValues<T> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(syn::Ident) {
            input
                .parse::<ItemAttributeIdentsSingle<T>>()
                .map(ItemAttributeValues::Single)
        } else if lookahead.peek(syn::token::Bracket) {
            input
                .parse::<ItemAttributeIdentsMultiple<T>>()
                .map(ItemAttributeValues::Multiple)
        } else {
            Err(lookahead.error())
        }
    }
}

struct ItemAttributeIdentsSingle<T: Parse> {
    value: T,
}

impl<T: Parse> Parse for ItemAttributeIdentsSingle<T> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        Ok(Self {
            value: input.parse()?,
        })
    }
}

struct ItemAttributeIdentsMultiple<T: Parse> {
    _bracket: syn::token::Bracket,
    values: syn::punctuated::Punctuated<T, syn::Token![,]>,
}

impl<T: Parse> Parse for ItemAttributeIdentsMultiple<T> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let content;
        Ok(Self {
            _bracket: syn::bracketed!(content in input),
            values: content.parse_terminated(T::parse, syn::Token![,])?,
        })
    }
}