2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-12-04 11:26:09 +02:00
|
|
|
use crate::documents::{BuildXML, Paragraph};
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
2020-12-15 15:33:01 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Comment {
|
2019-12-13 17:47:47 +02:00
|
|
|
pub id: usize,
|
2019-12-11 07:12:22 +02:00
|
|
|
pub author: String,
|
|
|
|
pub date: String,
|
|
|
|
pub paragraph: Paragraph,
|
2020-08-13 19:57:59 +03:00
|
|
|
pub parent_comment_id: Option<usize>,
|
2019-12-04 11:26:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Comment {
|
|
|
|
fn default() -> Comment {
|
2019-12-04 11:26:09 +02:00
|
|
|
Comment {
|
2019-12-13 17:47:47 +02:00
|
|
|
id: 1,
|
2019-12-08 21:14:27 +02:00
|
|
|
author: "unnamed".to_owned(),
|
|
|
|
date: "1970-01-01T00:00:00Z".to_owned(),
|
2019-12-04 11:26:09 +02:00
|
|
|
paragraph: Paragraph::new(),
|
2020-08-13 19:57:59 +03:00
|
|
|
parent_comment_id: None,
|
2019-12-04 11:26:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Comment {
|
2019-12-13 17:47:47 +02:00
|
|
|
pub fn new(id: usize) -> Comment {
|
2019-12-04 11:26:09 +02:00
|
|
|
Self {
|
2019-12-13 17:47:47 +02:00
|
|
|
id,
|
2019-12-04 11:26:09 +02:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn author(mut self, author: impl Into<String>) -> Comment {
|
|
|
|
self.author = author.into();
|
2019-12-05 08:44:18 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn date(mut self, date: impl Into<String>) -> Comment {
|
|
|
|
self.date = date.into();
|
2019-12-05 08:44:18 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
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
|
|
|
|
2020-08-13 19:57:59 +03:00
|
|
|
pub fn parent_comment_id(mut self, parent_comment_id: usize) -> Comment {
|
|
|
|
self.parent_comment_id = Some(parent_comment_id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-13 17:47:47 +02:00
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
self.id
|
2019-12-05 08:44:18 +02:00
|
|
|
}
|
2019-12-04 11:26:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Comment {
|
2019-12-04 11:26:09 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
XMLBuilder::new()
|
2019-12-13 17:47:47 +02:00
|
|
|
.open_comment(&format!("{}", 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() {
|
2019-12-13 17:47:47 +02:00
|
|
|
let b = Comment::new(1).build();
|
2019-12-04 11:26:09 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-08-13 19:57:59 +03:00
|
|
|
r#"<w:comment w:id="1" w:author="unnamed" w:date="1970-01-01T00:00:00Z" w:initials=""><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr></w:p></w:comment>"#
|
2019-12-04 11:26:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|