treelog 0.0.5

A highly customizable, optimized, and modular tree rendering library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
<div align="center">

![TreeLog Banner](images/banner.png)

[![CI](https://github.com/muijf/treelog/workflows/CI/badge.svg)](https://github.com/muijf/treelog/actions)
[![crates.io](https://img.shields.io/crates/v/treelog.svg)](https://crates.io/crates/treelog)
[![docs.rs](https://docs.rs/treelog/badge.svg)](https://docs.rs/treelog)
[![license](https://img.shields.io/crates/l/treelog.svg)](https://github.com/muijf/treelog/blob/main/LICENSE)

**A customizable tree rendering library and CLI for Rust**

*Provides low-level and high-level APIs for rendering hierarchical data structures, similar to `tree`, `npm ls`, or `cargo tree`. Also includes a command-line utility for quick tree visualization.*

[Installation](#installation) • [Quick Start](#quick-start) • [CLI Usage](#cli-usage) • [Documentation](https://docs.rs/treelog) • [Examples](#examples)

</div>

---

<details>
<summary><b>Features</b> - Overview of all available features</summary>

### Core Features
- **Multiple Styles** - Unicode, ASCII, Box drawing, and custom styles
- **Macro DSL** - Declarative syntax for easy tree construction
- **Builder API** - Fluent interface for easy tree construction
- **Iterator Support** - Stream large trees without materializing
- **Custom Formatters** - Format nodes and leaves to your needs
- **Optimized** - Pre-computed prefixes and efficient rendering
- **Zero Dependencies** - Lightweight and fast

### Analysis & Statistics
- **Tree Statistics** - Get depth, width, node/leaf counts, and more
- **Tree Traversal** - Pre-order, post-order, level-order, and filtered iterators
- **Tree Search** - Find nodes/leaves by label or content, get paths

### Transformation & Manipulation
- **Tree Transformation** - Map, filter, and prune trees functionally
- **Tree Sorting** - Sort children by label, depth, or custom comparison
- **Tree Merging** - Merge trees with different strategies (replace, append, merge-by-label)

### Utilities
- **Path Utilities** - Navigate trees by path, flatten to lists
- **Tree Comparison** - Compare structure, compute diffs, check subtrees
- **Export Formats** - Export to HTML, SVG, and Graphviz DOT format
- **Tree Validation** - Validate tree structure

</details>

## Installation

### As a Library

Add this to your `Cargo.toml`:

```toml
[dependencies]
treelog = "0.0.5"
```

### As a CLI Tool

Install the CLI tool using cargo:

```bash
cargo install treelog --features cli,walkdir,json
```

Or build from source:

```bash
git clone https://github.com/muijf/treelog
cd treelog
cargo build --release --bin treelog --features cli,walkdir,json
```

The CLI requires the `cli` feature and optionally other features depending on the input sources you want to use.

## Quick Start

```rust
use treelog::{Tree, renderer::write_tree};

let tree = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["item1".to_string()]),
    Tree::Node("sub".to_string(), vec![
        Tree::Leaf(vec!["subitem1".to_string()]),
        Tree::Leaf(vec!["subitem2".to_string()]),
    ]),
]);

let mut output = String::new();
write_tree(&mut output, &tree).unwrap();
println!("{}", output);
```

**Output:**
```text
root
├─ item1
└─ sub
   ├─ subitem1
   └─ subitem2
```

## Usage

<details>
<summary><b>Basic</b> - Simple tree construction and rendering</summary>

```rust
use treelog::{Tree, renderer::write_tree};

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);
let mut output = String::new();
write_tree(&mut output, &tree).unwrap();
// root
//  └─ item
```

</details>

<details>
<summary><b>Macro DSL</b> - Declarative syntax for tree construction (requires <code>macro</code> feature)</summary>

The `tree!` macro provides a clean, declarative syntax for constructing trees:

```rust
use treelog::tree;

let tree = tree! {
    root {
        "item1",
        "item2",
        sub {
            "subitem1",
            "subitem2"
        }
    }
};
println!("{}", tree.render_to_string());
```

The macro supports:
- **Nodes**: `identifier { ... }` or `"string" { ... }`
- **Leaves**: `"string"` or bare identifiers (converted to strings)
- **Nested structures**: Arbitrary nesting depth
- **Comma-separated**: Children separated by commas (trailing comma optional)

</details>

<details>
<summary><b>Builder API</b> - Fluent interface for tree construction (requires <code>builder</code> feature)</summary>

```rust
use treelog::builder::TreeBuilder;

let mut builder = TreeBuilder::new();
builder.node("root").leaf("item1").node("sub").leaf("subitem1").leaf("subitem2").end().leaf("item2");
let tree = builder.build();
println!("{}", tree.render_to_string());
```

</details>

<details>
<summary><b>Low-Level API</b> - Direct tree construction</summary>

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["line1".to_string(), "line2".to_string()]),
    Tree::Node("sub".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]),
]);
println!("{}", tree.render_to_string());
```

</details>

<details>
<summary><b>Iterator API</b> - Stream trees line by line (requires <code>iterator</code> feature)</summary>

```rust
use treelog::{Tree, TreeIteratorExt};

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);

// Stream lines
for line in TreeIteratorExt::lines(&tree) {
    println!("{} {}", line.prefix, line.content);
}

// Or collect all at once
let lines: Vec<String> = tree.to_lines();
```

</details>

<details>
<summary><b>Customization</b> - Custom styles and formatters</summary>

**Styles**:

```rust
use treelog::{Tree, TreeStyle, RenderConfig, StyleConfig};

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);

// Predefined styles
tree.render_to_string_with_config(&RenderConfig::default().with_style(TreeStyle::Ascii));

// Custom style
let style = StyleConfig::custom("├─", "└─", "│ ", "   ");
tree.render_to_string_with_config(&RenderConfig::default().with_style(style));
```

**Formatters** (requires `formatters` feature):

```rust
use treelog::{Tree, RenderConfig};

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);
let config = RenderConfig::default()
    .with_node_formatter(|label| format!("[{}]", label))
    .with_leaf_formatter(|line| format!("- {}", line));
tree.render_to_string_with_config(&config);
```

</details>

<details>
<summary><b>Streaming Large Trees</b> - Efficient streaming for large trees</summary>

```rust,no_run
use treelog::{Tree, TreeIteratorExt};
use std::io::Write;

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);
let mut stdout = std::io::stdout();
for line in TreeIteratorExt::lines(&tree) {
    writeln!(stdout, "{}{}", line.prefix, line.content).unwrap();
}
```

</details>

## Advanced Features

<details>
<summary><b>Tree Statistics</b> - Get detailed metrics about your tree</summary>

Get detailed statistics about your tree:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["main.rs".to_string()])]),
    Tree::Leaf(vec!["README.md".to_string()]),
]);

// Individual statistics
println!("Depth: {}", tree.depth());
println!("Width: {}", tree.width());
println!("Nodes: {}", tree.node_count());
println!("Leaves: {}", tree.leaf_count());

// Or get all at once
let stats = tree.stats();
println!("{:#?}", stats);
```

</details>

<details>
<summary><b>Tree Traversal</b> - Iterate over trees in different orders (requires <code>traversal</code> feature)</summary>

Iterate over trees in different orders:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["item".to_string()])
]);

// Pre-order: root, then children
for node in tree.pre_order() {
    // Process node
}

// Post-order: children, then root
for node in tree.post_order() {
    // Process node
}

// Level-order: breadth-first
for node in tree.level_order() {
    // Process node
}

// Iterate only over nodes or leaves
for node in tree.nodes() {
    println!("Node: {}", node.label().unwrap());
}

for leaf in tree.leaves() {
    println!("Leaf: {:?}", leaf.lines());
}
```

</details>

<details>
<summary><b>Tree Search</b> - Find nodes and leaves in your tree</summary>

Find nodes and leaves in your tree:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["main.rs".to_string()])]),
]);

// Find first node with label
if let Some(node) = tree.find_node("src") {
    println!("Found: {}", node.label().unwrap());
}

// Find all nodes with label
let all_nodes = tree.find_all_nodes("src");

// Find leaf containing content
if let Some(leaf) = tree.find_leaf("main.rs") {
    println!("Found leaf");
}

// Check if tree contains label/content
if tree.contains("src") {
    println!("Tree contains 'src'");
}

// Get path to a node
if let Some(path) = tree.path_to("main.rs") {
    println!("Path: {:?}", path); // [0, 0]
}
```

</details>

<details>
<summary><b>Tree Transformation</b> - Transform trees functionally (requires <code>transform</code> feature)</summary>

Transform trees functionally:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["main.rs".to_string()])]),
]);

// Map node labels
let transformed = tree.map_nodes(|label| format!("[{label}]"));

// Map leaf lines
let transformed = tree.map_leaves(|line| format!("- {line}"));

// Filter tree
let filtered = tree.filter(|t| {
    match t {
        Tree::Leaf(lines) => lines.iter().any(|l| l.contains("main")),
        Tree::Node(_, _) => true,
    }
});

// Prune tree (inverse of filter)
let pruned = tree.prune(|t| {
    match t {
        Tree::Leaf(lines) => lines.iter().any(|l| l.contains("README")),
        Tree::Node(_, _) => false,
    }
});
```

</details>

<details>
<summary><b>Tree Sorting</b> - Sort tree children</summary>

Sort tree children:

```rust
use treelog::Tree;

let mut tree = Tree::Node("root".to_string(), vec![
    Tree::Node("zebra".to_string(), vec![]),
    Tree::Node("apple".to_string(), vec![]),
]);

// Sort by label alphabetically
tree.sort_by_label();

// Sort by depth
tree.sort_by_depth(true); // deepest first

// Custom sort
let mut compare = |a: &Tree, b: &Tree| {
    // Your comparison logic
    std::cmp::Ordering::Equal
};
tree.sort_children(&mut compare);
```

</details>

<details>
<summary><b>Tree Path Utilities</b> - Navigate trees by path (requires <code>path</code> feature)</summary>

Navigate and manipulate trees by path:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["main.rs".to_string()])]),
]);

// Get path to a node
let child = &tree.children().unwrap()[0];
if let Some(path) = tree.get_path(child) {
    println!("Path: {:?}", path); // [0]
}

// Get node by path
if let Some(node) = tree.get_by_path(&[0]) {
    println!("Node: {}", node.label().unwrap());
}

// Modify node by path
let mut tree = tree.clone();
if let Some(Tree::Node(label, _)) = tree.get_by_path_mut(&[0]) {
    *label = "source".to_string();
}

// Flatten tree to list
let flattened = tree.flatten();
for entry in flattened {
    println!("Path: {:?}, Content: {}", entry.path, entry.content);
}
```

</details>

<details>
<summary><b>Tree Comparison</b> - Compare trees and find differences (requires <code>comparison</code> feature)</summary>

Compare trees and find differences:

```rust
use treelog::Tree;

let tree1 = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["a".to_string()])
]);
let tree2 = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["b".to_string()])
]);

// Compare structure (ignoring content)
if tree1.eq_structure(&tree2) {
    println!("Same structure!");
}

// Compute differences
let diffs = tree1.diff(&tree2);
for diff in diffs {
    match diff {
        treelog::comparison::TreeDiff::OnlyInFirst { path, content } => {
            println!("Only in first: {:?} - {}", path, content);
        }
        treelog::comparison::TreeDiff::OnlyInSecond { path, content } => {
            println!("Only in second: {:?} - {}", path, content);
        }
        treelog::comparison::TreeDiff::DifferentContent { path, first, second } => {
            println!("Different at {:?}: '{}' vs '{}'", path, first, second);
        }
    }
}

// Check if subtree
let subtree = Tree::Leaf(vec!["a".to_string()]);
if subtree.is_subtree_of(&tree1) {
    println!("Is a subtree!");
}
```

</details>

<details>
<summary><b>Tree Merging</b> - Merge trees with different strategies (requires <code>merge</code> feature)</summary>

Merge trees with different strategies:

```rust
use treelog::{Tree, merge::MergeStrategy};

let tree1 = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["main.rs".to_string()])]),
]);
let tree2 = Tree::Node("root".to_string(), vec![
    Tree::Node("src".to_string(), vec![Tree::Leaf(vec!["lib.rs".to_string()])]),
]);

// Replace: replace first tree with second
let merged = tree1.merge(tree2.clone(), MergeStrategy::Replace);

// Append: append all children
let merged = tree1.merge(tree2.clone(), MergeStrategy::Append);

// MergeByLabel: merge nodes with matching labels
let merged = tree1.merge(tree2, MergeStrategy::MergeByLabel);
```

</details>

<details>
<summary><b>Export Formats</b> - Export trees to HTML, SVG, and DOT (requires <code>export</code> feature)</summary>

Export trees to various formats:

```rust
use treelog::Tree;
use std::fs;

let tree = Tree::Node("root".to_string(), vec![
    Tree::Leaf(vec!["item".to_string()])
]);

// Create exports directory
fs::create_dir_all("exports").unwrap();

// Export to HTML (with collapsible nodes)
let html = tree.to_html();
fs::write("exports/tree.html", html).unwrap();

// Export to SVG
let svg = tree.to_svg();
fs::write("exports/tree.svg", svg).unwrap();

// Export to Graphviz DOT format
let dot = tree.to_dot();
fs::write("exports/tree.dot", dot).unwrap();
// Render with: dot -Tpng exports/tree.dot -o exports/tree.png
```

</details>

<details>
<summary><b>Library Integrations</b> - Integrations with popular Rust libraries</summary>

### Serialization (serde)

Serialize and deserialize trees to/from JSON and YAML:

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);

// JSON serialization (requires `json` feature)
#[cfg(feature = "json")]
{
    let json = tree.to_json().unwrap();
    let deserialized = Tree::from_json(&json).unwrap();
}

// YAML serialization (requires `yaml` feature)
#[cfg(feature = "yaml")]
{
    let yaml = tree.to_yaml().unwrap();
    let deserialized = Tree::from_yaml(&yaml).unwrap();
}
```

### TOML Support

**Serialize/deserialize trees to/from TOML** (requires both `toml` and `serde` features):

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);

// Serialize tree to TOML (preserves exact Tree structure)
let toml = tree.to_toml().unwrap();

// Deserialize back
let deserialized = Tree::from_toml(&toml).unwrap();
```

### File System Integration

Build trees from directory structures (requires `walkdir` feature):

```rust
use treelog::Tree;

// Build tree from current directory
let tree = Tree::from_dir(".").unwrap();

// Build tree with maximum depth
let tree = Tree::from_dir_max_depth(".", 2).unwrap();
```

### Graph Integration

Convert between trees and petgraph graphs (requires `petgraph` feature):

```rust
use treelog::Tree;
use petgraph::Graph;

// Tree to graph
let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);
let graph: Graph<String, ()> = tree.to_graph();

// Graph to tree
let mut graph = Graph::<String, ()>::new();
let a = graph.add_node("A".to_string());
let b = graph.add_node("B".to_string());
graph.add_edge(a, b, ());
let tree = Tree::from_graph(&graph);
```

### Cargo Metadata Integration

Visualize Cargo dependency trees (requires `cargo-metadata` feature):

```rust
use treelog::Tree;

// Build dependency tree for current project
let tree = Tree::from_cargo_metadata("Cargo.toml").unwrap();

// Build dependency tree for specific package
let tree = Tree::from_cargo_package_deps("treelog", "Cargo.toml").unwrap();
```

### Git Integration

Visualize Git repository structures (requires `git2` feature):

```rust
use treelog::Tree;

// Build tree from Git repository
let tree = Tree::from_git_repo(".").unwrap();

// Build tree from branches
use git2::Repository;
let repo = Repository::open(".").unwrap();
let tree = Tree::from_git_branches(&repo).unwrap();

// Build tree from specific commit
let commit = repo.head().unwrap().peel_to_commit().unwrap();
let tree = Tree::from_git_commit_tree(&repo, &commit).unwrap();
```

### XML/HTML Integration

Visualize XML/HTML DOM trees (requires `roxmltree` feature):

```rust,no_run
use treelog::Tree;

// Build tree from XML string
let xml = r#"<root><child>text</child></root>"#;
let tree = Tree::from_arbitrary_xml(xml).unwrap();

// Build tree from XML file
let tree = Tree::from_arbitrary_xml_file("example.xml").unwrap();
```

### Rust AST Integration

Visualize Rust source code AST (requires `syn` feature):

```rust,no_run
use treelog::Tree;

// Build tree from Rust file
let tree = Tree::from_syn_file("src/lib.rs").unwrap();

// Build tree from syn::File AST
use syn::parse_file;
let ast = parse_file("fn main() {}").unwrap();
let tree = Tree::from_syn_file_ast(&ast);

// Build tree from individual item
use syn::parse_quote;
let item: syn::Item = parse_quote! {
    struct Test { field: i32 }
};
let tree = Tree::from_syn_item(&item);
```

### RON Integration

Serialize and deserialize trees to/from RON (requires `ron` and `serde` features):

```rust
use treelog::Tree;

let tree = Tree::Node("root".to_string(), vec![Tree::Leaf(vec!["item".to_string()])]);

// Serialize to RON
let ron = tree.to_ron().unwrap();

// Serialize to pretty RON
let ron_pretty = tree.to_ron_pretty().unwrap();

// Deserialize from RON
let deserialized = Tree::from_ron(&ron).unwrap();
```

### Tree-sitter Integration

Visualize tree-sitter parse trees (requires `tree-sitter` feature):

```rust
use treelog::Tree;
use tree_sitter::{Parser, Language};

// Load a language (tree-sitter-rust is available as a dev-dependency for doctests)
let language: Language = tree_sitter_rust::LANGUAGE.into();

let mut parser = Parser::new();
parser.set_language(&language).unwrap();
let source_code = "fn main() {}";
let parse_tree = parser.parse(source_code, None).unwrap();
let tree = Tree::from_tree_sitter(&parse_tree);

// Or parse and convert in one step
let tree = Tree::from_tree_sitter_language(source_code, language).unwrap();
```

### Clap Integration

Visualize command-line argument structures (requires `clap` feature):

```rust
use treelog::Tree;
use clap::{Command, Arg};

let cmd = Command::new("myapp")
    .subcommand(Command::new("subcommand"))
    .arg(Arg::new("input").short('i'));

let tree = Tree::from_clap_command(&cmd);
```

</details>

<details>
<summary><b>Feature Flags</b> - Configure which features to enable</summary>

Most advanced features are behind feature flags to keep the core library lightweight:

```toml
[dependencies]
treelog = { version = "0.0.5", features = ["traversal", "transform", "path", "comparison", "merge", "export"] }
```

Available features:
- `traversal` - Tree traversal iterators (pre-order, post-order, level-order)
- `transform` - Tree transformation operations (map, filter, prune)
- `path` - Tree path utilities (get by path, flatten)
- `comparison` - Tree comparison and diff operations
- `merge` - Tree merging with different strategies
- `export` - Export to HTML, SVG, and DOT formats
- `builder` - Builder API for constructing trees
- `iterator` - Iterator API for streaming trees
- `macro` - Macro DSL for tree construction
- `formatters` - Custom formatters for nodes and leaves
- `color` - Color output support
- `serde` - Serde serialization support (Serialize/Deserialize traits)
- `json` - JSON serialization/deserialization (requires `serde`)
- `yaml` - YAML serialization/deserialization (requires `serde`)
- `toml` - TOML parsing and tree conversion
- `walkdir` - File system tree building from directories
- `petgraph` - Graph to/from tree conversion
- `cargo-metadata` - Cargo dependency tree visualization
- `git2` - Git repository structure visualization
- `roxmltree` - XML/HTML DOM tree visualization
- `syn` - Rust AST visualization
- `ron` - RON (Rusty Object Notation) serialization
- `tree-sitter` - Tree-sitter parse tree visualization
- `clap` - Command-line argument structure visualization
- `cli` - CLI binary (includes `clap`)

Use `all` feature to enable everything (note: `cli` is separate and must be enabled explicitly for the binary):
```toml
treelog = { version = "0.0.5", features = ["all"] }
```

To build the CLI binary, enable the `cli` feature along with any input source features you need:
```toml
treelog = { version = "0.0.5", features = ["cli", "walkdir", "json"] }
```

</details>

<details>
<summary><b>Performance</b> - Performance characteristics and optimizations</summary>

- **Pre-computed prefixes** - Efficient string buffer capacity estimation
- **Iterator API** - Stream large trees without materializing the entire structure
- **Stack-based traversal** - Memory-efficient tree walking
- **Zero-copy operations** - Most operations work with references where possible

</details>

## CLI Usage

The `treelog` CLI tool provides a convenient way to visualize trees from various sources without writing code. It's built on top of the library and exposes all its features through a command-line interface.

### Basic Usage

```bash
# Visualize a directory structure
treelog from dir . --max-depth 3

# Visualize Cargo dependencies
treelog from cargo

# Visualize Git repository structure
treelog from git .

# Get tree statistics
treelog from dir . --format json | treelog stats
```

### Input Sources

The CLI supports creating trees from various sources:

- **Directory structures**: `treelog from dir <path> [--max-depth <n>]`
- **Cargo metadata**: `treelog from cargo [--package <name>]`
- **Git repositories**: `treelog from git [path] [--branches] [--commit]`
- **XML/HTML files**: `treelog from xml <file>`
- **Rust source files**: `treelog from rust <file>`
- **JSON/YAML/TOML/RON files**: `treelog from json|yaml|toml|ron <file>`

### Rendering Options

```bash
# Use ASCII style
treelog from dir . --style ascii

# Custom style
treelog from dir . --custom-style ">-,<-,| ,   "

# Output to file
treelog from dir . --output tree.txt

# Export to different formats
treelog from dir . --format json > tree.json
treelog from dir . --format html > tree.html
treelog from dir . --format svg > tree.svg
treelog from dir . --format dot > tree.dot
```

### Tree Operations

```bash
# Get statistics
treelog stats < input.json

# Search for nodes
treelog search "pattern" < input.json

# Sort tree
treelog sort --method label < input.json

# Transform tree
treelog transform map-nodes "[{}]" < input.json

# Compare trees
treelog compare tree1.json tree2.json

# Merge trees
treelog merge --strategy append tree1.json tree2.json
```

### Piping and Serialization

The CLI supports piping trees between commands using serialized formats:

```bash
# Create tree and get statistics
treelog from dir . --format json | treelog stats

# Transform and export
treelog from dir . --format json | treelog transform filter "src" | treelog export html > output.html
```

**Note**: When piping between commands, use `--format json` (or `yaml`, `toml`, `ron`) to serialize the tree structure. The default `text` format is for human-readable output only.

### Help

Get help for any command:

```bash
treelog --help
treelog from --help
treelog from dir --help
```

## Examples

The repository includes comprehensive examples demonstrating all features:

**Core Examples:**
- **[`basic`]examples/basic.rs** - Basic tree construction and rendering
- **[`builder`]examples/builder.rs** - Using the builder API
- **[`iterator`]examples/iterator.rs** - Streaming large trees
- **[`macro`]examples/macro.rs** - Using the macro DSL
- **[`customization`]examples/customization.rs** - Custom styles and formatters
- **[`complex`]examples/complex.rs** - Complex tree structures
- **[`file_tree`]examples/file_tree.rs** - File system tree example

**Advanced Examples:**
- **[`statistics`]examples/statistics.rs** - Tree statistics and analysis
- **[`traversal`]examples/traversal.rs** - Tree traversal iterators
- **[`search`]examples/search.rs** - Tree search and query operations
- **[`transform`]examples/transform.rs** - Tree transformation operations
- **[`sorting`]examples/sorting.rs** - Tree sorting operations
- **[`path`]examples/path.rs** - Tree path utilities
- **[`comparison`]examples/comparison.rs** - Tree comparison and diff
- **[`merge`]examples/merge.rs** - Tree merging strategies
- **[`export`]examples/export.rs** - Export to HTML, SVG, and DOT formats

**Integration Examples:**
- **[`serde`]examples/serde.rs** - JSON and YAML serialization
- **[`toml`]examples/toml.rs** - TOML parsing and conversion
- **[`filesystem`]examples/filesystem.rs** - File system tree building
- **[`petgraph`]examples/petgraph.rs** - Graph to/from tree conversion
- **[`cargo`]examples/cargo.rs** - Cargo dependency tree visualization
- **[`git2`]examples/git2.rs** - Git repository structure visualization
- **[`xml`]examples/xml.rs** - XML/HTML DOM tree visualization
- **[`syn`]examples/syn.rs** - Rust AST visualization
- **[`ron`]examples/ron.rs** - RON serialization
- **[`tree_sitter`]examples/tree_sitter.rs** - Tree-sitter parse tree visualization
- **[`clap`]examples/clap.rs** - Command-line argument structure visualization

Run any example with:
```bash
cargo run --example <name> --all-features
```

## Development

```bash
cargo fmt --all
cargo clippy --all-targets --all-features -- -D warnings
cargo test --all-features
```

**Real-time feedback**: This project includes VS Code/Cursor settings (`.vscode/settings.json`) that configure rust-analyzer to provide real-time feedback:
- **Formatting**: Automatically formats on save (matches `cargo fmt`)
- **Clippy**: Shows clippy warnings/errors as you type (matches pre-commit hook settings)
- **Tests**: Displays test failures in the editor

Git hooks available via [pre-commit](https://pre-commit.com/) (see `.pre-commit-config.yaml`).

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.