nmd_core/resource/
table.rs1use getset::{Getters, MutGetters, Setters};
2
3
4#[derive(Debug, Clone, Default)]
5pub enum TableCellAlignment {
6 Left,
7 #[default] Center,
8 Right
9}
10
11
12
13#[derive(Debug, Clone, Default)]
14pub enum TableCell<T> {
15 #[default] None,
16 ContentCell{content: T, alignment: TableCellAlignment}
17}
18
19
20
21#[derive(Debug, Clone, Getters, MutGetters, Setters)]
22pub struct Table<H, B, F> {
23
24 #[getset(get = "pub", get_mut = "pub", set = "pub")]
25 header: Option<Vec<TableCell<H>>>,
26
27 #[getset(get = "pub", get_mut = "pub", set = "pub")]
28 body: Vec<Vec<TableCell<B>>>,
29
30 #[getset(get = "pub", get_mut = "pub", set = "pub")]
31 footer: Option<Vec<TableCell<F>>>
32}
33
34impl<H, B, F> Table<H, B, F>
35where B: Into<H> + Into<F> {
36
37 pub fn new(header: Option<Vec<TableCell<H>>>, body: Vec<Vec<TableCell<B>>>, footer: Option<Vec<TableCell<F>>>) -> Self {
38 Self {
39 header,
40 body,
41 footer
42 }
43 }
44
45 pub fn new_empty() -> Self {
46 Self {
47 header: None,
48 body: Vec::new(),
49 footer: None
50 }
51 }
52
53 pub fn append_to_body(&mut self, row: Vec<TableCell<B>>) {
54
55 self.body.push(row);
56 }
57
58 pub fn shift_first_body_row_to_header(&mut self) {
59
60 let first_row = self.body.remove(0);
61
62 let mut header: Vec<TableCell<H>> = Vec::new();
63
64 for table_cell in first_row {
65
66 match table_cell {
67 TableCell::None => header.push(TableCell::None),
68 TableCell::ContentCell { content, alignment } => {
69 header.push(TableCell::ContentCell {
70 content: Into::<H>::into(content),
71 alignment
72 })
73 },
74 }
75 }
76
77 self.header = Some(header);
78
79 }
80
81 pub fn shift_last_body_row_to_footer(&mut self) {
82
83 let last_row = self.body.remove(self.body.len() - 1);
84
85 let mut footer: Vec<TableCell<F>> = Vec::new();
86
87 for table_cell in last_row {
88
89 match table_cell {
90 TableCell::None => footer.push(TableCell::None),
91 TableCell::ContentCell { content, alignment } => {
92 footer.push(TableCell::ContentCell {
93 content: Into::<F>::into(content),
94 alignment
95 })
96 },
97 }
98 }
99
100 self.footer = Some(footer);
101
102 }
103}