2 unstable releases

0.2.0 Jul 25, 2023
0.1.1 Jun 25, 2023

#854 in Debugging

42 downloads per month

MIT/Apache

16KB
201 lines

deboog

crates.io build status license codecov documentation

Derive macro for extended debug formatting.

Currently allows skipping fields and masking field values using various strategies, with more to follow.

Basic usage

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 }"#
);

Masking

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: *** }"#
);

Type support

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? }"#
);

Version history

See change log.

License

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.

Dependencies

~0.6–1MB
~23K SLoC