11 个版本
0.4.0 | 2023年5月13日 |
---|---|
0.3.2 | 2022年10月16日 |
0.3.1 | 2022年6月25日 |
0.3.0 | 2022年4月23日 |
0.1.3 | 2022年4月17日 |
#20 in #dbg
用于 dbg-pls
16KB
268 行
dbg-pls
为Rust提供一个类似于Debug trait的特质,能够输出格式正确的代码
展示
以下代码
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)]
derivepretty
- 启用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-style宏)
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" }
依赖关系
~280–730KB
~17K SLoC