format-attr
document: https://docs.rs/format-attr
A Rust proc-macro crate that provides custom derive macros for implementing Display and Debug traits with custom format strings.
Features
DisplayAttr- Derivestd::fmt::Displaywith a custom format stringDebugAttr- Derivestd::fmt::Debugwith a custom format stringDisplayAsDebug- ImplementDisplayby delegating to the existingDebugimplementation- Support for separate format strings via
#[fmt_display(...)]and#[fmt_debug(...)] - Fallback to shared
#[fmt(...)]attribute
Usage
Add this to your Cargo.toml:
[]
= "0"
Basic Example
use ;
// `self.` prefix is optional for simple field access
let p = Point ;
assert_eq!;
assert_eq!;
Note: You can omit the
self.prefix for simple field names. If you need to call methods or use complex expressions, you can still useself.field.method()syntax.
Inline Field Names
You can also use field names directly inside the format string:
use ;
// {x} and {y} are automatically replaced with field values
let p = Point ;
assert_eq!;
Format specifiers are also supported:
use DisplayAttr;
let p = Person ;
assert_eq!;
Separate Format Strings
Use #[fmt_display(...)] and #[fmt_debug(...)] for different output:
use ;
let u = User ;
assert_eq!;
assert_eq!;
DisplayAsDebug
When you want Display to use the same output as Debug:
use DisplayAsDebug;
;
let v = Value;
assert_eq!;
assert_eq!;
Attribute Priority
| Derive Macro | Priority 1 | Priority 2 (fallback) |
|---|---|---|
DisplayAttr |
#[fmt_display(...)] |
#[fmt(...)] |
DebugAttr |
#[fmt_debug(...)] |
#[fmt(...)] |
License
MIT