Expand description
display_tree
provides simple, automatic, and customizable tree
pretty-printing.
This crate provies the DisplayTree
trait and a macro to derive it for
struct
s and enum
s. The derived implementation can be heavily customized
using helper attributes discussed in the DisplayTree
documentation It also provides AsTree
and a set of macros
mirroring standard library counterparts for displaying or formatting tree
types. The way a tree is formatted can be customized with the Style
type, or builder methods on AsTree
, using the StyleBuilder
trait.
See the DisplayTree
documentation to learn how to make a
type printable as a tree, or AsTree
or any of the macros provided by
display_tree
for information on displaying or formatting your new tree
type.
§Examples
use display_tree::{format_tree, CharSet, DisplayTree, Style, StyleBuilder};
// A tree representing a numerical expression.
#[derive(DisplayTree)]
enum Expr {
Int(i32),
BinOp {
#[node_label]
op: char,
#[tree]
left: Box<Self>,
#[tree]
right: Box<Self>,
},
UnaryOp {
#[node_label]
op: char,
#[tree]
arg: Box<Self>,
},
}
let expr: Expr = Expr::BinOp {
op: '+',
left: Box::new(Expr::UnaryOp {
op: '-',
arg: Box::new(Expr::Int(2)),
}),
right: Box::new(Expr::Int(7)),
};
assert_eq!(
format_tree!(
expr,
Style::default()
.indentation(1)
.char_set(CharSet::DOUBLE_LINE)
),
concat!(
"+\n",
"╠═ -\n",
"║ ╚═ Int\n",
"║ ╚═ 2\n",
"╚═ Int\n",
" ╚═ 7",
),
);
Modules§
- to_
display_ tree_ ref - A module containing the
ToDisplayTreeRef
trait and implementations forstd
types.
Macros§
- format_
tree - Creates a
String
from a type that implementsDisplayTree
, formatting it as a tree. - print_
tree - Prints a type that implements
DisplayTree
to the standard output as a tree. - println_
tree - Prints a type that implements
DisplayTree
to the standard output as a tree, with a newline. - write_
tree - Writes a type that implements
DisplayTree
to a buffer as a tree. - writeln_
tree - Writes a type that implements
DisplayTree
to a buffer as a tree, with a newline.
Structs§
- AsTree
- A helper struct for formatting a type that implements
DisplayTree
. - CharSet
- A set of
char
s used for formatting a type that implementsDisplayTree
. - Style
- A type that describes the way a type that implements
DisplayTree
should be formatted. - Text
Style - A type that described how text will be rendered.
Enums§
- Color
- An ANSI color that a tree can be styled with.
Traits§
- Display
Tree - A type that can be pretty-printed as a tree with a specified style.
- Style
Builder - A trait that provides builder methods for constructing an instance of
Style
.
Derive Macros§
- Display
Tree - Derive marco for the
DisplayTree
trait.