docx_reader/documents/elements/
run.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use super::*;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use crate::types::*;

#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Run {
	pub run_property: RunProperty,
	pub children: Vec<RunChild>,
}

impl Default for Run {
	fn default() -> Self {
		let run_property = RunProperty::new();
		Self {
			run_property,
			children: vec![],
		}
	}
}

#[derive(Debug, Clone, PartialEq)]
pub enum RunChild {
	Text(Text),
	Sym(Sym),
	DeleteText(DeleteText),
	Tab(Tab),
	Break(Break),
	Drawing(Box<Drawing>),
	Shape(Box<Shape>),
	FieldChar(FieldChar),
	InstrText(Box<InstrText>),
	DeleteInstrText(Box<DeleteInstrText>),
	// For reader
	InstrTextString(String),
}

impl Serialize for RunChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			RunChild::Text(ref s) => {
				let mut t = serializer.serialize_struct("Text", 2)?;
				t.serialize_field("type", "text")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::Sym(ref s) => {
				let mut t = serializer.serialize_struct("Sym", 2)?;
				t.serialize_field("type", "sym")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::DeleteText(ref s) => {
				let mut t = serializer.serialize_struct("DeleteText", 2)?;
				t.serialize_field("type", "deleteText")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::Tab(_) => {
				let mut t = serializer.serialize_struct("Tab", 1)?;
				t.serialize_field("type", "tab")?;
				t.end()
			}
			RunChild::Break(ref s) => {
				let mut t = serializer.serialize_struct("Break", 2)?;
				t.serialize_field("type", "break")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::Drawing(ref s) => {
				let mut t = serializer.serialize_struct("Drawing", 2)?;
				t.serialize_field("type", "drawing")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::Shape(ref s) => {
				let mut t = serializer.serialize_struct("Shape", 2)?;
				t.serialize_field("type", "shape")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			RunChild::FieldChar(ref f) => {
				let mut t = serializer.serialize_struct("FieldChar", 2)?;
				t.serialize_field("type", "fieldChar")?;
				t.serialize_field("data", f)?;
				t.end()
			}
			RunChild::InstrText(ref i) => {
				let mut t = serializer.serialize_struct("InstrText", 2)?;
				t.serialize_field("type", "instrText")?;
				t.serialize_field("data", i)?;
				t.end()
			}
			RunChild::DeleteInstrText(ref i) => {
				let mut t = serializer.serialize_struct("DeleteInstrText", 2)?;
				t.serialize_field("type", "deleteInstrText")?;
				t.serialize_field("data", i)?;
				t.end()
			}
			RunChild::InstrTextString(ref i) => {
				let mut t = serializer.serialize_struct("InstrTextString", 2)?;
				t.serialize_field("type", "instrTextString")?;
				t.serialize_field("data", i)?;
				t.end()
			}
		}
	}
}

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

	pub fn add_text(mut self, text: impl Into<String>) -> Run {
		self.children
			.push(RunChild::Text(Text::new(text.into().replace('\n', ""))));
		self
	}

	pub(crate) fn add_text_without_escape(mut self, text: impl Into<String>) -> Run {
		self.children.push(RunChild::Text(Text::without_escape(
			text.into().replace('\n', ""),
		)));
		self
	}

	pub fn add_delete_text(mut self, text: impl Into<String>) -> Run {
		self.children.push(RunChild::DeleteText(DeleteText::new(
			text.into().replace('\n', ""),
		)));
		self
	}

	pub(crate) fn add_delete_text_without_escape(mut self, text: impl Into<String>) -> Run {
		self.children
			.push(RunChild::DeleteText(DeleteText::without_escape(
				text.into().replace('\n', ""),
			)));
		self
	}

	pub fn add_field_char(mut self, t: crate::types::FieldCharType, dirty: bool) -> Run {
		let mut f = FieldChar::new(t);
		if dirty {
			f = f.dirty();
		};
		self.children.push(RunChild::FieldChar(f));
		self
	}

	pub fn add_instr_text(mut self, i: InstrText) -> Run {
		self.children.push(RunChild::InstrText(Box::new(i)));
		self
	}

	pub fn add_delete_instr_text(mut self, i: DeleteInstrText) -> Run {
		self.children.push(RunChild::DeleteInstrText(Box::new(i)));
		self
	}

	pub fn add_tab(mut self) -> Run {
		self.children.push(RunChild::Tab(Tab::new()));
		self
	}

	pub fn add_image(mut self, pic: Pic) -> Run {
		self.children
			.push(RunChild::Drawing(Box::new(Drawing::new().pic(pic))));
		self
	}

	pub(crate) fn add_drawing(mut self, d: Drawing) -> Run {
		self.children.push(RunChild::Drawing(Box::new(d)));
		self
	}

	// For now reader only
	//    pub(crate) fn add_shape(mut self, d: Shape) -> Run {
	//        self.children.push(RunChild::Shape(Box::new(d)));
	//        self
	//    }

	pub fn add_break(mut self, break_type: BreakType) -> Run {
		self.children.push(RunChild::Break(Break::new(break_type)));
		self
	}

	pub fn add_sym(mut self, sym: Sym) -> Run {
		self.children.push(RunChild::Sym(sym));
		self
	}

	pub fn style(mut self, style_id: &str) -> Self {
		self.run_property = self.run_property.style(style_id);
		self
	}

	pub fn size(mut self, size: usize) -> Run {
		self.run_property = self.run_property.size(size);
		self
	}

	pub fn character_spacing(mut self, v: i32) -> Run {
		self.run_property = self.run_property.spacing(v);
		self
	}

	pub fn color(mut self, color: impl Into<String>) -> Run {
		self.run_property = self.run_property.color(color);
		self
	}

	pub fn highlight(mut self, color: impl Into<String>) -> Run {
		self.run_property = self.run_property.highlight(color);
		self
	}

	pub fn bold(mut self) -> Run {
		self.run_property = self.run_property.bold();
		self
	}

	pub fn disable_bold(mut self) -> Run {
		self.run_property = self.run_property.disable_bold();
		self
	}

	pub fn italic(mut self) -> Run {
		self.run_property = self.run_property.italic();
		self
	}

	pub fn text_border(mut self, b: TextBorder) -> Run {
		self.run_property = self.run_property.text_border(b);
		self
	}

	pub fn disable_italic(mut self) -> Run {
		self.run_property = self.run_property.disable_italic();
		self
	}

	pub fn underline(mut self, line_type: impl Into<String>) -> Run {
		self.run_property = self.run_property.underline(line_type);
		self
	}

	pub fn vanish(mut self) -> Run {
		self.run_property = self.run_property.vanish();
		self
	}

	pub fn fonts(mut self, f: RunFonts) -> Run {
		self.run_property = self.run_property.fonts(f);
		self
	}

	pub(crate) fn set_property(mut self, p: RunProperty) -> Run {
		self.run_property = p;
		self
	}
}