derive-masked is a procedural macro crate for deriving Display and Debug implementations that mask sensitive fields, ensuring secure and controlled output of struct data.
- Derive
DisplayMaskedfor maskedDisplaytrait implementation. - Derive
DebugMaskedfor maskedDebugtrait implementation. - Mask sensitive fields with the
#[masked]attribute.
You can derive both DisplayMasked and DebugMasked if you want.
Add to Cargo.toml:
cargo add derive-maskeduse derive_masked::{DebugMasked, DisplayMasked};
#[derive(DebugMasked, DisplayMasked)]
struct User {
name: String,
#[masked]
password: String,
}
fn main() {
let user = User {
name: "Alice".to_string(),
password: "super_secret".to_string(),
};
// Uses DisplayMasked. Output: User { name: Alice, password: ***** }
println!("{}", user);
// Uses DebugMasked. Output: User { name: "Alice", password: ***** }
println!("{:?}", user);
// Uses DebugMasked with pretty print formatter. Output:
// User {
// name: "Alice",
// password: *****
// }
println!("{:#?}", user);
}For more examples, see the examples directory.
For support, file an issue on GitHub.
Licensed under either MIT or Apache 2.0, at your option.