Debug2
debug2 is a pretty printing crate based on std::fmt
Why not just use Debug
The Debug trait is good, but the problem is it is not very good at nested stuctures.
Either you use {:?} and get a line that is too long, or too many lines with not enough
information on them.
let complex_structure = vec!;
let one_line = format!;
assert_eq!;
let many_lines = format!;
assert_eq!
debug2 aims to be a third alternative, that gets this correct.
use pprint;
let complex_structure = vec!;
assert_eq!;
debug2 provides a debug2::Debug trait, which can be derived on your types, and is implemented
for common types in std.
Once your types implement debug2::Debug, you can use debug2::pprint to convert them to a string.
You can also manually implement Debug, using a subset of the API in std::fmt::Formatter
Limitations
- Speed: While doing this will always mean extra work, this crate is paticularly inefficient.
- Prevalence: Almost every type implements
std::fmt::Debug, but not this type - The derive isn't great: The derive macro for
std::fmt::Debugworks everywhere. This one is kind of basic, and will probably not work everywhere it should.
Prior art
std::fmt, where much of the code comes frompprintfrom python , which showed that this sort of thing is doable and great.ojg, whoseprettymodule is the basis for this whole thing.