Skip to main content

tree_table/types/
change.rs

1use alloc::vec::Vec;
2
3use crate::{ChangeHeaderCell, ChangeIndexCell, HeaderCell, IndexCell, Selected, Span, TableCell};
4
5#[cfg_attr(feature = "tsify", derive(tsify::Tsify))]
6#[cfg_attr(feature = "tsify", tsify(from_wasm_abi))]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[derive(Debug, Clone, PartialEq)]
9pub enum Change {
10    IndexCell {
11        row: usize,
12        change: ChangeIndexCell,
13    },
14    HeaderCell {
15        col: usize,
16        change: ChangeHeaderCell,
17    },
18
19    AddRows(Vec<usize>),
20    AddCols(Vec<usize>),
21
22    RemoveRows(Vec<(usize, IndexCell)>), // (position, row)
23
24    RemoveCols {
25        removed_header: Vec<(usize, HeaderCell)>,
26        removed_table: Vec<(usize, Vec<(usize, TableCell)>)>,
27    },
28
29    MoveRows(Vec<(usize, usize)>), // from → to
30    MoveCols(Vec<(usize, usize)>),
31
32    AddSelectionBoxes {
33        count: usize,
34        old_selected: Option<Selected>,
35    },
36    RemoveSelectionBoxes {
37        removed: Vec<Span>,
38        old_selected: Option<Selected>,
39    },
40    SetSelectionBoxes {
41        old_selection_boxes: Vec<Span>,
42        old_selected: Option<Selected>,
43    },
44}
45
46impl Change {
47    // ───── Change constructors ─────
48    pub fn index_cell(row: usize, change: ChangeIndexCell) -> Self {
49        Change::IndexCell { row, change }
50    }
51
52    pub fn header_cell(col: usize, change: ChangeHeaderCell) -> Self {
53        Change::HeaderCell { col, change }
54    }
55
56    // ───── Structural constructors ─────
57    pub fn add_rows(positions: Vec<usize>) -> Self {
58        Change::AddRows(positions)
59    }
60
61    pub fn add_cols(positions: Vec<usize>) -> Self {
62        Change::AddCols(positions)
63    }
64
65    pub fn remove_rows(rows: Vec<(usize, IndexCell)>) -> Self {
66        Change::RemoveRows(rows)
67    }
68
69    pub fn remove_cols(
70        removed_header: Vec<(usize, HeaderCell)>,
71        removed_table: Vec<(usize, Vec<(usize, TableCell)>)>,
72    ) -> Self {
73        Change::RemoveCols {
74            removed_header,
75            removed_table,
76        }
77    }
78
79    pub fn move_rows(map: Vec<(usize, usize)>) -> Self {
80        Change::MoveRows(map)
81    }
82
83    pub fn move_cols(map: Vec<(usize, usize)>) -> Self {
84        Change::MoveCols(map)
85    }
86
87    // ───── Selection box constructors ─────
88    /// One or more boxes were pushed to the end.
89    pub fn add_selection_boxes(count: usize, old_selected: Option<Selected>) -> Self {
90        Change::AddSelectionBoxes {
91            count,
92            old_selected,
93        }
94    }
95
96    /// One or more boxes were removed (any indices).
97    pub fn remove_selection_boxes(removed: Vec<Span>, old_selected: Option<Selected>) -> Self {
98        Change::RemoveSelectionBoxes {
99            removed,
100            old_selected,
101        }
102    }
103
104    pub fn set_selection_boxes(
105        old_selection_boxes: Vec<Span>,
106        old_selected: Option<Selected>,
107    ) -> Self {
108        Change::SetSelectionBoxes {
109            old_selection_boxes,
110            old_selected,
111        }
112    }
113
114    #[inline]
115    pub fn is_selection_change(&self) -> bool {
116        matches!(
117            self,
118            Change::AddSelectionBoxes { .. }
119                | Change::RemoveSelectionBoxes { .. }
120                | Change::SetSelectionBoxes { .. }
121        )
122    }
123}