docx_reader/documents/elements/
run.rs

1use super::*;
2use serde::ser::{SerializeStruct, Serializer};
3use serde::Serialize;
4
5use crate::types::*;
6
7#[derive(Serialize, Debug, Clone, PartialEq)]
8#[serde(rename_all = "camelCase")]
9pub struct Run {
10	pub run_property: RunProperty,
11	pub children: Vec<RunChild>,
12}
13
14impl Default for Run {
15	fn default() -> Self {
16		let run_property = RunProperty::new();
17		Self {
18			run_property,
19			children: vec![],
20		}
21	}
22}
23
24#[derive(Debug, Clone, PartialEq)]
25pub enum RunChild {
26	Text(Text),
27	Sym(Sym),
28	DeleteText(DeleteText),
29	Tab(Tab),
30	Break(Break),
31	Drawing(Box<Drawing>),
32	Shape(Box<Shape>),
33	FieldChar(FieldChar),
34	InstrText(Box<InstrText>),
35	DeleteInstrText(Box<DeleteInstrText>),
36	// For reader
37	InstrTextString(String),
38}
39
40impl Serialize for RunChild {
41	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
42	where
43		S: Serializer,
44	{
45		match *self {
46			RunChild::Text(ref s) => {
47				let mut t = serializer.serialize_struct("Text", 2)?;
48				t.serialize_field("type", "text")?;
49				t.serialize_field("data", s)?;
50				t.end()
51			}
52			RunChild::Sym(ref s) => {
53				let mut t = serializer.serialize_struct("Sym", 2)?;
54				t.serialize_field("type", "sym")?;
55				t.serialize_field("data", s)?;
56				t.end()
57			}
58			RunChild::DeleteText(ref s) => {
59				let mut t = serializer.serialize_struct("DeleteText", 2)?;
60				t.serialize_field("type", "deleteText")?;
61				t.serialize_field("data", s)?;
62				t.end()
63			}
64			RunChild::Tab(_) => {
65				let mut t = serializer.serialize_struct("Tab", 1)?;
66				t.serialize_field("type", "tab")?;
67				t.end()
68			}
69			RunChild::Break(ref s) => {
70				let mut t = serializer.serialize_struct("Break", 2)?;
71				t.serialize_field("type", "break")?;
72				t.serialize_field("data", s)?;
73				t.end()
74			}
75			RunChild::Drawing(ref s) => {
76				let mut t = serializer.serialize_struct("Drawing", 2)?;
77				t.serialize_field("type", "drawing")?;
78				t.serialize_field("data", s)?;
79				t.end()
80			}
81			RunChild::Shape(ref s) => {
82				let mut t = serializer.serialize_struct("Shape", 2)?;
83				t.serialize_field("type", "shape")?;
84				t.serialize_field("data", s)?;
85				t.end()
86			}
87			RunChild::FieldChar(ref f) => {
88				let mut t = serializer.serialize_struct("FieldChar", 2)?;
89				t.serialize_field("type", "fieldChar")?;
90				t.serialize_field("data", f)?;
91				t.end()
92			}
93			RunChild::InstrText(ref i) => {
94				let mut t = serializer.serialize_struct("InstrText", 2)?;
95				t.serialize_field("type", "instrText")?;
96				t.serialize_field("data", i)?;
97				t.end()
98			}
99			RunChild::DeleteInstrText(ref i) => {
100				let mut t = serializer.serialize_struct("DeleteInstrText", 2)?;
101				t.serialize_field("type", "deleteInstrText")?;
102				t.serialize_field("data", i)?;
103				t.end()
104			}
105			RunChild::InstrTextString(ref i) => {
106				let mut t = serializer.serialize_struct("InstrTextString", 2)?;
107				t.serialize_field("type", "instrTextString")?;
108				t.serialize_field("data", i)?;
109				t.end()
110			}
111		}
112	}
113}
114
115impl Run {
116	pub fn new() -> Run {
117		Run {
118			..Default::default()
119		}
120	}
121
122	pub fn add_text(mut self, text: impl Into<String>) -> Run {
123		self.children
124			.push(RunChild::Text(Text::new(text.into().replace('\n', ""))));
125		self
126	}
127
128	pub(crate) fn add_text_without_escape(mut self, text: impl Into<String>) -> Run {
129		self.children.push(RunChild::Text(Text::without_escape(
130			text.into().replace('\n', ""),
131		)));
132		self
133	}
134
135	pub fn add_delete_text(mut self, text: impl Into<String>) -> Run {
136		self.children.push(RunChild::DeleteText(DeleteText::new(
137			text.into().replace('\n', ""),
138		)));
139		self
140	}
141
142	pub(crate) fn add_delete_text_without_escape(mut self, text: impl Into<String>) -> Run {
143		self.children
144			.push(RunChild::DeleteText(DeleteText::without_escape(
145				text.into().replace('\n', ""),
146			)));
147		self
148	}
149
150	pub fn add_field_char(mut self, t: crate::types::FieldCharType, dirty: bool) -> Run {
151		let mut f = FieldChar::new(t);
152		if dirty {
153			f = f.dirty();
154		};
155		self.children.push(RunChild::FieldChar(f));
156		self
157	}
158
159	pub fn add_instr_text(mut self, i: InstrText) -> Run {
160		self.children.push(RunChild::InstrText(Box::new(i)));
161		self
162	}
163
164	pub fn add_delete_instr_text(mut self, i: DeleteInstrText) -> Run {
165		self.children.push(RunChild::DeleteInstrText(Box::new(i)));
166		self
167	}
168
169	pub fn add_tab(mut self) -> Run {
170		self.children.push(RunChild::Tab(Tab::new()));
171		self
172	}
173
174	pub fn add_image(mut self, pic: Pic) -> Run {
175		self.children
176			.push(RunChild::Drawing(Box::new(Drawing::new().pic(pic))));
177		self
178	}
179
180	pub(crate) fn add_drawing(mut self, d: Drawing) -> Run {
181		self.children.push(RunChild::Drawing(Box::new(d)));
182		self
183	}
184
185	// For now reader only
186	//    pub(crate) fn add_shape(mut self, d: Shape) -> Run {
187	//        self.children.push(RunChild::Shape(Box::new(d)));
188	//        self
189	//    }
190
191	pub fn add_break(mut self, break_type: BreakType) -> Run {
192		self.children.push(RunChild::Break(Break::new(break_type)));
193		self
194	}
195
196	pub fn add_sym(mut self, sym: Sym) -> Run {
197		self.children.push(RunChild::Sym(sym));
198		self
199	}
200
201	pub fn style(mut self, style_id: &str) -> Self {
202		self.run_property = self.run_property.style(style_id);
203		self
204	}
205
206	pub fn size(mut self, size: usize) -> Run {
207		self.run_property = self.run_property.size(size);
208		self
209	}
210
211	pub fn character_spacing(mut self, v: i32) -> Run {
212		self.run_property = self.run_property.spacing(v);
213		self
214	}
215
216	pub fn color(mut self, color: impl Into<String>) -> Run {
217		self.run_property = self.run_property.color(color);
218		self
219	}
220
221	pub fn highlight(mut self, color: impl Into<String>) -> Run {
222		self.run_property = self.run_property.highlight(color);
223		self
224	}
225
226	pub fn bold(mut self) -> Run {
227		self.run_property = self.run_property.bold();
228		self
229	}
230
231	pub fn disable_bold(mut self) -> Run {
232		self.run_property = self.run_property.disable_bold();
233		self
234	}
235
236	pub fn italic(mut self) -> Run {
237		self.run_property = self.run_property.italic();
238		self
239	}
240
241	pub fn text_border(mut self, b: TextBorder) -> Run {
242		self.run_property = self.run_property.text_border(b);
243		self
244	}
245
246	pub fn disable_italic(mut self) -> Run {
247		self.run_property = self.run_property.disable_italic();
248		self
249	}
250
251	pub fn underline(mut self, line_type: impl Into<String>) -> Run {
252		self.run_property = self.run_property.underline(line_type);
253		self
254	}
255
256	pub fn vanish(mut self) -> Run {
257		self.run_property = self.run_property.vanish();
258		self
259	}
260
261	pub fn fonts(mut self, f: RunFonts) -> Run {
262		self.run_property = self.run_property.fonts(f);
263		self
264	}
265
266	pub(crate) fn set_property(mut self, p: RunProperty) -> Run {
267		self.run_property = p;
268		self
269	}
270}