Crates.io | debug2 |
lib.rs | debug2 |
version | 0.1.1 |
source | src |
created_at | 2021-07-16 16:30:30.383077 |
updated_at | 2022-02-23 21:49:11.841581 |
description | Space Efficient Pretty Printer |
homepage | |
repository | https://github.com/aDotInTheVoid/debug2/ |
max_upload_size | |
id | 423703 |
size | 83,426 |
debug2
is a pretty printing crate based on std::fmt
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![
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
];
let one_line = format!("{:?}", complex_structure);
assert_eq!(one_line, "[[Some(1), Some(2), Some(3), None], [Some(2), None], [Some(4), Some(7)], [Some(1), Some(2), Some(3), None]]");
let many_lines = format!("{:#?}", complex_structure);
assert_eq!(many_lines, "[
[
Some(
1,
),
Some(
2,
),
Some(
3,
),
None,
],
[
Some(
2,
),
None,
],
[
Some(
4,
),
Some(
7,
),
],
[
Some(
1,
),
Some(
2,
),
Some(
3,
),
None,
],
]")
debug2
aims to be a third alternative, that gets this correct.
use debug2::pprint;
let complex_structure = vec![
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
];
assert_eq!(
pprint(complex_structure),
"\
[
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
]"
);
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
std::fmt::Debug
, but not this typestd::fmt::Debug
works everywhere. This one
is kind of basic, and will probably not work everywhere it should.std::fmt
, where much of the code comes frompprint
from python
, which showed that this sort of thing is doable and great.ojg
, whose pretty
module is the basis for this whole thing.