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
//! Small DOCX packages for the tests of the crates that lay out DOCX files, behind the feature
//! `testing`, on the package builder of `f-office-opc`: the namespace of WordprocessingML, the
//! root of a part, the main document and the parts that it names, written once.
pub use f_office_opc::testing::{Builder, PACKAGE_RELATIONSHIPS, RELATIONSHIPS, zip};
/// The source of paragraph IDs, for a test that builds a placed line
/// ([`PlacedLine`](crate::PlacedLine)) without a layout.
pub use crate::body::ParagraphIds;
/// The namespace of the elements of WordprocessingML (ECMA-376 Part 1, 17).
pub const MAIN: &str = crate::ns::W;
/// The main document part of a DOCX package.
pub const DOCUMENT: &str = "word/document.xml";
/// The content of a styles part whose document defaults are empty, so that text keeps the
/// defaults of ECMA-376 and not those of Word's template.
pub const PLAIN_STYLES: &str = "<w:docDefaults><w:rPrDefault/><w:pPrDefault/></w:docDefaults>";
/// The root element `w:{root}` of a WordprocessingML part, which holds `content` and declares
/// the prefixes `w` and `r`.
#[must_use]
pub fn word(root: &str, content: &str) -> String {
format!(r#"<w:{root} xmlns:w="{MAIN}" xmlns:r="{RELATIONSHIPS}">{content}</w:{root}>"#)
}
/// A DOCX package with the main document `document` and the package's relationship to it.
#[must_use]
pub fn package(document: impl Into<Vec<u8>>) -> Builder {
Builder::default()
.relationship("", "main", "officeDocument", DOCUMENT)
.part(DOCUMENT, document)
}
/// A DOCX package whose main document holds `body` in its `w:body`.
#[must_use]
pub fn docx(body: &str) -> Builder {
package(word("document", &format!("<w:body>{body}</w:body>")))
}
/// The parts of a DOCX package that its main document names.
pub trait WordParts {
/// Adds the part `word/{target}`, whose root `w:{root}` holds `content`, and the main
/// document's relationship of the type `kind` to it under the ID `id`.
#[must_use]
fn word_part(self, id: &str, kind: &str, target: &str, root: &str, content: &str) -> Self;
/// Adds a styles part of [`PLAIN_STYLES`] and the main document's relationship to it.
#[must_use]
fn plain_styles(self) -> Self;
}
impl WordParts for Builder {
fn word_part(self, id: &str, kind: &str, target: &str, root: &str, content: &str) -> Self {
self.relationship(DOCUMENT, id, kind, target)
.part(&format!("word/{target}"), word(root, content))
}
fn plain_styles(self) -> Self {
self.word_part(
"plainStyles",
"styles",
"styles.xml",
"styles",
PLAIN_STYLES,
)
}
}
/// Edits of the XML text of a test that fail the test when the text that they change is not
/// there, so that a test never passes on XML that it did not change.
pub trait Replaced {
/// The text with its one `from` replaced by `to`.
///
/// # Panics
///
/// When the text holds `from` other than once.
#[must_use]
fn replaced(&self, from: &str, to: &str) -> String;
/// The text with each `from` replaced by `to`.
///
/// # Panics
///
/// When the text does not hold `from`.
#[must_use]
fn replaced_all(&self, from: &str, to: &str) -> String;
}
impl Replaced for str {
#[track_caller]
fn replaced(&self, from: &str, to: &str) -> String {
let count = self.matches(from).count();
assert_eq!(count, 1, "the XML holds {from:?} {count} times, not once");
self.replacen(from, to, 1)
}
#[track_caller]
fn replaced_all(&self, from: &str, to: &str) -> String {
assert!(self.contains(from), "the XML does not hold {from:?}");
self.replace(from, to)
}
}
#[cfg(test)]
mod tests;