docx_rs/documents/doc_props/
custom.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Default)]
8#[serde(rename_all = "camelCase")]
9pub struct CustomProps {
10    pub properties: std::collections::HashMap<String, String>,
11}
12
13impl CustomProps {
14    pub(crate) fn new() -> Self {
15        Self::default()
16    }
17
18    pub fn add_custom_property(mut self, name: impl Into<String>, item: impl Into<String>) -> Self {
19        self.properties.insert(name.into(), item.into());
20        self
21    }
22}
23
24impl BuildXML for CustomProps {
25    fn build_to<W: Write>(
26        &self,
27        stream: xml::writer::EventWriter<W>,
28    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
29        XMLBuilder::from(stream)
30            .declaration(Some(true))?
31            .open_custom_properties(
32                "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
33                "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
34            )?
35            .apply_each(self.properties.iter().enumerate(), |(i, (key, item)), b| {
36                b.open_property(
37                    "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
38                    // I can not find spec about this id.
39                    // It is invalid if pid starts from 1...
40                    &format!("{}", i + 2),
41                    key,
42                )?
43                .lpwstr(item)?
44                .close()
45            })?
46            .close()?
47            .into_inner()
48    }
49}