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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use crate::structured::RowOperation;
/// A single visible-or-collapsible row in a tree view.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Row {
/// Depth from the root node. Used for indentation when rendering.
pub depth: usize,
/// Display label of the current node.
pub id: String,
/// Breadcrumb-like labels from the root to this node.
pub path: Vec<String>,
/// Whether this row has child nodes.
pub has_children: bool,
/// Whether the children of this row are currently collapsed.
pub collapsed: bool,
}
/// Adapts an arbitrary tree-shaped data source into rows.
pub trait Adapter {
/// Input node type used by the adapted tree source.
type Node;
/// Error returned while reading node metadata or children.
type Error;
/// Returns the display label for the given node.
fn id_of(&self, node: &Self::Node) -> Result<String, Self::Error>;
/// Returns the direct children of the given node.
fn children_of(&self, node: &Self::Node) -> Result<Vec<Self::Node>, Self::Error>;
/// Creates tree rows from the given root node.
///
/// Parent rows are emitted before their descendants, and rows with children
/// start in the collapsed state by default.
fn create_rows(&self, root: &Self::Node) -> Result<Vec<Row>, Self::Error> {
let mut rows = Vec::new();
let mut current_path = Vec::new();
collect_rows_with(root, 0, &mut current_path, &mut rows, self)?;
Ok(rows)
}
}
fn collect_rows_with<T, E, A>(
input: &T,
depth: usize,
current_path: &mut Vec<String>,
rows: &mut Vec<Row>,
adapter: &A,
) -> Result<(), E>
where
A: Adapter<Node = T, Error = E> + ?Sized,
{
let id = adapter.id_of(input)?;
let children = adapter.children_of(input)?;
let has_children = !children.is_empty();
current_path.push(id.clone());
rows.push(Row {
depth,
id,
path: current_path.clone(),
has_children,
collapsed: has_children,
});
if has_children {
for child in &children {
collect_rows_with(child, depth + 1, current_path, rows, adapter)?;
}
}
current_path.pop();
Ok(())
}
fn is_visible(rows: &[Row], index: usize) -> bool {
if index >= rows.len() {
return false;
}
let mut ancestor_depth = rows[index].depth;
for row in rows[..index].iter().rev() {
if row.depth < ancestor_depth {
if row.has_children && row.collapsed {
return false;
}
ancestor_depth = row.depth;
if ancestor_depth == 0 {
break;
}
}
}
true
}
impl RowOperation for Vec<Row> {
type Row = Row;
fn up(&self, current: usize) -> usize {
if self.is_empty() || current == 0 {
return 0;
}
let mut prev = current - 1;
loop {
if is_visible(self, prev) {
return prev;
}
if prev == 0 {
return current;
}
prev -= 1;
}
}
fn head(&self) -> usize {
self.iter()
.enumerate()
.find_map(|(index, _)| is_visible(self, index).then_some(index))
.unwrap_or(0)
}
fn down(&self, current: usize) -> usize {
if self.is_empty() || current >= self.len().saturating_sub(1) {
return current;
}
let mut next = current + 1;
while next < self.len() {
if is_visible(self, next) {
return next;
}
next += 1;
}
current
}
fn tail(&self) -> usize {
self.iter()
.enumerate()
.rev()
.find_map(|(index, _)| is_visible(self, index).then_some(index))
.unwrap_or(0)
}
fn toggle(&mut self, current: usize) -> usize {
let Some(row) = self.get_mut(current) else {
return current;
};
if row.has_children {
row.collapsed = !row.collapsed;
}
current
}
fn set_rows_visibility(&mut self, collapsed: bool) {
for row in self.iter_mut() {
if row.has_children {
row.collapsed = collapsed;
}
}
}
fn extract(&self, current: usize, n: usize) -> Vec<Row> {
let mut result = Vec::new();
let mut index = current;
while index < self.len() && result.len() < n {
if is_visible(self, index) {
result.push(self[index].clone());
}
index += 1;
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone)]
struct TestNode {
id: &'static str,
children: Vec<TestNode>,
}
struct TestAdapter;
impl Adapter for TestAdapter {
type Node = TestNode;
type Error = std::convert::Infallible;
fn id_of(&self, node: &Self::Node) -> Result<String, Self::Error> {
Ok(node.id.to_string())
}
fn children_of(&self, node: &Self::Node) -> Result<Vec<Self::Node>, Self::Error> {
Ok(node.children.clone())
}
}
fn create_test_rows() -> Vec<Row> {
vec![
Row {
depth: 0,
id: "root".into(),
path: vec!["root".into()],
has_children: true,
collapsed: false,
},
Row {
depth: 1,
id: "a".into(),
path: vec!["root".into(), "a".into()],
has_children: true,
collapsed: false,
},
Row {
depth: 2,
id: "aa".into(),
path: vec!["root".into(), "a".into(), "aa".into()],
has_children: false,
collapsed: false,
},
Row {
depth: 2,
id: "ab".into(),
path: vec!["root".into(), "a".into(), "ab".into()],
has_children: false,
collapsed: false,
},
Row {
depth: 1,
id: "b".into(),
path: vec!["root".into(), "b".into()],
has_children: false,
collapsed: false,
},
]
}
mod row_operation {
use super::*;
mod extract {
use super::*;
#[test]
fn skips_hidden_descendants() {
let mut rows = create_test_rows();
rows[0].collapsed = true;
assert_eq!(
rows.extract(0, 5),
vec![Row {
depth: 0,
id: "root".into(),
path: vec!["root".into()],
has_children: true,
collapsed: true,
}]
);
}
}
mod down {
use super::*;
#[test]
fn skips_hidden_descendants() {
let mut rows = create_test_rows();
rows[1].collapsed = true;
assert_eq!(rows.down(1), 4);
}
}
}
mod adapter {
use super::*;
mod create_rows {
use super::*;
#[test]
fn supports_arbitrary_node_types() {
let root = TestNode {
id: "root",
children: vec![
TestNode {
id: "a",
children: vec![
TestNode {
id: "aa",
children: vec![],
},
TestNode {
id: "ab",
children: vec![],
},
],
},
TestNode {
id: "b",
children: vec![],
},
],
};
let rows = TestAdapter.create_rows(&root).unwrap();
assert_eq!(
rows,
vec![
Row {
depth: 0,
id: "root".into(),
path: vec!["root".into()],
has_children: true,
collapsed: true,
},
Row {
depth: 1,
id: "a".into(),
path: vec!["root".into(), "a".into()],
has_children: true,
collapsed: true,
},
Row {
depth: 2,
id: "aa".into(),
path: vec!["root".into(), "a".into(), "aa".into()],
has_children: false,
collapsed: false,
},
Row {
depth: 2,
id: "ab".into(),
path: vec!["root".into(), "a".into(), "ab".into()],
has_children: false,
collapsed: false,
},
Row {
depth: 1,
id: "b".into(),
path: vec!["root".into(), "b".into()],
has_children: false,
collapsed: false,
},
]
);
}
}
}
}