2个不稳定版本

0.2.0 2023年7月25日
0.1.1 2023年6月25日

#373 in 值格式化

Download history 13/week @ 2024-04-27 7/week @ 2024-05-04 37/week @ 2024-05-11 30/week @ 2024-05-18 30/week @ 2024-05-25 77/week @ 2024-06-01 25/week @ 2024-06-08 49/week @ 2024-06-15 1/week @ 2024-06-22 89/week @ 2024-06-29 12/week @ 2024-07-06 83/week @ 2024-07-13 83/week @ 2024-07-20 187/week @ 2024-07-27

396 次每月下载

MIT/Apache

16KB
201 代码行

deboog

crates.io build status license codecov documentation

扩展调试格式化宏。

目前允许使用各种策略跳过字段和掩码字段值,更多功能即将推出。

基本用法

跳过序列化字段

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

也适用于元组结构体

use deboog::Deboog;

#[derive(Deboog)]
struct Data(i32, #[deboog(skip)] i32);

assert_eq!(
    format!("{:?}", Data(123, 234)),
    r#"Data(123)"#
);

在枚举中也是如此

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

掩码

掩码一个字段

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

掩码信用卡号码或类似值

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

如果您需要隐藏实际字段长度

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

类型支持

可以使用 field::DeboogField 特性实现自定义字段类型的掩码支持

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

版本历史

变更日志.

许可证

根据您的选择,在以下许可证下获得许可: Apache许可证第2版MIT许可证

除非您明确表示,否则根据Apache-2.0许可证定义,您提交的任何有意提交的工作将根据上述许可双重许可,不附加任何额外条款或条件。

依赖项

~0.6–1MB
~24K SLoC