easydoc_core/document/
document_block.rs1use crate::{DocumentImage, DocumentList, DocumentTable, DocumentTextRun};
2
3#[derive(Clone, Debug, PartialEq)]
8#[non_exhaustive]
9pub enum DocumentBlock {
10 Heading {
12 level: u8,
14 runs: Vec<DocumentTextRun>,
16 },
17 Paragraph(Vec<DocumentTextRun>),
19 Table(DocumentTable),
21 List(DocumentList),
23 Image(DocumentImage),
25 ThematicBreak,
27 PageBreak,
29 ColumnBreak,
31 CodeBlock {
33 language: Option<String>,
35 code: String,
37 },
38 TextBox(Vec<DocumentBlock>),
40 Footnote {
42 id: u32,
44 blocks: Vec<DocumentBlock>,
46 },
47 Endnote {
49 id: u32,
51 blocks: Vec<DocumentBlock>,
53 },
54 Section {
56 blocks: Vec<DocumentBlock>,
58 section_type: Option<String>,
60 },
61 Math {
63 omml: Option<String>,
65 latex: Option<String>,
67 display: bool,
69 },
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn section_variant_roundtrip() {
78 let section = DocumentBlock::Section {
79 blocks: vec![DocumentBlock::Paragraph(vec![DocumentTextRun {
80 text: "hello".into(),
81 ..DocumentTextRun::default()
82 }])],
83 section_type: Some("nextPage".into()),
84 };
85 match §ion {
86 DocumentBlock::Section {
87 blocks,
88 section_type,
89 } => {
90 assert_eq!(blocks.len(), 1);
91 assert_eq!(section_type.as_deref(), Some("nextPage"));
92 }
93 _ => panic!("expected Section"),
94 }
95 }
96
97 #[test]
98 fn math_variant_display_and_inline() {
99 let display = DocumentBlock::Math {
100 omml: None,
101 latex: Some(r"\frac{1}{2}".into()),
102 display: true,
103 };
104 let inline = DocumentBlock::Math {
105 omml: Some("<m:oMath><m:r><m:t>x</m:t></m:r></m:oMath>".into()),
106 latex: None,
107 display: false,
108 };
109 match &display {
110 DocumentBlock::Math { latex, display, .. } => {
111 assert_eq!(latex.as_deref(), Some(r"\frac{1}{2}"));
112 assert!(*display);
113 }
114 _ => panic!("expected Math"),
115 }
116 match &inline {
117 DocumentBlock::Math { omml, display, .. } => {
118 assert!(omml.is_some());
119 assert!(!(*display));
120 }
121 _ => panic!("expected Math"),
122 }
123 }
124
125 #[test]
126 fn document_block_is_non_exhaustive() {
127 let block = DocumentBlock::ThematicBreak;
129 let desc = match block {
130 DocumentBlock::Heading { .. } => "heading",
131 DocumentBlock::Paragraph(_) => "paragraph",
132 DocumentBlock::Table(_) => "table",
133 DocumentBlock::List(_) => "list",
134 DocumentBlock::Image(_) => "image",
135 DocumentBlock::ThematicBreak => "break",
136 DocumentBlock::PageBreak => "page",
137 DocumentBlock::ColumnBreak => "column",
138 DocumentBlock::CodeBlock { .. } => "code",
139 DocumentBlock::TextBox(_) => "textbox",
140 DocumentBlock::Footnote { .. } => "footnote",
141 DocumentBlock::Endnote { .. } => "endnote",
142 DocumentBlock::Section { .. } => "section",
143 DocumentBlock::Math { .. } => "math",
144 };
145 assert_eq!(desc, "break");
146 }
147}