| Crates.io | crud-pretty-struct |
| lib.rs | crud-pretty-struct |
| version | 0.1.7 |
| created_at | 2023-07-20 07:22:44.419982+00 |
| updated_at | 2025-04-05 08:41:58.69939+00 |
| 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 | 141,428 |
Displays (json) structures and enums 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::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
}
// Instanciate a `var` of type `Foo`
println!("{}",var.pretty(true,None,None).expect("Can prettify var"));
is_prettythe 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
}
labelcustom label for this field
#[derive(PrettyPrint)]
struct Foo {
#[pretty(label="☀️ my field")]
field: u32
}
colorcustom color for the value of this field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
label_colorcustom color for the label of dthis field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(label_color="red")]
field: u32
}
skipskip the field. It won't be display.
#[derive(PrettyPrint)]
struct Foo {
#[pretty(skip)]
field: u32
}
skip_noneskip 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>
}
profilethe field is displayed only when this field profiles matched the profile declare when calling the pretty function.
#[derive(PrettyPrint)]
struct Foo {
#[pretty(profiles = "a")]
field1: u32,
#[pretty(profiles = "a,b")]
field2: bool,
}
let foo = Foo{field1:0, field2:false};
foo.pretty(false, None, Some("b")).unwrap(); // print only `field2`
formatterCustom 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
}
Limitations on enums:
colorcustom color for this variant avaiable colors are [Color].
#[derive(PrettyPrint)]
enum Foo {
#[pretty(color="red")]
Variant
}
labelcustom label for this variant
#[derive(PrettyPrint)]
enum Foo {
#[pretty(label="☀️ my field")]
Variant
}