1use super::{ListItem, RubyText, Tab, TableCell, TableRow};
22use crate::parsing::ParseErrorKind;
23
24#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
30pub enum PartialElement<'t> {
31 ListItem(ListItem<'t>),
33
34 TableRow(TableRow<'t>),
36
37 TableCell(TableCell<'t>),
39
40 Tab(Tab<'t>),
42
43 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#[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}