debugify

Crates.iodebugify
lib.rsdebugify
version0.2.0
sourcesrc
created_at2023-12-14 04:14:27.415392
updated_at2023-12-14 19:52:28.088755
descriptionDerive macro for `std::fmt::Debug` focused on reducing boilerplate.Supports both format strings and formatter functions.
homepage
repositoryhttps://github.com/LouisGariepy/debugify
max_upload_size
id1069097
size23,898
__dil__ (LouisGariepy)

documentation

README

Crates.io version docs.rs Crates.io version License

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.

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.

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.

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.

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.

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)")
Commit count: 8

cargo fmt