1use crate::{DescriptorView, DocumentPart, Error, Props, Role, SemanticId};
3
4pub const MAX_CELL_HEADERS: usize = 32;
5pub const MAX_TABLE_SPAN: u16 = 1000;
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum SortDirection {
9 None,
10 Ascending,
11 Descending,
12}
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub enum TableMetadata {
15 Table {
16 rows: Option<u32>,
17 columns: Option<u16>,
18 },
19 Row {
20 index: u32,
21 },
22 Cell {
23 row: u32,
24 column: u16,
25 row_span: u16,
26 column_span: u16,
27 headers: Vec<SemanticId>,
28 },
29}
30impl TableMetadata {
31 pub(crate) fn validate(&self, descriptor: DescriptorView<'_>) -> Result<(), Error> {
32 let part = descriptor.document_part();
33 let role = descriptor.role;
34 let structural = matches!(descriptor.props, Props::View);
35 let valid = match self {
36 Self::Table { rows, columns } => {
37 structural
38 && (part == Some(DocumentPart::Table)
39 || matches!(role, Some(Role::Table | Role::Grid)))
40 && rows.is_none_or(|value| value < u32::MAX)
41 && columns.is_none_or(|value| value > 0)
42 }
43 Self::Row { index } => {
44 structural
45 && (part == Some(DocumentPart::TableRow) || role == Some(Role::Row))
46 && *index < u32::MAX
47 }
48 Self::Cell {
49 row,
50 column,
51 row_span,
52 column_span,
53 headers,
54 } => {
55 structural
56 && (matches!(
57 part,
58 Some(
59 DocumentPart::TableCell
60 | DocumentPart::ColumnHeader
61 | DocumentPart::RowHeader
62 )
63 ) || matches!(role, Some(Role::Cell | Role::ColumnHeader | Role::GridCell)))
64 && (1..=MAX_TABLE_SPAN).contains(row_span)
65 && (1..=MAX_TABLE_SPAN).contains(column_span)
66 && row.checked_add(u32::from(*row_span)).is_some()
67 && column.checked_add(*column_span).is_some()
68 && headers.len() <= MAX_CELL_HEADERS
69 && (part.is_some() || headers.is_empty())
70 && headers
71 .iter()
72 .enumerate()
73 .all(|(i, value)| !headers[..i].contains(value))
74 }
75 };
76 if valid {
77 Ok(())
78 } else {
79 Err(Error::InvalidProps(
80 "table metadata conflicts with its document part or finite coordinates",
81 ))
82 }
83 }
84}