docx_reader/documents/elements/
text_box_content.rs

1use super::*;
2use serde::ser::{SerializeStruct, Serializer};
3use serde::Serialize;
4
5#[derive(Debug, Clone, Serialize, PartialEq)]
6pub struct TextBoxContent {
7	pub children: Vec<TextBoxContentChild>,
8	pub has_numbering: bool,
9}
10
11#[derive(Debug, Clone, PartialEq)]
12pub enum TextBoxContentChild {
13	Paragraph(Box<Paragraph>),
14	Table(Box<Table>),
15}
16
17impl Serialize for TextBoxContentChild {
18	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19	where
20		S: Serializer,
21	{
22		match *self {
23			TextBoxContentChild::Paragraph(ref p) => {
24				let mut t = serializer.serialize_struct("Paragraph", 2)?;
25				t.serialize_field("type", "paragraph")?;
26				t.serialize_field("data", p)?;
27				t.end()
28			}
29			TextBoxContentChild::Table(ref c) => {
30				let mut t = serializer.serialize_struct("Table", 2)?;
31				t.serialize_field("type", "table")?;
32				t.serialize_field("data", c)?;
33				t.end()
34			}
35		}
36	}
37}
38
39impl TextBoxContent {
40	pub fn new() -> TextBoxContent {
41		Default::default()
42	}
43
44	pub fn add_paragraph(mut self, p: Paragraph) -> Self {
45		if p.has_numbering {
46			self.has_numbering = true
47		}
48		self.children
49			.push(TextBoxContentChild::Paragraph(Box::new(p)));
50		self
51	}
52
53	pub fn add_table(mut self, t: Table) -> Self {
54		if t.has_numbering {
55			self.has_numbering = true
56		}
57		self.children.push(TextBoxContentChild::Table(Box::new(t)));
58		self
59	}
60}
61
62impl Default for TextBoxContent {
63	fn default() -> Self {
64		TextBoxContent {
65			children: vec![],
66			has_numbering: false,
67		}
68	}
69}