4 releases
| 0.1.7 | Apr 5, 2025 |
|---|---|
| 0.1.6 | Mar 19, 2024 |
| 0.1.5 | Sep 24, 2023 |
| 0.1.4 | Jul 20, 2023 |
#92 in Value formatting
199 downloads per month
Used in 3 crates
(2 directly)
94KB
2.5K
SLoC
Pretty Struct
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.
Example
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"));
Field Options
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 the value of this field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(color="red")]
field: u32
}
label_color
custom color for the label of dthis field. The avaiable colors are [Color].
#[derive(PrettyPrint)]
struct Foo {
#[pretty(label_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>
}
profile
the 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`
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: whentruethe formatted value can be colored
Result:
- String: the formatted value
- bool: returns
trueif the value have colors. returnsfalseif 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
}
Enum Option
Limitations on enums:
- unit variants are supported
- tuple variants with only 1 argument are supported
color
custom color for this variant avaiable colors are [Color].
#[derive(PrettyPrint)]
enum Foo {
#[pretty(color="red")]
Variant
}
label
custom label for this variant
#[derive(PrettyPrint)]
enum Foo {
#[pretty(label="☀️ my field")]
Variant
}
Dependencies
~7–13MB
~272K SLoC