Skip to main content

glory_render/
document.rs

1//! Finite document structure, independent of a host tag or rendering engine.
2use crate::{Error, Props};
3
4/// Structural document parts are fixed for a live node's lifetime. Groups are
5/// transparent; lists contain items and description lists contain terms/values.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum DocumentPart {
8    Blockquote,
9    OrderedList,
10    UnorderedList,
11    ListItem,
12    DescriptionList,
13    Term,
14    Definition,
15    Table,
16    TableCaption,
17    TableHead,
18    TableBody,
19    TableFoot,
20    TableRow,
21    ColumnHeader,
22    RowHeader,
23    TableCell,
24    Strong,
25    Emphasis,
26    Strikethrough,
27}
28
29/// The parent is the nearest boxed parent, crossing only boxless Groups. A
30/// detached Plan may leave its root context open until actual live placement.
31pub(crate) fn validate_child(
32    child: crate::DescriptorView<'_>,
33    parent: Option<crate::DescriptorView<'_>>,
34    detached: bool,
35) -> Result<(), Error> {
36    if matches!(child.props, Props::Group) {
37        return Ok(());
38    }
39    let part = child.document_part();
40    let parent = parent.and_then(|d| d.document_part());
41    let valid = match (parent, part) {
42        (
43            Some(DocumentPart::Table),
44            Some(
45                DocumentPart::TableCaption
46                | DocumentPart::TableHead
47                | DocumentPart::TableBody
48                | DocumentPart::TableFoot,
49            ),
50        ) => true,
51        (Some(DocumentPart::Table), _) => false,
52        (
53            Some(DocumentPart::TableHead | DocumentPart::TableBody | DocumentPart::TableFoot),
54            Some(DocumentPart::TableRow),
55        ) => true,
56        (Some(DocumentPart::TableHead | DocumentPart::TableBody | DocumentPart::TableFoot), _) => {
57            false
58        }
59        (
60            Some(DocumentPart::TableRow),
61            Some(DocumentPart::ColumnHeader | DocumentPart::RowHeader | DocumentPart::TableCell),
62        ) => true,
63        (Some(DocumentPart::TableRow), _) => false,
64        (Some(DocumentPart::TableCaption), _) => matches!(child.props, Props::Text(_)),
65        (
66            _,
67            Some(
68                DocumentPart::TableCaption
69                | DocumentPart::TableHead
70                | DocumentPart::TableBody
71                | DocumentPart::TableFoot
72                | DocumentPart::TableRow
73                | DocumentPart::ColumnHeader
74                | DocumentPart::RowHeader
75                | DocumentPart::TableCell,
76            ),
77        ) => detached,
78
79        (
80            Some(DocumentPart::OrderedList | DocumentPart::UnorderedList),
81            Some(DocumentPart::ListItem),
82        ) => true,
83        (Some(DocumentPart::OrderedList | DocumentPart::UnorderedList), _) => false,
84        (
85            Some(DocumentPart::DescriptionList),
86            Some(DocumentPart::Term | DocumentPart::Definition),
87        ) => true,
88        (Some(DocumentPart::DescriptionList), _) => false,
89        (_, Some(DocumentPart::ListItem)) => detached,
90        (_, Some(DocumentPart::Term | DocumentPart::Definition)) => detached,
91        _ => true,
92    };
93    if valid {
94        Ok(())
95    } else {
96        Err(Error::InvalidPlacement(
97            "document child conflicts with its nearest boxed parent",
98        ))
99    }
100}