docx-rs/docx-core/src/documents/elements/comment.rs

78 lines
1.7 KiB
Rust
Raw Normal View History

2019-12-04 11:26:09 +02:00
use crate::documents::{BuildXML, Paragraph};
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Comment<'a> {
id: &'a str,
author: &'a str,
date: &'a str,
paragraph: Paragraph<'a>,
}
impl<'a> Default for Comment<'a> {
fn default() -> Comment<'a> {
Comment {
id: "invalidId",
author: "unnamed",
date: "1970-01-01T00:00:00Z",
paragraph: Paragraph::new(),
}
}
}
impl<'a> Comment<'a> {
pub fn new(id: &'a str) -> Comment<'a> {
Self {
id,
..Default::default()
}
}
2019-12-05 08:44:18 +02:00
pub fn author(mut self, author: &'a str) -> Comment<'a> {
self.author = author;
self
}
pub fn date(mut self, date: &'a str) -> Comment<'a> {
self.date = date;
self
}
2019-12-04 11:26:09 +02:00
pub fn paragraph(mut self, p: Paragraph<'a>) -> Comment<'a> {
self.paragraph = p;
self
}
2019-12-05 08:44:18 +02:00
pub fn id(&self) -> &'a str {
self.id
}
2019-12-04 11:26:09 +02:00
}
impl<'a> BuildXML for Comment<'a> {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
2019-12-05 08:44:18 +02:00
.open_comment(&self.id, self.author, self.date, "")
.add_child(&self.paragraph)
2019-12-04 11:26:09 +02:00
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_ins_default() {
let b = Comment::new("123").build();
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-12-05 08:44:18 +02:00
r#"<w:comment w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z" w:initials=""><w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:p></w:comment>"#
2019-12-04 11:26:09 +02:00
);
}
}