docx-rs/docx-core/src/documents/comments.rs

53 lines
1.8 KiB
Rust
Raw Normal View History

2019-12-05 08:44:18 +02:00
use super::Comment;
2019-12-04 10:39:59 +02:00
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug)]
2019-12-05 08:44:18 +02:00
pub struct Comments<'a> {
comments: Vec<Comment<'a>>,
}
2019-12-04 10:39:59 +02:00
2019-12-05 08:44:18 +02:00
impl<'a> Comments<'a> {
pub fn new() -> Self {
2019-12-04 10:39:59 +02:00
Default::default()
}
2019-12-05 08:44:18 +02:00
pub(crate) fn add_comments(&mut self, comments: Vec<Comment<'a>>) {
self.comments = comments;
}
2019-12-04 10:39:59 +02:00
}
2019-12-05 08:44:18 +02:00
impl<'a> Default for Comments<'a> {
2019-12-04 10:39:59 +02:00
fn default() -> Self {
2019-12-05 08:44:18 +02:00
Self { comments: vec![] }
2019-12-04 10:39:59 +02:00
}
}
2019-12-05 08:44:18 +02:00
impl<'a> BuildXML for Comments<'a> {
2019-12-04 10:39:59 +02:00
fn build(&self) -> Vec<u8> {
2019-12-05 08:44:18 +02:00
let mut b = XMLBuilder::new().declaration(Some(true)).open_comments();
for c in &self.comments {
b = b.add_child(c)
}
b.close().build()
2019-12-04 10:39:59 +02:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_comments() {
let b = Comments::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
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" />"#
);
}
}