https://github.com/captain-camel/display_tree
Simple, automatic, and customizable tree pretty-printing in Rust.
https://github.com/captain-camel/display_tree
formatter rust tree
Last synced: over 1 year ago
JSON representation
Simple, automatic, and customizable tree pretty-printing in Rust.
- Host: GitHub
- URL: https://github.com/captain-camel/display_tree
- Owner: captain-camel
- Created: 2022-12-19T21:10:28.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-01T04:30:43.000Z (over 3 years ago)
- Last Synced: 2025-03-12T14:48:52.944Z (over 1 year ago)
- Topics: formatter, rust, tree
- Language: Rust
- Homepage: https://crates.io/crates/display_tree
- Size: 50.8 KB
- Stars: 30
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `display_tree`
Simple, automatic, and customizable tree pretty-printing in Rust.

This crate provides tools to easily pretty-print data as a tree, including a trait that represents the ability to be printed as a tree, and a derive macro to automatically implement it for your types
See the [crate-level documentation](https://docs.rs/display_tree/*/display_tree) to get started.
## Examples
```rust
use display_tree::{AsTree, CharSet, DisplayTree, StyleBuilder};
// A tree representing a numerical expression.
#[derive(DisplayTree)]
enum Expr {
Int(i32),
BinOp {
#[node_label]
op: char,
#[tree]
left: Box,
#[tree]
right: Box,
},
UnaryOp {
#[node_label]
op: char,
#[tree]
arg: Box,
},
}
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!(
"{}",
AsTree::new(&expr)
.indentation(1)
.char_set(CharSet::DOUBLE_LINE)
),
concat!(
"+\n",
"╠═ -\n",
"║ ╚═ Int\n",
"║ ╚═ 2\n",
"╚═ Int\n",
" ╚═ 7",
),
);
```