docx_rs/documents/
webextension.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::escape::escape;
6use crate::xml_builder::*;
7
8#[derive(Serialize, Debug, PartialEq, Clone)]
9pub struct WebExtensionProperty {
10 pub name: String,
11 pub value: String,
12}
13
14impl WebExtensionProperty {
15 pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
16 Self {
17 name: name.into(),
18 value: value.into(),
19 }
20 }
21}
22
23#[derive(Serialize, Debug, PartialEq, Clone)]
24pub struct WebExtension {
25 pub id: String,
26 pub version: String,
27 pub store: String,
28 pub store_type: String,
29 pub reference_id: String,
30 pub properties: Vec<WebExtensionProperty>,
31}
32
33impl WebExtension {
34 pub fn new(
35 id: impl Into<String>,
36 reference_id: impl Into<String>,
37 version: impl Into<String>,
38 store: impl Into<String>,
39 store_type: impl Into<String>,
40 ) -> Self {
41 Self {
42 id: id.into(),
43 reference_id: reference_id.into(),
44 version: version.into(),
45 store: store.into(),
46 store_type: store_type.into(),
47 properties: vec![],
48 }
49 }
50
51 pub fn property(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
52 let v = value.into();
53 let v = format!(""{}"", escape(&v).replace(""", "\\""));
54 self.properties.push(WebExtensionProperty::new(name, v));
55 self
56 }
57}
58
59impl BuildXML for WebExtension {
60 fn build_to<W: Write>(
61 &self,
62 stream: xml::writer::EventWriter<W>,
63 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
64 XMLBuilder::from(stream)
65 .declaration(Some(true))?
66 .open_webextension(
67 "http://schemas.microsoft.com/office/webextensions/webextension/2010/11",
68 &format!("{{{}}}", &self.id),
69 )?
70 .webextension_reference(
71 &self.reference_id,
72 &self.version,
73 &self.store,
74 &self.store_type,
75 )?
76 .webextension_alternate_references()?
77 .open_webextension_properties()?
78 .apply_each(&self.properties, |p, b| {
79 b.webextension_property(&p.name, &p.value)
80 })?
81 .close()?
82 .webextension_bindings()?
83 .webextension_snapshot(
84 "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
85 )?
86 .close()?
87 .into_inner()
88 }
89}
90
91#[cfg(test)]
92mod tests {
93
94 use super::*;
95 #[cfg(test)]
96 use pretty_assertions::assert_eq;
97 use std::str;
98
99 #[test]
100 fn test_build() {
101 let c = WebExtension::new(
102 "7f33b723-fb58-4524-8733-dbedc4b7c095",
103 "abcd",
104 "1.0.0.0",
105 "developer",
106 "Registry",
107 )
108 .property("hello", "world");
109 let b = c.build();
110 assert_eq!(
111 str::from_utf8(&b).unwrap(),
112 r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><we:webextension xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" id="{7f33b723-fb58-4524-8733-dbedc4b7c095}"><we:reference id="abcd" version="1.0.0.0" store="developer" storeType="Registry" /><we:alternateReferences /><we:properties><we:property name="hello" value=""world"" /></we:properties><we:bindings /><we:snapshot xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" /></we:webextension>"#
113 );
114 }
115}