docx_rs/documents/elements/
data_binding.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::*;
5use crate::xml_builder::*;
6
7#[derive(Serialize, Debug, Clone, PartialEq, Default)]
8pub struct DataBinding {
9 pub xpath: Option<String>,
10 pub prefix_mappings: Option<String>,
11 pub store_item_id: Option<String>,
12}
13
14impl DataBinding {
15 pub fn new() -> Self {
16 Default::default()
17 }
18
19 pub fn xpath(mut self, xpath: impl Into<String>) -> Self {
20 self.xpath = Some(xpath.into());
21 self
22 }
23
24 pub fn prefix_mappings(mut self, m: impl Into<String>) -> Self {
25 self.prefix_mappings = Some(m.into());
26 self
27 }
28
29 pub fn store_item_id(mut self, id: impl Into<String>) -> Self {
30 self.store_item_id = Some(id.into());
31 self
32 }
33}
34
35impl BuildXML for DataBinding {
36 fn build_to<W: Write>(
37 &self,
38 stream: xml::writer::EventWriter<W>,
39 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
40 XMLBuilder::from(stream)
41 .data_binding(
42 self.xpath.as_ref(),
43 self.prefix_mappings.as_ref(),
44 self.store_item_id.as_ref(),
45 )?
46 .into_inner()
47 }
48}
49
50#[cfg(test)]
51mod tests {
52
53 use super::*;
54 #[cfg(test)]
55 use pretty_assertions::assert_eq;
56 use std::str;
57
58 #[test]
59 fn test_delete_default() {
60 let b = DataBinding::new().xpath("root/hello").build();
61 assert_eq!(
62 str::from_utf8(&b).unwrap(),
63 r#"<w:dataBinding w:xpath="root/hello" />"#
64 );
65 }
66}