tui_treelistview/
model.rs1use std::hash::Hash;
2
3pub trait TreeModel {
10 type Id: Copy + Eq + Hash;
12
13 fn root(&self) -> Option<Self::Id>;
15 fn children(&self, id: Self::Id) -> &[Self::Id];
17 fn contains(&self, id: Self::Id) -> bool;
19 fn size_hint(&self) -> usize {
21 0
22 }
23}
24
25pub trait TreeFilter<T: TreeModel> {
27 fn is_match(&self, model: &T, id: T::Id) -> bool;
29}
30
31impl<T, F> TreeFilter<T> for F
32where
33 T: TreeModel,
34 F: Fn(&T, T::Id) -> bool,
35{
36 #[inline]
37 fn is_match(&self, model: &T, id: T::Id) -> bool {
38 self(model, id)
39 }
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum TreeFilterConfig {
45 Disabled,
47 Enabled { auto_expand: bool },
49}
50
51impl TreeFilterConfig {
52 #[must_use]
54 pub const fn disabled() -> Self {
55 Self::Disabled
56 }
57
58 #[must_use]
60 pub const fn enabled() -> Self {
61 Self::Enabled { auto_expand: true }
62 }
63
64 #[must_use]
66 pub const fn enabled_manual_expand() -> Self {
67 Self::Enabled { auto_expand: false }
68 }
69}
70
71impl Default for TreeFilterConfig {
72 fn default() -> Self {
73 Self::disabled()
74 }
75}
76
77#[derive(Clone, Copy, Debug)]
79pub struct NoFilter;
80
81impl<T: TreeModel> TreeFilter<T> for NoFilter {
82 #[inline]
83 fn is_match(&self, _model: &T, _id: T::Id) -> bool {
84 true
85 }
86}