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
#![allow(unused_must_use)]
use derive_more::From;
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};
use crate::{
__setter, __xml_test_suites,
document::{field_char::FieldChar, tab::Tab, drawing::Drawing, instrtext::InstrText, r#break::Break, text::Text},
formatting::CharacterProperty,
DocxResult,
};
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:r")]
pub struct Run<'a> {
#[xml(attr = "w:rsidR")]
pub rsid_r: Option<Cow<'a, str>>,
#[xml(attr = "w:rsidRDefault")]
pub rsid_r_default: Option<Cow<'a, str>>,
#[xml(child = "w:rPr")]
pub property: Option<CharacterProperty<'a>>,
#[xml(
child = "w:t",
child = "w:br",
child = "w:tab",
child = "w:drawing",
child = "w:fldChar",
child = "w:instrText",
child = "w:separator",
child = "w:continuationSeparator",
)]
pub content: Vec<RunContent<'a>>,
#[xml(attr = "w:is_delete")]
pub shall_destroy: Option<bool>,
}
impl<'a> Run<'a> {
__setter!(property: Option<CharacterProperty<'a>>);
__setter!(shall_destroy: Option<bool>);
#[inline(always)]
pub fn push<T: Into<RunContent<'a>>>(mut self, content: T) -> Self {
self.content.push(content.into());
self
}
#[inline(always)]
pub fn push_text<T: Into<Text<'a>>>(mut self, content: T) -> Self {
self.content.push(RunContent::Text(content.into()));
self
}
#[inline(always)]
pub fn push_break<T: Into<Break>>(mut self, br: T) -> Self {
self.content.push(RunContent::Break(br.into()));
self
}
pub fn iter_text(&self) -> impl Iterator<Item = &Cow<'a, str>> {
self.content.iter().filter_map(|content| match content {
RunContent::Text(Text { text, .. }) => Some(text),
RunContent::InstrText(InstrText { text, .. }) => Some(text),
RunContent::Break(_) => None,
RunContent::FieldChar(_) => None,
RunContent::Separator(_) => None,
RunContent::ContinuationSeparator(_) => None,
RunContent::Tab(_) => None,
RunContent::Drawing(_) => None,
})
}
pub fn iter_text_mut(&mut self) -> impl Iterator<Item = &mut Cow<'a, str>> {
self.content.iter_mut().filter_map(|content| match content {
RunContent::Text(Text { text, .. }) => Some(text),
RunContent::InstrText(InstrText { text, .. }) => Some(text),
RunContent::Break(_) => None,
RunContent::FieldChar(_) => None,
RunContent::Separator(_) => None,
RunContent::ContinuationSeparator(_) => None,
RunContent::Tab(_) => None,
RunContent::Drawing(_) => None,
})
}
pub fn replace_text_simple<S>(&mut self, old: S, new: S)
where
S: AsRef<str>,
{
let dic = (old, new);
let dic = vec![dic];
self.replace_text(&dic);
}
pub fn replace_text<'b, T, S>(&mut self, dic: T) -> DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
{
for c in self.content.iter_mut() {
match c {
RunContent::Text(t) => {
let mut tc = t.text.to_string();
for p in dic {
tc = tc.replace(p.0.as_ref(), p.1.as_ref());
}
t.text = tc.into();
}
_ => {}
}
}
Ok(())
}
}
#[derive(Debug, From, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum RunContent<'a> {
#[xml(tag = "w:t")]
Text(Text<'a>),
#[xml(tag = "w:br")]
Break(Break),
#[xml(tag = "w:fldChar")]
FieldChar(FieldChar),
#[xml(tag = "w:instrText")]
InstrText(InstrText<'a>),
#[xml(tag = "w:separator")]
Separator(Separator),
#[xml(tag = "w:continuationSeparator")]
ContinuationSeparator(ContinuationSeparator),
#[xml(tag = "w:tab")]
Tab(Tab),
#[xml(tag = "w:drawing")]
Drawing(Drawing),
}
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:separator")]
pub struct Separator {
}
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:continuationSeparator")]
pub struct ContinuationSeparator {
}
__xml_test_suites!(
Run,
Run::default(),
r#"<w:r/>"#,
Run::default().push_break(None),
r#"<w:r><w:br/></w:r>"#,
Run::default().push_text("text"),
r#"<w:r><w:t>text</w:t></w:r>"#,
);