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}
25
26/// The parent is the nearest boxed parent, crossing only boxless Groups. A
27/// detached Plan may leave its root context open until actual live placement.
28pub(crate) fn validate_child(
29    child: crate::DescriptorView<'_>,
30    parent: Option<crate::DescriptorView<'_>>,
31    detached: bool,
32) -> Result<(), Error> {
33    if matches!(child.props, Props::Group) {
34        return Ok(());
35    }
36    let part = child.document_part();
37    let parent = parent.and_then(|d| d.document_part());
38    let valid = match (parent, part) {
39        (
40            Some(DocumentPart::Table),
41            Some(
42                DocumentPart::TableCaption
43                | DocumentPart::TableHead
44                | DocumentPart::TableBody
45                | DocumentPart::TableFoot,
46            ),
47        ) => true,
48        (Some(DocumentPart::Table), _) => false,
49        (
50            Some(DocumentPart::TableHead | DocumentPart::TableBody | DocumentPart::TableFoot),
51            Some(DocumentPart::TableRow),
52        ) => true,
53        (Some(DocumentPart::TableHead | DocumentPart::TableBody | DocumentPart::TableFoot), _) => {
54            false
55        }
56        (
57            Some(DocumentPart::TableRow),
58            Some(DocumentPart::ColumnHeader | DocumentPart::RowHeader | DocumentPart::TableCell),
59        ) => true,
60        (Some(DocumentPart::TableRow), _) => false,
61        (Some(DocumentPart::TableCaption), _) => matches!(child.props, Props::Text(_)),
62        (
63            _,
64            Some(
65                DocumentPart::TableCaption
66                | DocumentPart::TableHead
67                | DocumentPart::TableBody
68                | DocumentPart::TableFoot
69                | DocumentPart::TableRow
70                | DocumentPart::ColumnHeader
71                | DocumentPart::RowHeader
72                | DocumentPart::TableCell,
73            ),
74        ) => detached,
75
76        (
77            Some(DocumentPart::OrderedList | DocumentPart::UnorderedList),
78            Some(DocumentPart::ListItem),
79        ) => true,
80        (Some(DocumentPart::OrderedList | DocumentPart::UnorderedList), _) => false,
81        (
82            Some(DocumentPart::DescriptionList),
83            Some(DocumentPart::Term | DocumentPart::Definition),
84        ) => true,
85        (Some(DocumentPart::DescriptionList), _) => false,
86        (_, Some(DocumentPart::ListItem)) => detached,
87        (_, Some(DocumentPart::Term | DocumentPart::Definition)) => detached,
88        _ => true,
89    };
90    if valid {
91        Ok(())
92    } else {
93        Err(Error::InvalidPlacement(
94            "document child conflicts with its nearest boxed parent",
95        ))
96    }
97}