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
mod hook;
mod memoized;
mod state;
mod table;

pub use hook::*;
pub use memoized::*;
pub use state::*;
use std::fmt::Debug;
pub use table::*;

use super::TableEntryRenderer;
use std::rc::Rc;
use yew::virtual_dom::Key;

/// A model providing data for a table.
pub trait TableModel<C>
where
    C: Clone + Eq + 'static,
{
    type Iterator<'i>: Iterator<Item = TableModelEntry<'i, Self::Item, Self::Key, C>>
    where
        Self: 'i;
    type Item: TableEntryRenderer<C> + Clone + 'static;
    type Key: Into<Key> + Clone + Debug + Eq + 'static;

    /// Get the number of items
    fn len(&self) -> usize;

    /// Test if the table model is empty
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Iterate over all the items
    fn iter(&self) -> Self::Iterator<'_>;
}

impl<C, M> TableModel<C> for Rc<M>
where
    C: Clone + Eq + 'static,
    M: TableModel<C> + 'static,
{
    type Iterator<'i> = M::Iterator<'i>;
    type Item = M::Item;
    type Key = M::Key;

    fn len(&self) -> usize {
        self.as_ref().len()
    }

    fn is_empty(&self) -> bool {
        self.as_ref().is_empty()
    }

    fn iter(&self) -> Self::Iterator<'_> {
        self.as_ref().iter()
    }
}

pub trait TableDataModel<C>
where
    C: Clone + Eq + 'static,
{
    type Iterator<'i>: Iterator<Item = (Self::Key, &'i Self::Item)>
    where
        Self: 'i;
    type Item: TableEntryRenderer<C> + Clone + 'static;
    type Key: Into<Key> + Clone + Debug + Eq + 'static;

    /// Get the number of items
    fn len(&self) -> usize;

    /// Test if the model is empty
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Test if the model contains the key
    fn contains(&self, key: &Self::Key) -> bool;

    /// Iterate over all the items
    fn iter(&self) -> Self::Iterator<'_>;
}

impl<C, M> TableDataModel<C> for Rc<M>
where
    C: Clone + Eq + 'static,
    M: TableDataModel<C> + 'static,
{
    type Iterator<'i> = M::Iterator<'i>;
    type Item = M::Item;
    type Key = M::Key;

    fn len(&self) -> usize {
        self.as_ref().len()
    }

    fn is_empty(&self) -> bool {
        self.as_ref().is_empty()
    }

    fn contains(&self, key: &Self::Key) -> bool {
        self.as_ref().contains(key)
    }

    fn iter(&self) -> Self::Iterator<'_> {
        self.as_ref().iter()
    }
}

pub struct TableModelEntry<'t, T, K, C>
where
    K: Into<Key>,
    C: Clone + Eq,
{
    pub value: &'t T,
    pub key: K,
    pub expansion: Option<ExpansionState<C>>,
}