25个版本

0.4.3 2023年8月21日
0.4.1 2023年5月26日
0.3.5 2023年2月12日
0.3.4 2022年10月16日
0.3.3 2022年6月25日

#69过程宏

Download history 27/week @ 2024-04-02 1/week @ 2024-04-23 114/week @ 2024-07-02

每月下载量 114

MIT 协议

205KB
4K SLoC

dbg-pls

crate docs

为Rust提供类似Debug的特质,可以输出格式正确的代码

展示

以下代码示例:

let code = r#"
    [
        "Hello, World! I am a long string",
        420,
        "Wait, you can't mix and match types in arrays, is this python?",
        69,
        "Nice."
    ]
"#;
let expr: syn::Expr = syn::parse_str(code).unwrap();
println!("{expr:?}");

这将输出:

Array(ExprArray { attrs: [], bracket_token: Bracket, elems: [Lit(ExprLit { attrs: [], lit: Str(LitStr { token: "Hello, World! I am a long string" }) }), Comma, Lit(ExprLit { attrs: [], lit: Int(LitInt { token: 420 }) }), Comma, Lit(ExprLit { attrs: [], lit: Str(LitStr { token: "Wait, you can't mix and match types in arrays, is this python?" }) }), Comma, Lit(ExprLit { attrs: [], lit: Int(LitInt { token: 69 }) }), Comma, Lit(ExprLit { attrs: [], lit: Str(LitStr { token: "Nice." }) })] })

内容过于密集,难以阅读。

如果我们将println改为使用另一种打印方式(:#?),则得到:

Array(
    ExprArray {
        attrs: [],
        bracket_token: Bracket,
        elems: [
            Lit(
                ExprLit {
                    attrs: [],
                    lit: Str(
                        LitStr {
                            token: "Hello, World! I am a long string",
                        },
                    ),
                },
            ),
            Comma,
            Lit(
                ExprLit {
                    attrs: [],
                    lit: Int(
                        LitInt {
                            token: 420,
                        },
                    ),
                },
            ),
            Comma,
            Lit(
                ExprLit {
                    attrs: [],
                    lit: Str(
                        LitStr {
                            token: "Wait, you can't mix and match types in arrays, is this python?",
                        },
                    ),
                },
            ),
            Comma,
            Lit(
                ExprLit {
                    attrs: [],
                    lit: Int(
                        LitInt {
                            token: 69,
                        },
                    ),
                },
            ),
            Comma,
            Lit(
                ExprLit {
                    attrs: [],
                    lit: Str(
                        LitStr {
                            token: "Nice.",
                        },
                    ),
                },
            ),
        ],
    },
)

内容过于分散,不够自然。

这时,dbg_pls就派上用场了。将println替换为:

println!("{}", dbg_pls::color(&expr));

然后你得到:

在库中应用

cargo add dbg-pls

添加到你的类型中

#[derive(dbg_pls::DebugPls)]

在应用程序中使用

cargo add dbg-pls +pretty

并使用pretty打印,例如

println!("{}", dbg_pls::pretty(&value));

功能

  • derive - 启用#[derive(DebugPls)]派生
  • pretty - 启用pretty函数以进行漂亮打印
  • colors - 启用color函数以进行语法高亮打印

示例

use dbg_pls::{pretty, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let mut val = [Demo { foo: 5, bar: "hello" }; 10];
val[6].bar = "Hello, world! I am a very long string";

println!("{}", pretty(&val));

输出

[
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo {
        foo: 5,
        bar: "Hello, world! I am a very long string",
    },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
]

示例(高亮显示)

use dbg_pls::{color, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let mut val = [Demo { foo: 5, bar: "hello" }; 10];
val[6].bar = "Hello, world! I am a very long string";

println!("{}", color(&val));

输出

示例(dbg样式宏)

use dbg_pls::{color, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let foo = 5;
let bar = "Hello, World! This is the color macro";
let _ = color!(Demo { foo, bar });

输出

use dbg_pls::{pretty, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let foo = 5;
let bar = "hello";
let _ = pretty!(Demo { foo, bar });

输出

[src/lib.rs:558] Demo { foo, bar } => Demo { foo: 5, bar: "hello" }

依赖项

~0.3–8.5MB
~66K SLoC