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
//! Demonstrates gilt's #[derive(Tree)] proc macro.
//! Run with: cargo run --example derive_tree --features derive
#[cfg(feature = "derive")]
fn main() {
use gilt::prelude::*;
use gilt::Tree as TreeDerive;
let mut console = Console::new();
// ── 1. File system tree — recursive struct with label and children ───
console.print_text("\n[bold cyan]1. File system tree — basic recursive structure[/]");
#[derive(TreeDerive)]
struct FileEntry {
#[tree(label)]
name: String,
#[tree(children)]
entries: Vec<FileEntry>,
}
let root = FileEntry {
name: "my-project/".into(),
entries: vec![
FileEntry {
name: "src/".into(),
entries: vec![
FileEntry {
name: "main.rs".into(),
entries: vec![],
},
FileEntry {
name: "lib.rs".into(),
entries: vec![],
},
FileEntry {
name: "utils/".into(),
entries: vec![
FileEntry {
name: "helpers.rs".into(),
entries: vec![],
},
FileEntry {
name: "config.rs".into(),
entries: vec![],
},
],
},
],
},
FileEntry {
name: "tests/".into(),
entries: vec![
FileEntry {
name: "integration.rs".into(),
entries: vec![],
},
FileEntry {
name: "unit.rs".into(),
entries: vec![],
},
],
},
FileEntry {
name: "Cargo.toml".into(),
entries: vec![],
},
FileEntry {
name: "README.md".into(),
entries: vec![],
},
],
};
console.print(&root.to_tree());
// ── 2. With leaf details — file size shown as leaf nodes ────────────
console.print_text("\n[bold cyan]2. With leaf details — file sizes as leaf nodes[/]");
#[derive(TreeDerive)]
struct SizedEntry {
#[tree(label)]
name: String,
#[tree(leaf)]
size_bytes: u64,
#[tree(children)]
children: Vec<SizedEntry>,
}
let project = SizedEntry {
name: "dist/".into(),
size_bytes: 0,
children: vec![
SizedEntry {
name: "index.html".into(),
size_bytes: 4_096,
children: vec![],
},
SizedEntry {
name: "assets/".into(),
size_bytes: 0,
children: vec![
SizedEntry {
name: "app.js".into(),
size_bytes: 128_512,
children: vec![],
},
SizedEntry {
name: "style.css".into(),
size_bytes: 24_320,
children: vec![],
},
SizedEntry {
name: "logo.png".into(),
size_bytes: 51_200,
children: vec![],
},
],
},
SizedEntry {
name: "manifest.json".into(),
size_bytes: 512,
children: vec![],
},
],
};
console.print(&project.to_tree());
// ── 3. Styled tree — colored nodes and guide lines ──────────────────
console.print_text("\n[bold cyan]3. Styled tree — colored output[/]");
#[derive(TreeDerive)]
#[tree(style = "bold green", guide_style = "dim cyan")]
struct StyledEntry {
#[tree(label)]
name: String,
#[tree(leaf)]
description: String,
#[tree(children)]
items: Vec<StyledEntry>,
}
let menu = StyledEntry {
name: "Application Menu".into(),
description: "Top-level navigation".into(),
items: vec![
StyledEntry {
name: "File".into(),
description: "File operations".into(),
items: vec![
StyledEntry {
name: "New".into(),
description: "Create new document".into(),
items: vec![],
},
StyledEntry {
name: "Open".into(),
description: "Open existing file".into(),
items: vec![],
},
StyledEntry {
name: "Save".into(),
description: "Save current file".into(),
items: vec![],
},
],
},
StyledEntry {
name: "Edit".into(),
description: "Edit operations".into(),
items: vec![
StyledEntry {
name: "Undo".into(),
description: "Undo last action".into(),
items: vec![],
},
StyledEntry {
name: "Redo".into(),
description: "Redo last undo".into(),
items: vec![],
},
],
},
StyledEntry {
name: "View".into(),
description: "Display settings".into(),
items: vec![],
},
],
};
console.print(&menu.to_tree());
// ── 4. Org chart — department / team / person hierarchy ─────────────
console.print_text("\n[bold cyan]4. Org chart — department / team / person[/]");
#[derive(TreeDerive)]
#[tree(style = "bold", guide_style = "dim blue")]
struct OrgNode {
#[tree(label)]
title: String,
#[tree(leaf)]
role: String,
#[tree(children)]
reports: Vec<OrgNode>,
}
let org = OrgNode {
title: "Acme Corp".into(),
role: "Organization".into(),
reports: vec![
OrgNode {
title: "Engineering".into(),
role: "Department".into(),
reports: vec![
OrgNode {
title: "Platform Team".into(),
role: "Team".into(),
reports: vec![
OrgNode {
title: "Alice Chen".into(),
role: "Staff Engineer".into(),
reports: vec![],
},
OrgNode {
title: "Bob Martinez".into(),
role: "Senior Engineer".into(),
reports: vec![],
},
],
},
OrgNode {
title: "Frontend Team".into(),
role: "Team".into(),
reports: vec![
OrgNode {
title: "Carol Washington".into(),
role: "Tech Lead".into(),
reports: vec![],
},
OrgNode {
title: "David Kim".into(),
role: "Engineer".into(),
reports: vec![],
},
],
},
],
},
OrgNode {
title: "Design".into(),
role: "Department".into(),
reports: vec![
OrgNode {
title: "UX Team".into(),
role: "Team".into(),
reports: vec![
OrgNode {
title: "Eve Johnson".into(),
role: "Senior Designer".into(),
reports: vec![],
},
],
},
],
},
OrgNode {
title: "Product".into(),
role: "Department".into(),
reports: vec![
OrgNode {
title: "Frank Lee".into(),
role: "Product Manager".into(),
reports: vec![],
},
OrgNode {
title: "Grace Patel".into(),
role: "Product Analyst".into(),
reports: vec![],
},
],
},
],
};
console.print(&org.to_tree());
}
#[cfg(not(feature = "derive"))]
fn main() {
eprintln!(
"This example requires the 'derive' feature: cargo run --example derive_tree --features derive"
);
}