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
// Copyright © 2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Display trait for tree like output
/// Trait for displaying a tree
pub trait TreeDisplay {
/// Write item into `f` and use `{:depth$}` syntax in front of your single line
/// output to get proper indention.
fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result;
/// Display as tree starting at depth `0`.
fn display_tree(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.tree_print(
f,
TreeState {
depth: 0,
debug: false,
},
)
}
/// Display as tree starting at given depth in debug mode
fn debug_tree(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.tree_print(
f,
TreeState {
depth: 0,
debug: true,
},
)
}
/// Display as tree starting at depth `0` into a file
fn write_tree(&self, f: &mut impl std::io::Write) -> std::io::Result<()> {
write!(
f,
"{}",
WriteFmt(|f| self.tree_print(
f,
TreeState {
depth: 0,
debug: false
}
))
)
}
}
/// Helper to write into io from fmt writers
struct WriteFmt<F>(pub F)
where
F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result;
impl<F> std::fmt::Display for WriteFmt<F>
where
F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0(f)
}
}
/// Indention size
const INDENT: usize = 2;
/// Indention depth counter
#[derive(derive_more::Deref, Clone, Copy)]
pub struct TreeState {
#[deref]
depth: usize,
/// Print in debug mode
pub debug: bool,
}
impl TreeState {
/// Create new tree state for std::fmt::Display
pub fn new_display() -> Self {
Self {
depth: 0,
debug: false,
}
}
/// Create new tree state for std::fmt::Debug
pub fn new_debug(depth: usize) -> Self {
Self { depth, debug: true }
}
/// Change indention one step deeper
pub fn indent(&mut self) {
self.depth += INDENT
}
/// Return a indention which is one step deeper
pub fn indented(&self) -> Self {
Self {
depth: self.depth + INDENT,
debug: self.debug,
}
}
}
/// print syntax via std::fmt::Display
pub struct FormatTree<'a, T: TreeDisplay>(pub &'a T);
impl<T: TreeDisplay> std::fmt::Display for FormatTree<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.tree_print(
f,
TreeState {
depth: 2,
debug: false,
},
)
}
}
impl<T: TreeDisplay> std::fmt::Debug for FormatTree<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.tree_print(
f,
TreeState {
depth: 2,
debug: true,
},
)
}
}