tree-display
Display Rust data structures as syntax-highlighted trees in the terminal with a single derive macro. Features full color theming, context-aware value mapping, and intelligent formatting for collections. Perfect for debugging, compiler diagnostics, and AST inspection.
Features
- Simple derive macro -
#[derive(TreeDisplay)]for automatic tree generation - Field attributes - Control rendering of individual fields:
#[tree(map)]- Apply a custom mapper fromContext#[tree(ignore)]- Exclude a field from the tree#[tree(label = "...")]- Override the field's display label#[tree(unlabeled)]- Display the field without a label
- Custom mapping - Transform values with
Contextmappers - Theming - Predefined themes (VS Code Dark+, Solarized, etc.)
- Color support - Optional ANSI color highlighting
- Line styles - ASCII or Unicode box-drawing characters
- Formatting - Labels, alignment, and custom content types
- Comprehensive support - Works with most standard library types
Quick Start
Add to your Cargo.toml:
[]
= "1.0"
Then derive TreeDisplay for your types:
use ;
let person = Person ;
println!;
Output:
Person
├─ name: "Alice"
├─ age: 30
└─ children: Vec
├─ len: 2
├─ [0]: Person
│ ├─ name: "Bob"
│ └─ age: 5
└─ [1]: Person
├─ name: "Charlie"
└─ age: 3
Field Attributes
#[tree(map)]
Apply a custom mapper from the context. Requires a Context with a registered mapper for the field's type.
use ;
let context = new.map;
let person = Person ;
println!;
Output:
Person
└─ name: 5
#[tree(ignore)]
Exclude a field from the tree entirely.
let person = Person ;
println!;
Output:
Person
└─ name: "Bob"
#[tree(label = "...")]
Override the field's display label.
Output:
Person
└─ full_name: "Alice"
#[tree(unlabeled)]
Display the field without a label.
Output:
Person
└─ "Alice"
Theming
tree-display comes with several predefined themes:
use ;
let theme = default
.colors
.lines
.align_to_values;
let output = of.theme.format;
Available themes:
Colors::VSCODE_DARK_PLUSColors::VSCODE_LIGHT_PLUSColors::SOLARIZED_DARKColors::SOLARIZED_LIGHTColors::DRACULAColors::MONOKAIColors::NORDColors::GITHUB_DARKColors::GITHUB_LIGHTColors::NONE(no colors)
Available line styles:
Graphics::ASCII- Pure ASCII (terminal compatible)Graphics::LIGHT- Unicode box-drawingGraphics::LIGHT_ROUNDED- Rounded cornersGraphics::LIGHT_DOTTED- Dotted linesGraphics::DOUBLE- Double linesGraphics::HEAVY- Heavy linesGraphics::BLANK- No lines
Custom Mappers
Use Context to transform values before display:
use ;
let context = new
.map
.map;
let data = Data ;
println!;
Supported Types
tree-display automatically implements TreeDisplay for many standard library types:
- Primitives (bool, char, integers, floats)
- Strings and
&str - Tuples (up to 12 elements)
- Arrays, slices,
Vec,VecDeque,LinkedList HashMap,BTreeMap,HashSet,BTreeSet,BinaryHeapOption,Result- Smart pointers (
Box,Rc,Arc,RefCell,Mutex,RwLock) - Ranges (
Range,RangeInclusive,RangeFrom,RangeTo,RangeFull) Duration,Instant,SystemTimePath,PathBuf,OsString,OsStrPhantomData
Feature Flags
color- Enables ANSI color support (enabled by default)chumsky- Adds support forchumsky::span::SimpleSpan
To disable default features:
[]
= "1.0"
= false
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.