docx_rs/documents/
taskpanes_rels.rs

1use serde::{Deserialize, Serialize};
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)]
8pub struct TaskpanesRels {
9    pub rels: Vec<(String, String, String)>,
10}
11
12impl TaskpanesRels {
13    pub fn new() -> Self {
14        Default::default()
15    }
16
17    pub fn add_rel(mut self) -> Self {
18        let index = self.rels.len() + 1;
19        self.rels.push((
20            "http://schemas.microsoft.com/office/2011/relationships/webextension".to_string(),
21            format!("rId{}", index),
22            format!("webextension{}.xml", index),
23        ));
24        self
25    }
26
27    pub fn find_target(&self, rel_type: &str) -> Option<&(String, String, String)> {
28        self.rels.iter().find(|rel| rel.0 == rel_type)
29    }
30}
31
32impl BuildXML for TaskpanesRels {
33    fn build_to<W: Write>(
34        &self,
35        stream: xml::writer::EventWriter<W>,
36    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
37        XMLBuilder::from(stream)
38            .declaration(Some(true))?
39            .open_relationships("http://schemas.openxmlformats.org/package/2006/relationships")?
40            .apply_each(&self.rels, |(k, id, v), b| b.relationship(id, k, v))?
41            .close()?
42            .into_inner()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48
49    use super::*;
50    #[cfg(test)]
51    use pretty_assertions::assert_eq;
52    use std::str;
53
54    #[test]
55    fn test_build() {
56        let c = TaskpanesRels::new().add_rel();
57        let b = c.build();
58        assert_eq!(
59            str::from_utf8(&b).unwrap(),
60            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.microsoft.com/office/2011/relationships/webextension" Target="webextension1.xml" /></Relationships>"#
61        );
62    }
63}