docx_reader/documents/elements/
table_of_contents.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use crate::documents::*;

#[derive(Debug, Clone, PartialEq)]
pub enum TocContent {
	Paragraph(Box<Paragraph>),
	Table(Box<Table>),
}

impl Serialize for TocContent {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			TocContent::Paragraph(ref p) => {
				let mut t = serializer.serialize_struct("Paragraph", 2)?;
				t.serialize_field("type", "paragraph")?;
				t.serialize_field("data", p)?;
				t.end()
			}
			TocContent::Table(ref c) => {
				let mut t = serializer.serialize_struct("Table", 2)?;
				t.serialize_field("type", "table")?;
				t.serialize_field("data", c)?;
				t.end()
			}
		}
	}
}

#[derive(Serialize, Debug, Clone, PartialEq, Default)]
pub struct TableOfContentsReviewData {
	pub author: String,
	pub date: String,
}

/// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_TOCTOC_topic_ID0ELZO1.html
/// This struct is only used by writers
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
pub struct TableOfContents {
	pub instr: InstrToC,
	pub items: Vec<TableOfContentsItem>,
	// don't use
	pub auto: bool,
	pub dirty: bool,
	/// Skip StructuredDataTag rendering
	pub without_sdt: bool,
	pub alias: Option<String>,
	pub page_ref_placeholder: Option<String>,
	// it is inserted in before toc.
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub before_contents: Vec<TocContent>,
	// it is inserted in after toc.
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub after_contents: Vec<TocContent>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub delete: Option<TableOfContentsReviewData>,
}

impl TableOfContents {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn with_instr_text(s: &str) -> Self {
		let instr = InstrToC::with_instr_text(s);
		Self {
			instr,
			..Self::default()
		}
	}

	pub fn heading_styles_range(mut self, start: usize, end: usize) -> Self {
		self.instr = self.instr.heading_styles_range(start, end);
		self
	}

	pub fn add_style_with_level(mut self, s: StyleWithLevel) -> Self {
		self.instr = self.instr.add_style_with_level(s);
		self
	}

	pub fn hyperlink(mut self) -> Self {
		self.instr = self.instr.hyperlink();
		self
	}

	pub fn alias(mut self, a: impl Into<String>) -> Self {
		self.alias = Some(a.into());
		self
	}

	pub fn delete(mut self, author: impl Into<String>, date: impl Into<String>) -> Self {
		self.delete = Some(TableOfContentsReviewData {
			author: author.into(),
			date: date.into(),
		});
		self
	}

	// pub fn tc_field_level_range(mut self, start: usize, end: usize) -> Self {
	//     self.instr = self.instr.tc_field_level_range(start, end);
	//     self
	// }

	pub fn add_item(mut self, t: TableOfContentsItem) -> Self {
		self.items.push(t);
		self
	}

	pub fn auto(mut self) -> Self {
		self.auto = true;
		self
	}

	pub fn dirty(mut self) -> Self {
		self.dirty = true;
		self
	}

	pub fn add_before_paragraph(mut self, p: Paragraph) -> Self {
		self.before_contents
			.push(TocContent::Paragraph(Box::new(p)));
		self
	}

	pub fn add_after_paragraph(mut self, p: Paragraph) -> Self {
		self.after_contents.push(TocContent::Paragraph(Box::new(p)));
		self
	}

	pub fn add_before_table(mut self, t: Table) -> Self {
		self.before_contents.push(TocContent::Table(Box::new(t)));
		self
	}

	pub fn add_after_table(mut self, t: Table) -> Self {
		self.after_contents.push(TocContent::Table(Box::new(t)));
		self
	}

	pub fn without_sdt(mut self) -> Self {
		self.without_sdt = true;
		self
	}
}