Skip to main content

tree_table/api/
debug.rs

1use alloc::format;
2use alloc::string::String;
3use alloc::string::ToString;
4
5use crate::Table;
6use crate::log;
7
8impl Table {
9    // Existing macro-based log (uses println! — visible in native targets, silent in wasm)
10    pub fn log_grid(&self) -> Result<(), String> {
11        log!("Grid: {:?}", self.grid);
12        Ok(())
13    }
14
15    pub fn log_index(&self) -> Result<(), String> {
16        log!("Index: {:?}", self.grid.index);
17        Ok(())
18    }
19
20    pub fn log_header(&self) -> Result<(), String> {
21        log!("Header: {:?}", self.grid.header);
22        Ok(())
23    }
24
25    pub fn log_selection(&self) -> Result<(), String> {
26        log!("Selection: {:?}", self.sel);
27        Ok(())
28    }
29
30    pub fn log_options(&self) -> Result<(), String> {
31        log!("Options: {:?}", self.opts);
32        Ok(())
33    }
34
35    pub fn log_disp_rows(&self) -> Result<(), String> {
36        log!("Displayed Rows: {:?}", self.grid.disp_rows);
37        Ok(())
38    }
39
40    pub fn log_canvas_rows(&self) -> Result<(), String> {
41        log!("Canvas Rows: {:?}", self.grid.canvas_rows);
42        Ok(())
43    }
44
45    pub fn log_disp_cols(&self) -> Result<(), String> {
46        log!("Displayed Cols: {:?}", self.grid.disp_cols);
47        Ok(())
48    }
49
50    pub fn log_canvas_cols(&self) -> Result<(), String> {
51        log!("Canvas Cols: {:?}", self.grid.canvas_cols);
52        Ok(())
53    }
54
55    pub fn log_undo_stack(&self) -> Result<(), String> {
56        log!("Undo Stack Size: {}", self.grid.undo_stack.len());
57        log!("Redo Stack Size: {}", self.grid.redo_stack.len());
58        Ok(())
59    }
60
61    // String-returning debug functions (safe for wasm — wasm layer can console.log the result)
62    // These avoid println! entirely and return formatted strings for flexible use
63    pub fn debug_grid(&self) -> String {
64        format!("Grid: {:?}", self.grid)
65    }
66
67    pub fn debug_index(&self) -> String {
68        format!("Index: {:?}", self.grid.index)
69    }
70
71    pub fn debug_header(&self) -> String {
72        format!("Header: {:?}", self.grid.header)
73    }
74
75    pub fn debug_selection(&self) -> String {
76        format!("Selection: {:?}", self.sel)
77    }
78
79    pub fn debug_options(&self) -> String {
80        format!("Options: {:?}", self.opts)
81    }
82
83    pub fn debug_disp_rows(&self) -> String {
84        format!("Displayed Rows: {:?}", self.grid.disp_rows)
85    }
86
87    pub fn debug_canvas_rows(&self) -> String {
88        format!("Canvas Rows: {:?}", self.grid.canvas_rows)
89    }
90
91    pub fn debug_disp_cols(&self) -> String {
92        format!("Displayed Cols: {:?}", self.grid.disp_cols)
93    }
94
95    pub fn debug_canvas_cols(&self) -> String {
96        format!("Canvas Cols: {:?}", self.grid.canvas_cols)
97    }
98
99    pub fn debug_sizes(&self) -> String {
100        format!(
101            "Total Rows: {} | Total Cols: {} | Undo Stack: {} | Redo Stack: {}",
102            Self::acr_total_rows(&self.grid.index.cells),
103            Self::acr_total_cols(&self.grid.header.cells),
104            self.grid.undo_stack.len(),
105            self.grid.redo_stack.len(),
106        )
107    }
108
109    pub fn debug_tree_summary(&self) -> String {
110        if !self.opts.tree_mode {
111            return "Tree mode disabled".to_string();
112        }
113        let mut summary = String::new();
114        summary.push_str(&format!(
115            "Tree Mode Enabled | Total Rows: {}\n",
116            Self::acr_total_rows(&self.grid.index.cells)
117        ));
118        summary.push_str("Root-level rows (visible tops):\n");
119        for (rn, cell) in self.grid.index.cells.iter().enumerate() {
120            if cell.par.is_empty() {
121                summary.push_str(&format!(
122                    "  Row {}: iid={} vis={} open={} children={}\n",
123                    rn,
124                    cell.iid,
125                    cell.vis,
126                    cell.open,
127                    cell.chn.len()
128                ));
129            }
130        }
131        summary
132    }
133
134    pub fn debug_full(&self) -> String {
135        let mut full = String::new();
136        full.push_str(&self.debug_sizes());
137        full.push_str("\n\n");
138        full.push_str(&self.debug_options());
139        full.push_str("\n\n");
140        full.push_str(&self.debug_header());
141        full.push_str("\n\n");
142        full.push_str(&self.debug_index());
143        full.push_str("\n\n");
144        full.push_str(&self.debug_selection());
145        full.push_str("\n\n");
146        full.push_str(&self.debug_tree_summary());
147        full
148    }
149}