BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / card_table.rs

card_table.rs

card_table.rs 5.2 KiB Download Raw
1use serde_json::Value;
2
3use crate::adaptive_card::{
4 CardAction, CardElement, CardItem, ColumnWidth, ContainerStyle, VerticalAlignment,
5 parse_column_width, parse_container_style, parse_items, parse_select_action,
6 parse_vertical_alignment, string_field,
7};
8use crate::card_layout::{HorizontalAlignment, parse_horizontal_alignment};
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct CardTable {
12 pub columns: Vec<TableColumn>,
13 pub rows: Vec<TableRow>,
14 pub first_row_as_header: bool,
15 pub show_grid_lines: bool,
16 pub grid_style: ContainerStyle,
17 pub horizontal_alignment: Option<HorizontalAlignment>,
18 pub vertical_alignment: Option<VerticalAlignment>,
19}
20
21#[derive(Debug, Clone, PartialEq)]
22pub struct TableColumn {
23 pub width: ColumnWidth,
24 pub horizontal_alignment: Option<HorizontalAlignment>,
25 pub vertical_alignment: Option<VerticalAlignment>,
26}
27
28#[derive(Debug, Clone, PartialEq)]
29pub struct TableRow {
30 pub cells: Vec<TableCell>,
31 pub style: ContainerStyle,
32 pub horizontal_alignment: Option<HorizontalAlignment>,
33 pub vertical_alignment: Option<VerticalAlignment>,
34}
35
36#[derive(Debug, Clone, PartialEq)]
37pub struct TableCell {
38 pub items: Vec<CardItem>,
39 pub style: ContainerStyle,
40 pub vertical_alignment: Option<VerticalAlignment>,
41 pub select_action: Option<CardAction>,
42}
43
44impl CardTable {
45 pub fn cell_horizontal_alignment(
46 &self,
47 row: &TableRow,
48 column_index: usize,
49 ) -> HorizontalAlignment {
50 row.horizontal_alignment
51 .or_else(|| self.columns.get(column_index)?.horizontal_alignment)
52 .or(self.horizontal_alignment)
53 .unwrap_or_default()
54 }
55
56 pub fn cell_vertical_alignment(
57 &self,
58 row: &TableRow,
59 cell: &TableCell,
60 column_index: usize,
61 ) -> VerticalAlignment {
62 cell.vertical_alignment
63 .or(row.vertical_alignment)
64 .or_else(|| self.columns.get(column_index)?.vertical_alignment)
65 .or(self.vertical_alignment)
66 .unwrap_or_default()
67 }
68}
69
70pub(crate) fn parse_table(value: &Value) -> Option<CardElement> {
71 let rows: Vec<TableRow> = value
72 .get("rows")?
73 .as_array()?
74 .iter()
75 .filter_map(parse_row)
76 .collect();
77 if rows.is_empty() {
78 return None;
79 }
80 let column_count = rows.iter().map(|row| row.cells.len()).max().unwrap_or(0);
81 let declared: Vec<TableColumn> = value
82 .get("columns")
83 .and_then(Value::as_array)
84 .into_iter()
85 .flatten()
86 .map(parse_column)
87 .collect();
88 let columns = (0..column_count.max(declared.len()))
89 .map(|index| declared.get(index).cloned().unwrap_or_else(default_column))
90 .collect();
91 Some(CardElement::Table(CardTable {
92 columns,
93 rows,
94 first_row_as_header: value
95 .get("firstRowAsHeader")
96 .and_then(Value::as_bool)
97 .unwrap_or(true),
98 show_grid_lines: value
99 .get("showGridLines")
100 .and_then(Value::as_bool)
101 .unwrap_or(true),
102 grid_style: parse_container_style(string_field(value, "gridStyle")),
103 horizontal_alignment: parse_horizontal_alignment(string_field(
104 value,
105 "horizontalCellContentAlignment",
106 )),
107 vertical_alignment: parse_optional_vertical(value, "verticalCellContentAlignment"),
108 }))
109}
110
111fn default_column() -> TableColumn {
112 TableColumn {
113 width: ColumnWidth::Weighted(1.),
114 horizontal_alignment: None,
115 vertical_alignment: None,
116 }
117}
118
119fn parse_column(value: &Value) -> TableColumn {
120 let width = value
121 .get("width")
122 .map_or(ColumnWidth::Weighted(1.), |width| {
123 parse_column_width(Some(width))
124 });
125 TableColumn {
126 width,
127 horizontal_alignment: parse_horizontal_alignment(string_field(
128 value,
129 "horizontalCellContentAlignment",
130 )),
131 vertical_alignment: parse_optional_vertical(value, "verticalCellContentAlignment"),
132 }
133}
134
135fn parse_row(value: &Value) -> Option<TableRow> {
136 if value.get("isVisible").and_then(Value::as_bool) == Some(false) {
137 return None;
138 }
139 let cells: Vec<TableCell> = value
140 .get("cells")?
141 .as_array()?
142 .iter()
143 .map(parse_cell)
144 .collect();
145 (!cells.is_empty()).then(|| TableRow {
146 cells,
147 style: parse_container_style(string_field(value, "style")),
148 horizontal_alignment: parse_horizontal_alignment(string_field(
149 value,
150 "horizontalCellContentAlignment",
151 )),
152 vertical_alignment: parse_optional_vertical(value, "verticalCellContentAlignment"),
153 })
154}
155
156fn parse_cell(value: &Value) -> TableCell {
157 TableCell {
158 items: parse_items(value.get("items")),
159 style: parse_container_style(string_field(value, "style")),
160 vertical_alignment: parse_optional_vertical(value, "verticalContentAlignment"),
161 select_action: parse_select_action(value),
162 }
163}
164
165fn parse_optional_vertical(value: &Value, key: &str) -> Option<VerticalAlignment> {
166 string_field(value, key)?;
167 Some(parse_vertical_alignment(string_field(value, key)))
168}