microcad_lang/
tree_display.rs1pub trait TreeDisplay {
8 fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result;
11
12 fn display_tree(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
14 self.tree_print(
15 f,
16 TreeState {
17 depth: 0,
18 debug: false,
19 },
20 )
21 }
22
23 fn debug_tree(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25 self.tree_print(
26 f,
27 TreeState {
28 depth: 0,
29 debug: true,
30 },
31 )
32 }
33
34 fn write_tree(&self, f: &mut impl std::io::Write) -> std::io::Result<()> {
36 write!(
37 f,
38 "{}",
39 WriteFmt(|f| self.tree_print(
40 f,
41 TreeState {
42 depth: 0,
43 debug: false
44 }
45 ))
46 )
47 }
48}
49
50struct WriteFmt<F>(pub F)
52where
53 F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result;
54
55impl<F> std::fmt::Display for WriteFmt<F>
56where
57 F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result,
58{
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 self.0(f)
61 }
62}
63
64const INDENT: usize = 2;
66
67#[derive(derive_more::Deref, Clone, Copy)]
69pub struct TreeState {
70 #[deref]
72 pub depth: usize,
73 pub debug: bool,
75}
76
77impl TreeState {
78 pub fn new_display() -> Self {
80 Self {
81 depth: 0,
82 debug: false,
83 }
84 }
85
86 pub fn new_debug(depth: usize) -> Self {
88 Self { depth, debug: true }
89 }
90 pub fn indent(&mut self) {
92 self.depth += INDENT
93 }
94
95 pub fn indented(&self) -> Self {
97 Self {
98 depth: self.depth + INDENT,
99 debug: self.debug,
100 }
101 }
102}
103
104pub struct FormatTree<'a, T: TreeDisplay>(pub &'a T);
106
107impl<T: TreeDisplay> std::fmt::Display for FormatTree<'_, T> {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 self.0.tree_print(
110 f,
111 TreeState {
112 depth: 2,
113 debug: false,
114 },
115 )
116 }
117}
118
119impl<T: TreeDisplay> std::fmt::Debug for FormatTree<'_, T> {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 self.0.tree_print(
122 f,
123 TreeState {
124 depth: 2,
125 debug: true,
126 },
127 )
128 }
129}