ftml/tree/
partial.rs

1/*
2 * tree/partial.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2025 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::{ListItem, RubyText, Tab, TableCell, TableRow};
22use crate::parsing::ParseErrorKind;
23
24/// Part of an element, as returned by a rule.
25///
26/// These are used by specific rules attempting to
27/// build complex or nested structures. From any other
28/// context, they are errors are parsing will fail.
29#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
30pub enum PartialElement<'t> {
31    /// An item or sub-list within some list.
32    ListItem(ListItem<'t>),
33
34    /// A row within some table.
35    TableRow(TableRow<'t>),
36
37    /// A cell within some table row.
38    TableCell(TableCell<'t>),
39
40    /// A particular tab within a tab view.
41    Tab(Tab<'t>),
42
43    /// Text associated with a Ruby annotation.
44    ///
45    /// Outputs HTML `<rt>`. See also <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby>.
46    RubyText(RubyText<'t>),
47}
48
49impl PartialElement<'_> {
50    pub fn name(&self) -> &'static str {
51        match self {
52            PartialElement::ListItem(_) => "ListItem",
53            PartialElement::TableRow(_) => "TableRow",
54            PartialElement::TableCell(_) => "TableCell",
55            PartialElement::Tab(_) => "Tab",
56            PartialElement::RubyText(_) => "RubyText",
57        }
58    }
59
60    #[inline]
61    pub fn parse_error_kind(&self) -> ParseErrorKind {
62        match self {
63            PartialElement::ListItem(_) => ParseErrorKind::ListItemOutsideList,
64            PartialElement::TableRow(_) => ParseErrorKind::TableRowOutsideTable,
65            PartialElement::TableCell(_) => ParseErrorKind::TableCellOutsideTable,
66            PartialElement::Tab(_) => ParseErrorKind::TabOutsideTabView,
67            PartialElement::RubyText(_) => ParseErrorKind::RubyTextOutsideRuby,
68        }
69    }
70
71    pub fn to_owned(&self) -> PartialElement<'static> {
72        match self {
73            PartialElement::ListItem(list_item) => {
74                PartialElement::ListItem(list_item.to_owned())
75            }
76            PartialElement::TableRow(table_row) => {
77                PartialElement::TableRow(table_row.to_owned())
78            }
79            PartialElement::TableCell(table_cell) => {
80                PartialElement::TableCell(table_cell.to_owned())
81            }
82            PartialElement::Tab(tab) => PartialElement::Tab(tab.to_owned()),
83            PartialElement::RubyText(text) => PartialElement::RubyText(text.to_owned()),
84        }
85    }
86}
87
88/// A marker enum counterpart to `PartialElement`.
89///
90/// This is a flag to the parser which designates which
91/// partial (if any) the rule is currently looking to accept.
92#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
93pub enum AcceptsPartial {
94    #[default]
95    None,
96    ListItem,
97    TableRow,
98    TableCell,
99    Tab,
100    Ruby,
101}
102
103impl AcceptsPartial {
104    pub fn matches(self, partial: &PartialElement) -> bool {
105        matches!(
106            (self, partial),
107            (AcceptsPartial::ListItem, PartialElement::ListItem(_))
108                | (AcceptsPartial::TableRow, PartialElement::TableRow(_))
109                | (AcceptsPartial::TableCell, PartialElement::TableCell(_))
110                | (AcceptsPartial::Tab, PartialElement::Tab(_))
111                | (AcceptsPartial::Ruby, PartialElement::RubyText(_))
112        )
113    }
114}