Crates.io | debugify |
lib.rs | debugify |
version | 0.2.0 |
source | src |
created_at | 2023-12-14 04:14:27.415392 |
updated_at | 2023-12-14 19:52:28.088755 |
description | Derive macro for `std::fmt::Debug` focused on reducing boilerplate.Supports both format strings and formatter functions. |
homepage | |
repository | https://github.com/LouisGariepy/debugify |
max_upload_size | |
id | 1069097 |
size | 23,898 |
Derive macro for std::fmt::Debug
focused on reducing boilerplate. Supports both format strings and formatter functions.
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
.
In case of a conflict between attributes, the order of precedence is
If no format is specified, the default format is used.
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 }");
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 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 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)")