docx_rs/documents/elements/
paragraph_property_change.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::*;
5use crate::escape;
6use crate::xml_builder::*;
7
8#[derive(Serialize, Debug, Clone, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub struct ParagraphPropertyChange {
11    pub author: String,
12    pub date: String,
13    pub property: Box<ParagraphProperty>,
14}
15
16impl Default for ParagraphPropertyChange {
17    fn default() -> ParagraphPropertyChange {
18        Self {
19            author: "unnamed".to_owned(),
20            date: "1970-01-01T00:00:00Z".to_owned(),
21            property: Default::default(),
22        }
23    }
24}
25
26impl ParagraphPropertyChange {
27    pub fn new() -> ParagraphPropertyChange {
28        Self {
29            ..Default::default()
30        }
31    }
32
33    pub fn property(mut self, p: ParagraphProperty) -> ParagraphPropertyChange {
34        self.property = Box::new(p);
35        self
36    }
37
38    pub fn author(mut self, author: impl Into<String>) -> ParagraphPropertyChange {
39        self.author = escape::escape(&author.into());
40        self
41    }
42
43    pub fn date(mut self, date: impl Into<String>) -> ParagraphPropertyChange {
44        self.date = date.into();
45        self
46    }
47}
48
49impl ParagraphPropertyChangeId for ParagraphPropertyChange {}
50
51impl BuildXML for ParagraphPropertyChange {
52    fn build_to<W: Write>(
53        &self,
54        stream: xml::writer::EventWriter<W>,
55    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
56        let id = self.generate();
57        XMLBuilder::from(stream)
58            .open_paragraph_property_change(&id, &self.author, &self.date)?
59            .add_child(&self.property)?
60            .close()?
61            .into_inner()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67
68    use super::*;
69    #[cfg(test)]
70    use pretty_assertions::assert_eq;
71    use std::str;
72
73    #[test]
74    fn test_ppr_change_default() {
75        let b = ParagraphPropertyChange::new()
76            .property(ParagraphProperty::new())
77            .build();
78        assert_eq!(
79            str::from_utf8(&b).unwrap(),
80            r#"<w:pPrChange w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:pPr><w:rPr /></w:pPr></w:pPrChange>"#
81        );
82    }
83}