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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/// Manages the order and visibility of columns in a table.
#[derive(Clone, PartialEq, Debug)]
pub struct ColumnOrder {
/// The order of visible columns (indices into the column tuple)
order: Vec<usize>,
/// Total number of columns available
total_columns: usize,
}
impl ColumnOrder {
/// Creates a new ColumnOrder with default ordering (all columns visible in natural order)
pub fn new(total_columns: usize) -> Self {
Self {
order: (0..total_columns).collect(),
total_columns,
}
}
/// Returns the current column order as a slice
pub fn get_order(&self) -> &[usize] {
&self.order
}
/// Returns the total number of columns
pub fn total_columns(&self) -> usize {
self.total_columns
}
/// Swaps two columns in the display order.
/// If either column is hidden or out of bounds, this is a no-op (saturating behavior).
pub fn swap(&mut self, col_a: usize, col_b: usize) {
// Saturate to valid column indices
let col_a = col_a.min(self.total_columns.saturating_sub(1));
let col_b = col_b.min(self.total_columns.saturating_sub(1));
// Find positions in the order vector
let pos_a = self.order.iter().position(|&c| c == col_a);
let pos_b = self.order.iter().position(|&c| c == col_b);
// Only swap if both are visible
if let (Some(pos_a), Some(pos_b)) = (pos_a, pos_b) {
self.order.swap(pos_a, pos_b);
}
}
/// Hides a column by removing it from the display order.
/// If the column is already hidden or out of bounds, this is a no-op.
pub fn hide_column(&mut self, col: usize) {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
// Remove from order if present
self.order.retain(|&c| c != col);
}
/// Shows a column by inserting it into the display order.
/// If at_index is None, appends to the end.
/// If at_index is Some(idx), inserts at that position (saturated to valid range).
/// If the column is already visible or out of bounds, this is a no-op.
pub fn show_column(&mut self, col: usize, at_index: Option<usize>) {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
// If already visible, do nothing
if self.order.contains(&col) {
return;
}
// Insert at specified position or append
match at_index {
None => self.order.push(col),
Some(idx) => {
let insert_pos = idx.min(self.order.len());
self.order.insert(insert_pos, col);
}
}
}
/// Moves a column to a specific position in the display order (0-indexed).
/// The position is saturated to the valid range.
/// If the column is hidden or out of bounds, this is a no-op.
pub fn move_to(&mut self, col: usize, new_index: usize) {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
// Find current position
if let Some(current_pos) = self.order.iter().position(|&c| c == col) {
// Remove from current position
self.order.remove(current_pos);
// Insert at new position (saturated)
let insert_pos = new_index.min(self.order.len());
self.order.insert(insert_pos, col);
}
}
/// Moves a column one position forward (towards index 0) in the display order.
/// If the column is already first or hidden, this is a no-op.
pub fn move_forward(&mut self, col: usize) {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
if let Some(pos) = self.order.iter().position(|&c| c == col) && pos > 0 {
self.order.swap(pos, pos - 1);
}
}
/// Moves a column one position backward (towards the end) in the display order.
/// If the column is already last or hidden, this is a no-op.
pub fn move_backward(&mut self, col: usize) {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
if let Some(pos) = self.order.iter().position(|&c| c == col) && pos < self.order.len() - 1 {
self.order.swap(pos, pos + 1);
}
}
/// Checks if a column is currently visible
pub fn is_visible(&self, col: usize) -> bool {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
self.order.contains(&col)
}
/// Returns the display position of a column (0-indexed), or None if hidden
pub fn position(&self, col: usize) -> Option<usize> {
// Saturate to valid column index
let col = col.min(self.total_columns.saturating_sub(1));
self.order.iter().position(|&c| c == col)
}
/// Resets the column order to the default state (all columns visible in natural order)
pub fn reset(&mut self) {
self.order = (0..self.total_columns).collect();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let order = ColumnOrder::new(3);
assert_eq!(order.get_order(), &[0, 1, 2]);
assert_eq!(order.total_columns(), 3);
}
#[test]
fn test_swap() {
let mut order = ColumnOrder::new(3);
order.swap(0, 2);
assert_eq!(order.get_order(), &[2, 1, 0]);
}
#[test]
fn test_swap_saturating() {
let mut order = ColumnOrder::new(3);
order.swap(0, 100); // Should saturate to 2
assert_eq!(order.get_order(), &[2, 1, 0]);
}
#[test]
fn test_hide_show() {
let mut order = ColumnOrder::new(3);
order.hide_column(1);
assert_eq!(order.get_order(), &[0, 2]);
assert!(!order.is_visible(1));
order.show_column(1, None);
assert_eq!(order.get_order(), &[0, 2, 1]);
assert!(order.is_visible(1));
}
#[test]
fn test_show_at_index() {
let mut order = ColumnOrder::new(3);
order.hide_column(1);
order.show_column(1, Some(0));
assert_eq!(order.get_order(), &[1, 0, 2]);
}
#[test]
fn test_move_to() {
let mut order = ColumnOrder::new(3);
order.move_to(0, 2);
assert_eq!(order.get_order(), &[1, 2, 0]);
}
#[test]
fn test_move_forward_backward() {
let mut order = ColumnOrder::new(3);
order.move_backward(0);
assert_eq!(order.get_order(), &[1, 0, 2]);
order.move_forward(0);
assert_eq!(order.get_order(), &[0, 1, 2]);
}
#[test]
fn test_move_forward_at_start() {
let mut order = ColumnOrder::new(3);
order.move_forward(0);
assert_eq!(order.get_order(), &[0, 1, 2]); // No change
}
#[test]
fn test_move_backward_at_end() {
let mut order = ColumnOrder::new(3);
order.move_backward(2);
assert_eq!(order.get_order(), &[0, 1, 2]); // No change
}
#[test]
fn test_position() {
let mut order = ColumnOrder::new(3);
assert_eq!(order.position(1), Some(1));
order.hide_column(1);
assert_eq!(order.position(1), None);
}
#[test]
fn test_reset() {
let mut order = ColumnOrder::new(3);
// Make some changes
order.hide_column(1);
order.swap(0, 2);
assert_eq!(order.get_order(), &[2, 0]);
// Reset should restore default order
order.reset();
assert_eq!(order.get_order(), &[0, 1, 2]);
assert!(order.is_visible(1));
}
}