2021-01-27 12:44:29 +02:00
|
|
|
use serde::ser::{SerializeStruct, Serializer};
|
2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2021-01-27 12:44:29 +02:00
|
|
|
use crate::documents::*;
|
2019-11-15 11:15:43 +02:00
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Delete {
|
2019-12-19 09:37:28 +02:00
|
|
|
pub author: String,
|
|
|
|
pub date: String,
|
2021-01-27 12:44:29 +02:00
|
|
|
pub children: Vec<DeleteChild>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum DeleteChild {
|
|
|
|
Run(Run),
|
|
|
|
CommentStart(Box<CommentRangeStart>),
|
|
|
|
CommentEnd(CommentRangeEnd),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for DeleteChild {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
DeleteChild::Run(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("Run", 2)?;
|
|
|
|
t.serialize_field("type", "run")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
DeleteChild::CommentStart(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeStart")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
DeleteChild::CommentEnd(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeEnd")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Delete {
|
|
|
|
fn default() -> Delete {
|
2019-11-15 11:15:43 +02:00
|
|
|
Delete {
|
2019-12-08 21:14:27 +02:00
|
|
|
author: "unnamed".to_owned(),
|
|
|
|
date: "1970-01-01T00:00:00Z".to_owned(),
|
2021-01-27 12:44:29 +02:00
|
|
|
children: vec![],
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Delete {
|
2021-01-27 12:44:29 +02:00
|
|
|
pub fn new() -> Delete {
|
2019-12-19 09:37:28 +02:00
|
|
|
Self {
|
2021-01-27 12:44:29 +02:00
|
|
|
children: vec![],
|
2019-12-19 09:37:28 +02:00
|
|
|
..Default::default()
|
|
|
|
}
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
2020-06-01 07:49:41 +03:00
|
|
|
|
|
|
|
pub fn add_run(mut self, run: Run) -> Delete {
|
2021-01-27 12:44:29 +02:00
|
|
|
self.children.push(DeleteChild::Run(run));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_comment_start(mut self, comment: Comment) -> Delete {
|
|
|
|
self.children
|
|
|
|
.push(DeleteChild::CommentStart(Box::new(CommentRangeStart::new(
|
|
|
|
comment,
|
|
|
|
))));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_comment_end(mut self, id: usize) -> Delete {
|
|
|
|
self.children
|
|
|
|
.push(DeleteChild::CommentEnd(CommentRangeEnd::new(id)));
|
2019-11-15 11:15:43 +02:00
|
|
|
self
|
|
|
|
}
|
2019-12-19 09:37:28 +02:00
|
|
|
|
|
|
|
pub fn author(mut self, author: impl Into<String>) -> Delete {
|
|
|
|
self.author = author.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn date(mut self, date: impl Into<String>) -> Delete {
|
|
|
|
self.date = date.into();
|
|
|
|
self
|
|
|
|
}
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl HistoryId for Delete {}
|
2019-11-15 11:15:43 +02:00
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Delete {
|
2019-11-15 11:15:43 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2021-01-27 12:44:29 +02:00
|
|
|
let mut b = XMLBuilder::new().open_delete(&self.generate(), &self.author, &self.date);
|
|
|
|
for c in &self.children {
|
|
|
|
match c {
|
|
|
|
DeleteChild::Run(t) => b = b.add_child(t),
|
|
|
|
DeleteChild::CommentStart(c) => b = b.add_child(c),
|
|
|
|
DeleteChild::CommentEnd(c) => b = b.add_child(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.close().build()
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delete_default() {
|
2021-01-27 12:44:29 +02:00
|
|
|
let b = Delete::new().add_run(Run::new()).build();
|
2019-11-15 11:15:43 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-12-04 11:26:09 +02:00
|
|
|
r#"<w:del w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:del>"#
|
2019-11-15 11:15:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|