docx_rs/documents/
comments.rs1use super::Comment;
2use crate::documents::BuildXML;
3use crate::xml_builder::*;
4use std::io::Write;
5
6use serde::Serialize;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Default)]
9#[serde(rename_all = "camelCase")]
10pub struct Comments {
11 pub(crate) comments: Vec<Comment>,
12}
13
14impl Comments {
15 pub fn new() -> Self {
16 Default::default()
17 }
18
19 pub fn inner(&self) -> &[Comment] {
20 &self.comments
21 }
22
23 pub fn into_inner(self) -> Vec<Comment> {
24 self.comments
25 }
26
27 pub(crate) fn add_comments(&mut self, comments: Vec<Comment>) {
28 self.comments = comments;
29 }
30}
31
32impl BuildXML for Comments {
33 fn build_to<W: Write>(
34 &self,
35 stream: xml::writer::EventWriter<W>,
36 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
37 XMLBuilder::from(stream)
38 .declaration(Some(true))?
39 .open_comments()?
40 .add_children(&self.comments)?
41 .close()?
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 use std::str;
53
54 #[test]
55 fn test_comments() {
56 let b = Comments::new().build();
57 assert_eq!(
58 str::from_utf8(&b).unwrap(),
59 r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:comments xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14" />"#
60 );
61 }
62}