Skip to main content

ftml/tree/
table.rs

1/*
2 * tree/table.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2026 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21use super::clone::elements_to_owned;
22use super::{Alignment, AttributeMap, Element};
23use std::num::NonZeroU32;
24
25#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
26#[serde(rename_all = "kebab-case")]
27pub struct Table<'t> {
28    #[serde(rename = "type")]
29    pub table_type: TableType,
30    pub attributes: AttributeMap<'t>,
31    pub rows: Vec<TableRow<'t>>,
32}
33
34impl Table<'_> {
35    pub fn to_owned(&self) -> Table<'static> {
36        Table {
37            table_type: self.table_type,
38            attributes: self.attributes.to_owned(),
39            rows: self.rows.iter().map(|row| row.to_owned()).collect(),
40        }
41    }
42}
43
44#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
45#[serde(rename_all = "kebab-case")]
46pub enum TableType {
47    Simple,
48    Advanced,
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
52#[serde(rename_all = "kebab-case")]
53pub struct TableRow<'t> {
54    pub attributes: AttributeMap<'t>,
55    pub cells: Vec<TableCell<'t>>,
56}
57
58impl TableRow<'_> {
59    pub fn to_owned(&self) -> TableRow<'static> {
60        TableRow {
61            attributes: self.attributes.to_owned(),
62            cells: self.cells.iter().map(|cell| cell.to_owned()).collect(),
63        }
64    }
65}
66
67#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
68#[serde(rename_all = "kebab-case")]
69pub struct TableCell<'t> {
70    pub header: bool,
71    pub column_span: NonZeroU32,
72    pub align: Option<Alignment>,
73    pub attributes: AttributeMap<'t>,
74    pub elements: Vec<Element<'t>>,
75}
76
77impl TableCell<'_> {
78    pub fn to_owned(&self) -> TableCell<'static> {
79        TableCell {
80            header: self.header,
81            column_span: self.column_span,
82            align: self.align,
83            attributes: self.attributes.to_owned(),
84            elements: elements_to_owned(&self.elements),
85        }
86    }
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
90#[serde(rename_all = "kebab-case")]
91pub enum TableItem<'t> {
92    Row(TableRow<'t>),
93    Cell(TableCell<'t>),
94}
95
96impl TableItem<'_> {
97    pub fn to_owned(&self) -> TableItem<'static> {
98        match self {
99            TableItem::Row(row) => TableItem::Row(row.to_owned()),
100            TableItem::Cell(cell) => TableItem::Cell(cell.to_owned()),
101        }
102    }
103}