3个版本
0.1.6 | 2024年3月19日 |
---|---|
0.1.5 | 2023年9月24日 |
0.1.4 | 2023年7月20日 |
#80 in 值格式化
69KB
2K SLoC
美观结构体
以美观的方式显示(json)结构。
此crate与crud库相关联。如果有时间和动力将其通用化,它可以成为一个独立的crate。
示例
use crud_pretty_struct_derive::PrettyPrint;
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="green")]
a: u32,
#[pretty(skip_none)]
b: Option<String>,
#[pretty(formatter=crud_pretty_struct::formatters::bool_check_formatter)]
c: bool,
#[pretty(is_pretty)]
d: OtherPrettyStruct
}
字段选项
is_pretty
嵌套结构体实现了PrettyPrint
,应该使用它进行打印。
use crud_pretty_struct_derive::PrettyPrint;
#[derive(PrettyPrint)]
struct OtherPrettyStruct {}
#[derive(PrettyPrint)]
struct Foo {
#[pretty(is_pretty)]
field: OtherPrettyStruct
}
label
此字段的自定义标签
#[derive(PrettyPrint)]
struct Foo {
#[pretty(label="☀️ my field")]
field: u32
}
color
此字段的自定义颜色。可用的颜色有[Color]。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
label_color
此字段标签的自定义颜色。可用的颜色有[Color]。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
skip
跳过字段。它不会显示。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(skip)]
field: u32
}
skip_none
如果值是None
,则跳过字段。字段的类型应该是Option<T>
。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(skip_none)]
field: Option<u32>
}
formatter
此字段的自定义值格式化器。
此crate中包含一些[格式化器]。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(formatter=crud_pretty_struct::formatters::bool_check_formatter)]
field: bool
}
格式化器应遵循此签名
type Formatter = dyn Fn(/*value:*/ &dyn ToString, /*colored:*/ bool) -> miette::Result<(String, bool)>;
参数
value
:要格式化的值colored
:当true
时,格式化的值可以是彩色的
结果
- String:格式化的值
- bool:如果值有颜色,则返回
true
。如果没有颜色,则返回false
,然后应用默认颜色。
#[derive(PrettyPrint)]
struct Foo {
#[pretty(formatter=|x, _| Ok((format!("{} kg", x.to_string()), false)))]
field: f32
}
依赖项
~4–16MB
~192K SLoC