tui_treelistview/
model.rs1use std::hash::Hash;
2
3pub trait TreeModel {
10 type Id: Copy + Eq + Hash;
11
12 fn root(&self) -> Option<Self::Id>;
14 fn children(&self, id: Self::Id) -> &[Self::Id];
16 fn contains(&self, id: Self::Id) -> bool;
18 fn size_hint(&self) -> usize {
20 0
21 }
22}
23
24pub trait TreeFilter<T: TreeModel> {
26 fn is_match(&self, model: &T, id: T::Id) -> bool;
27}
28
29impl<T, F> TreeFilter<T> for F
30where
31 T: TreeModel,
32 F: Fn(&T, T::Id) -> bool,
33{
34 fn is_match(&self, model: &T, id: T::Id) -> bool {
35 self(model, id)
36 }
37}
38
39#[derive(Clone, Copy, Debug)]
40pub struct TreeFilterConfig {
41 pub enabled: bool,
42 pub auto_expand: bool,
43}
44
45impl TreeFilterConfig {
46 pub const fn disabled() -> Self {
47 Self {
48 enabled: false,
49 auto_expand: false,
50 }
51 }
52
53 pub const fn enabled() -> Self {
54 Self {
55 enabled: true,
56 auto_expand: true,
57 }
58 }
59}
60
61impl Default for TreeFilterConfig {
62 fn default() -> Self {
63 Self::disabled()
64 }
65}
66
67#[derive(Clone, Copy, Debug)]
68pub struct NoFilter;
69
70impl<T: TreeModel> TreeFilter<T> for NoFilter {
71 fn is_match(&self, _model: &T, _id: T::Id) -> bool {
72 true
73 }
74}