docx_rs/documents/elements/
comment_extended.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7#[derive(Debug, Clone, PartialEq, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct CommentExtended {
10    pub paragraph_id: String,
11    pub done: bool,
12    pub parent_paragraph_id: Option<String>,
13}
14
15impl CommentExtended {
16    pub fn new(paragraph_id: impl Into<String>) -> CommentExtended {
17        Self {
18            paragraph_id: paragraph_id.into(),
19            done: false,
20            parent_paragraph_id: None,
21        }
22    }
23
24    pub fn done(mut self) -> CommentExtended {
25        self.done = true;
26        self
27    }
28
29    pub fn parent_paragraph_id(mut self, id: impl Into<String>) -> CommentExtended {
30        self.parent_paragraph_id = Some(id.into());
31        self
32    }
33}
34
35impl BuildXML for CommentExtended {
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            .comment_extended(&self.paragraph_id, self.done, &self.parent_paragraph_id)?
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    #[test]
53    fn test_comment_extended_json() {
54        let ex = CommentExtended {
55            paragraph_id: "00002".to_owned(),
56            done: false,
57            parent_paragraph_id: Some("0004".to_owned()),
58        };
59        assert_eq!(
60            serde_json::to_string(&ex).unwrap(),
61            r#"{"paragraphId":"00002","done":false,"parentParagraphId":"0004"}"#
62        );
63    }
64}