Crates.io | crud-pretty-struct |
lib.rs | crud-pretty-struct |
version | 0.1.6 |
source | src |
created_at | 2023-07-20 07:22:44.419982 |
updated_at | 2024-03-19 06:32:23.230924 |
description | Pretty display for struct |
homepage | https://github.com/djedi23/crud.rs |
repository | https://github.com/djedi23/crud.rs |
max_upload_size | |
id | 921110 |
size | 82,926 |
Displays (json) structures in a pretty way.
This crate is linked to the crud library. If I have time and motivation to generalize it, it can be an indenpendant 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
the nested struct implements PrettyPrint
and should be printed using it.
use crud_pretty_struct_derive::PrettyPrint;
#[derive(PrettyPrint)]
struct OtherPrettyStruct {}
#[derive(PrettyPrint)]
struct Foo {
#[pretty(is_pretty)]
field: OtherPrettyStruct
}
label
custom label for this field
#[derive(PrettyPrint)]
struct Foo {
#[pretty(label="☀️ my field")]
field: u32
}
color
custom color for this field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
label_color
custom color for the label of this field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
skip
skip the field. It won't be display.
#[derive(PrettyPrint)]
struct Foo {
#[pretty(skip)]
field: u32
}
skip_none
skip the field if the value is None
. The type of the field should be an Option<T>
.
#[derive(PrettyPrint)]
struct Foo {
#[pretty(skip_none)]
field: Option<u32>
}
formatter
Custom value formatter for this field.
Some [formatters] are shipped in this crate.
#[derive(PrettyPrint)]
struct Foo {
#[pretty(formatter=crud_pretty_struct::formatters::bool_check_formatter)]
field: bool
}
Formatters should follow this signature:
type Formatter = dyn Fn(/*value:*/ &dyn ToString, /*colored:*/ bool) -> miette::Result<(String, bool)>;
Parameters:
value
: the value to formatcolored
: when true
the formatted value can be coloredResult:
true
if the value have colors. returns false
if the value don't have colors then default color will be applied.#[derive(PrettyPrint)]
struct Foo {
#[pretty(formatter=|x, _| Ok((format!("{} kg", x.to_string()), false)))]
field: f32
}