#derive-debug #macro-derive #derive #traits #customizable #enums

macro derive-debug-plus

Customizable derive macro for Debug trait

5 releases (3 breaking)

0.5.0 Feb 21, 2024
0.4.1 Feb 20, 2024
0.4.0 Feb 20, 2024
0.3.0 Feb 20, 2024
0.2.0 Feb 20, 2024

#417 in Debugging

Download history 63/week @ 2024-07-23 57/week @ 2024-07-30 57/week @ 2024-08-06 228/week @ 2024-08-13 213/week @ 2024-08-20 312/week @ 2024-08-27 373/week @ 2024-09-03 108/week @ 2024-09-10 52/week @ 2024-09-17 58/week @ 2024-09-24 105/week @ 2024-10-01 284/week @ 2024-10-08 236/week @ 2024-10-15 454/week @ 2024-10-22 450/week @ 2024-10-29 375/week @ 2024-11-05

1,531 downloads per month

MIT license

24KB
469 lines

derive-debug

This crate implements a more customizable version of #[derive(Debug)].

This is a fork of derive-debug

The difference

  • alias allow expr
  • expr option
  • sort
  • flat option

Usage

The usage is very similar to #[derive(Debug)] with a few extra customization options.

Deriving a struct

use derive_debug_plus::Dbg;

#[derive(Dbg)]
struct Foo {
    field_a: u32,
    #[dbg(placeholder = "...")]
    field_b: Vec<u32>, // will be printed as "field_b: ..."
    #[dbg(skip)]
    field_c: bool, // will be left out
    #[dbg(alias = "my_string")]
    field_d: u32, // will be printed as "my_string: 42"
    #[dbg(fmt = "{:#06X}")]
    field_e: u32, // will be printed with the specified format
}

Deriving an enum

use derive_debug_plus::Dbg;

#[derive(Dbg)]
enum Foo {
    VariantA(u32, u32, u32),
    #[dbg(skip)]
    VariantB{a: u32, b: bool}, // Will be printed as "VariantB"

    // same options available as for struct fields
    VariantC{a: u32, #[dbg(placeholder = "...")] b: bool}
}

Detailed options

Field Options

  • #[dbg(skip)] completely omits a field in the output
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(skip)]
        field_b: u32,
    }

    // Outputs: Foo { field_a: true }
  • #[dbg(placeholder = "xyz")] will print xyz instead of the actual contents of a field
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(placeholder = "...")]
        field_b: u32,
    }

    // Outputs: Foo { field_a: true, field_b: ... }
  • #[dbg(alias = "some_alias")] will print some_alias as field name instead of the real name
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(alias = "not_field_b")]
        field_b: u32,
        #[dbg(alias = &format!("expr_{}", "b"))]
        field_c: u32,
    }

    // Outputs: Foo { field_a: true, not_field_b: 42, expr_b: 137 }
  • #[dbg(fmt = "{:#06X}")] will print the field with the specified format
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(fmt = "{:#06X}")]
        field_b: u32,
    }

    // Outputs: Foo { field_a: true, field_b: 0x002A }
  • #[dbg(formatter = "my_func")] will print the field using the specified function.
    The function has to return a type that can be formatted using "{}"
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo(u32, #[dbg(formatter = fmt_not_zero)] u32);

    fn fmt_not_zero(v: &u32) -> &'static str {
        if *v == 0 {
            "0"
        } else {
            "not 0"
        }
    }

    // Outputs: Foo(42, not 0)
  • #[dbg(expr = &"123")] will pass the expression directly into
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(expr = &"123")]
        field_b: u32,
    }

    // Outputs: Foo { field_a: true, field_b: "123" }
  • #[dbg(sort = -1)]
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        field_a: bool,
        #[dbg(sort = -1)]
        field_b: u32,
    }

    // Outputs: Foo { field_b: 123, field_a: true }
  • #[dbg(flat_option)]
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    struct Foo {
        #[dbg(flat_option)]
        field_a: Option<u32>, // None
        #[dbg(flat_option)]
        field_b: Option<u32>, // Some
    }

    // Outputs: Foo { field_b: 123 }

Enum Variant Options

  • #[dbg(skip)] only prints the name of the variant and omits its contents
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    enum Foo {
        #[dbg(skip)]
        SomeVariant{a: bool, b: u32},
    }

    // Outputs: SomeVariant
  • #[dbg(alias = "some_alias")] will use some_alias as variant name instead of the real name
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    enum Foo {
        #[dbg(alias = "NotSomeVariant")]
        SomeVariant{a: bool, b: u32},
    }

    // Outputs: NotSomeVariant { a: true, b: 42 }

struct Options

  • #[dbg(alias = "MyAlias")] will use MyAlias as struct name instead of the real name
    use derive_debug_plus::Dbg;

    #[derive(Dbg)]
    #[dbg(alias = "NotFoo")]
    struct Foo {
        field_a: bool,
        field_b: u32,
    }

    // Outputs: NotFoo { field_a: true, not_field_b: 42 }

Dependencies

~250–700KB
~17K SLoC