Crates.io | deboog |
lib.rs | deboog |
version | 0.2.0 |
source | src |
created_at | 2023-06-25 23:52:36.466921 |
updated_at | 2023-07-25 03:49:17.962441 |
description | Derive macro for extended debug formatting |
homepage | |
repository | https://github.com/unikmhz/deboog |
max_upload_size | |
id | 899878 |
size | 39,156 |
Derive macro for extended debug formatting.
Currently allows skipping fields and masking field values using various strategies, with more to follow.
Skip serializing a field:
use deboog::Deboog;
#[derive(Deboog)]
struct Data {
shown: i32,
#[deboog(skip)]
skipped: i32,
}
assert_eq!(
format!("{:?}", Data { shown: 123, skipped: 234 }),
r#"Data { shown: 123 }"#
);
Also works with tuple structs:
use deboog::Deboog;
#[derive(Deboog)]
struct Data(i32, #[deboog(skip)] i32);
assert_eq!(
format!("{:?}", Data(123, 234)),
r#"Data(123)"#
);
And in enums too:
use deboog::Deboog;
#[derive(Deboog)]
enum Data {
One,
Two {
shown: i32,
#[deboog(skip)]
skipped: i32,
},
}
assert_eq!(
format!("{:?}", Data::Two { shown: 123, skipped: 234 }),
r#"Two { shown: 123 }"#
);
Mask a field:
use deboog::Deboog;
#[derive(Deboog)]
struct Data {
unmasked: i32,
#[deboog(mask = "all")]
masked: i32,
}
assert_eq!(
format!("{:?}", Data { unmasked: 123, masked: 23456 }),
r#"Data { unmasked: 123, masked: ***** }"#
);
Mask a credit card number or a similar value:
use deboog::Deboog;
#[derive(Deboog)]
struct Data {
unmasked: &'static str,
#[deboog(mask = "pan")]
masked: &'static str,
}
assert_eq!(
format!("{:?}", Data { unmasked: "1111222233334444", masked: "1111222233334444" }),
r#"Data { unmasked: "1111222233334444", masked: "111122******4444" }"#
);
In case you need to hide real field length:
use deboog::Deboog;
#[derive(Deboog)]
struct Data {
unmasked: i32,
#[deboog(mask = "hidden")]
masked: i32,
}
assert_eq!(
format!("{:?}", Data { unmasked: 123, masked: 23456 }),
r#"Data { unmasked: 123, masked: *** }"#
);
Support for masking for custom field types can be implemented using [field::DeboogField
] trait:
use deboog::{Deboog, DeboogField, MaskType};
#[derive(Deboog)]
struct What;
impl DeboogField for What {
fn fmt_masked(&self, f: &mut std::fmt::Formatter<'_>, _mask_type: MaskType) -> std::fmt::Result {
write!(f, "WHAT?")
}
}
#[derive(Deboog)]
struct Data {
unmasked: What,
#[deboog(mask = "all")]
masked: What,
}
assert_eq!(
format!("{:?}", Data { unmasked: What, masked: What }),
r#"Data { unmasked: What, masked: WHAT? }"#
);
See change log.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.