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 {
id: String,
author: String,
date: String,
paragraph: Paragraph,
2019-12-04 11:26:09 +02:00
}
impl Default for Comment {
fn default() -> Comment {
2019-12-04 11:26:09 +02:00
Comment {
id: "invalidId".to_owned(),
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
2019-12-04 11:26:09 +02:00
paragraph: Paragraph::new(),
}
}
}
impl Comment {
pub fn new(id: impl Into<String>) -> Comment {
2019-12-04 11:26:09 +02:00
Self {
id: id.into(),
2019-12-04 11:26:09 +02:00
..Default::default()
}
}
pub fn author(mut self, author: impl Into<String>) -> Comment {
self.author = author.into();
2019-12-05 08:44:18 +02:00
self
}
pub fn date(mut self, date: impl Into<String>) -> Comment {
self.date = date.into();
2019-12-05 08:44:18 +02:00
self
}
pub fn paragraph(mut self, p: Paragraph) -> Comment {
2019-12-04 11:26:09 +02:00
self.paragraph = p;
self
}
2019-12-05 08:44:18 +02:00
pub fn id(&self) -> String {
self.id.clone()
2019-12-05 08:44:18 +02:00
}
2019-12-04 11:26:09 +02:00
}
impl BuildXML for Comment {
2019-12-04 11:26:09 +02:00
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_comment(&self.id, &self.author, &self.date, "")
2019-12-05 08:44:18 +02:00
.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
);
}
}