docx_rust/document/
comments.rs

1//! Comments part
2//!
3//! The corresponding ZIP item is `/word/comments.xml`.
4#![allow(unused_must_use)]
5
6use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
7use std::{borrow::Cow, io::Write};
8
9use crate::{
10    document::Paragraph,
11    schema::{SCHEMA_MAIN, SCHEMA_WORDML_14},
12};
13
14/// The root element of the comments document part.
15#[derive(Debug, Default, XmlRead, Clone)]
16#[cfg_attr(test, derive(PartialEq))]
17#[xml(tag = "w:comments")]
18pub struct Comments<'a> {
19    // Specifies the comments
20    #[xml(child = "w:comment")]
21    pub comments: Vec<Comment<'a>>,
22}
23
24#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
25#[cfg_attr(test, derive(PartialEq))]
26#[xml(tag = "w:comment")]
27pub struct Comment<'a> {
28    // Specifies the id of the comment.
29    #[xml(attr = "w:id")]
30    pub id: Option<isize>,
31
32    #[xml(attr = "w:author")]
33    pub author: Cow<'a, str>,
34
35    // Specifies the body of the comment.
36    #[xml(child = "w:p")]
37    pub content: Paragraph<'a>,
38}
39
40#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
41#[cfg_attr(test, derive(PartialEq))]
42#[xml(tag = "w:annotationRef")]
43pub struct AnnotationRef;
44
45#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
46#[cfg_attr(test, derive(PartialEq))]
47#[xml(tag = "w:commentReference")]
48pub struct CommentReference<'a> {
49    #[xml(attr = "w:id")]
50    pub id: Option<Cow<'a, str>>,
51}
52
53impl<'a> XmlWrite for Comments<'a> {
54    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
55        let Comments { comments } = self;
56
57        log::debug!("[Comments] Started writing.");
58        let _ = write!(writer.inner, "{}", crate::schema::SCHEMA_XML);
59
60        writer.write_element_start("w:comments")?;
61
62        writer.write_attribute("xmlns:w", SCHEMA_MAIN)?;
63
64        writer.write_attribute("xmlns:w14", SCHEMA_WORDML_14)?;
65
66        writer.write_element_end_open()?;
67
68        for c in comments {
69            c.to_writer(writer)?;
70        }
71
72        writer.write_element_end_close("w:comments")?;
73
74        log::debug!("[Comments] Finished writing.");
75
76        Ok(())
77    }
78}