microcad_lang_base/
tree_display.rs1pub trait TreeDisplay {
8 fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result;
11}
12
13const INDENT: usize = 2;
15
16#[derive(derive_more::Deref, Clone, Copy)]
18pub struct TreeState {
19 #[deref]
21 pub depth: usize,
22 pub debug: bool,
24}
25
26impl TreeState {
27 pub fn new_display() -> Self {
29 Self {
30 depth: 0,
31 debug: false,
32 }
33 }
34
35 pub fn new_debug(depth: usize) -> Self {
37 Self { depth, debug: true }
38 }
39 pub fn indent(&mut self) {
41 self.depth += INDENT
42 }
43
44 pub fn indented(&self) -> Self {
46 Self {
47 depth: self.depth + INDENT,
48 debug: self.debug,
49 }
50 }
51}
52
53pub struct FormatTree<'a, T: TreeDisplay>(pub &'a T);
55
56impl<T: TreeDisplay> std::fmt::Display for FormatTree<'_, T> {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 self.0.tree_print(
59 f,
60 TreeState {
61 depth: 2,
62 debug: false,
63 },
64 )
65 }
66}
67
68impl<T: TreeDisplay> std::fmt::Debug for FormatTree<'_, T> {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 self.0.tree_print(
71 f,
72 TreeState {
73 depth: 2,
74 debug: true,
75 },
76 )
77 }
78}