docx_reader/documents/
content_types.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::io::Read;
use xml::reader::{EventReader, XmlEvent};

use crate::reader::{FromXML, ReaderError};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ContentTypes {
	types: BTreeMap<String, String>,
	web_extension_count: usize,
	custom_xml_count: usize,
	header_count: usize,
	footer_count: usize,
}

impl ContentTypes {
	pub fn new() -> ContentTypes {
		Default::default()
	}

	pub fn add_content(mut self, path: impl Into<String>, namespace: impl Into<String>) -> Self {
		self.types.insert(path.into(), namespace.into());
		self
	}

	pub fn set_default(mut self) -> ContentTypes {
		self.types.insert(
			"/_rels/.rels".to_owned(),
			"application/vnd.openxmlformats-package.relationships+xml".to_owned(),
		);
		self.types.insert(
			"/docProps/app.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.extended-properties+xml".to_owned(),
		);
		self.types.insert(
			"/docProps/core.xml".to_owned(),
			"application/vnd.openxmlformats-package.core-properties+xml".to_owned(),
		);
		self.types.insert(
			"/word/_rels/document.xml.rels".to_owned(),
			"application/vnd.openxmlformats-package.relationships+xml".to_owned(),
		);
		self.types.insert(
			"/word/settings.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"
				.to_owned(),
		);
		self.types.insert(
			"/word/fontTable.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"
				.to_owned(),
		);
		self.types.insert(
			"/word/document.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
				.to_owned(),
		);
		self.types.insert(
			"/word/styles.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml".to_owned(),
		);
		self.types.insert(
			"/word/comments.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"
				.to_owned(),
		);
		self.types.insert(
			"/word/numbering.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"
				.to_owned(),
		);
		self.types.insert(
			"/word/commentsExtended.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml"
				.to_owned(),
		);
		self.types.insert(
			"/docProps/custom.xml".to_owned(),
			"application/vnd.openxmlformats-officedocument.custom-properties+xml".to_owned(),
		);
		self
	}

	pub fn add_taskpanes(mut self) -> Self {
		self.types.insert(
			"/word/webextensions/taskpanes.xml".to_owned(),
			"application/vnd.ms-office.webextensiontaskpanes+xml".to_owned(),
		);
		self
	}

	pub fn add_web_extensions(mut self) -> Self {
		self.types.insert(
			format!(
				"/word/webextensions/webextension{}.xml",
				self.web_extension_count
			),
			"application/vnd.ms-office.webextension+xml".to_owned(),
		);
		self.web_extension_count += 1;
		self
	}

	pub fn add_custom_xml(mut self) -> Self {
		self.types.insert(
			format!("/customXml/itemProps{}.xml", self.web_extension_count),
			"application/vnd.openxmlformats-officedocument.customXmlProperties+xml".to_owned(),
		);
		self.custom_xml_count += 1;
		self
	}

	pub fn add_header(mut self) -> Self {
		self.header_count += 1;
		self.types.insert(
			format!("/word/header{}.xml", self.header_count),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml".to_owned(),
		);
		self
	}

	pub fn add_footer(mut self) -> Self {
		self.footer_count += 1;
		self.types.insert(
			format!("/word/footer{}.xml", self.footer_count),
			"application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml".to_owned(),
		);
		self
	}
}

impl Default for ContentTypes {
	fn default() -> Self {
		ContentTypes {
			types: BTreeMap::new(),
			web_extension_count: 1,
			custom_xml_count: 1,
			header_count: 0,
			footer_count: 0,
		}
	}
}

impl FromXML for ContentTypes {
	fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
		let parser = EventReader::new(reader);
		let mut s = Self::default();
		let mut depth = 0;
		for e in parser {
			match e {
				Ok(XmlEvent::StartElement { attributes, .. }) => {
					if depth == 1 {
						let namespace = attributes[0].value.clone();
						let path = attributes[1].value.clone();
						s = s.add_content(path, namespace);
					}
					depth += 1;
				}
				Ok(XmlEvent::EndElement { .. }) => {
					depth -= 1;
				}
				Err(_) => {}
				_ => {}
			}
		}
		Ok(s)
	}
}