docx_rs/documents/elements/
delete.rs1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use crate::xml_builder::*;
6use crate::{documents::*, escape};
7
8#[derive(Serialize, Debug, Clone, PartialEq)]
9pub struct Delete {
10 pub author: String,
11 pub date: String,
12 pub children: Vec<DeleteChild>,
13}
14
15#[derive(Debug, Clone, PartialEq)]
16pub enum DeleteChild {
17 Run(Run),
18 CommentStart(Box<CommentRangeStart>),
19 CommentEnd(CommentRangeEnd),
20}
21
22impl Serialize for DeleteChild {
23 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
24 where
25 S: Serializer,
26 {
27 match *self {
28 DeleteChild::Run(ref r) => {
29 let mut t = serializer.serialize_struct("Run", 2)?;
30 t.serialize_field("type", "run")?;
31 t.serialize_field("data", r)?;
32 t.end()
33 }
34 DeleteChild::CommentStart(ref r) => {
35 let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
36 t.serialize_field("type", "commentRangeStart")?;
37 t.serialize_field("data", r)?;
38 t.end()
39 }
40 DeleteChild::CommentEnd(ref r) => {
41 let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
42 t.serialize_field("type", "commentRangeEnd")?;
43 t.serialize_field("data", r)?;
44 t.end()
45 }
46 }
47 }
48}
49
50impl Default for Delete {
51 fn default() -> Delete {
52 Delete {
53 author: "unnamed".to_owned(),
54 date: "1970-01-01T00:00:00Z".to_owned(),
55 children: vec![],
56 }
57 }
58}
59
60impl Delete {
61 pub fn new() -> Delete {
62 Self {
63 children: vec![],
64 ..Default::default()
65 }
66 }
67
68 pub fn add_run(mut self, run: Run) -> Delete {
69 self.children.push(DeleteChild::Run(run));
70 self
71 }
72
73 pub fn add_comment_start(mut self, comment: Comment) -> Delete {
74 self.children
75 .push(DeleteChild::CommentStart(Box::new(CommentRangeStart::new(
76 comment,
77 ))));
78 self
79 }
80
81 pub fn add_comment_end(mut self, id: usize) -> Delete {
82 self.children
83 .push(DeleteChild::CommentEnd(CommentRangeEnd::new(id)));
84 self
85 }
86
87 pub fn author(mut self, author: impl Into<String>) -> Delete {
88 self.author = escape::escape(&author.into());
89 self
90 }
91
92 pub fn date(mut self, date: impl Into<String>) -> Delete {
93 self.date = date.into();
94 self
95 }
96}
97
98impl HistoryId for Delete {}
99
100impl BuildXML for Delete {
101 fn build_to<W: Write>(
102 &self,
103 stream: xml::writer::EventWriter<W>,
104 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
105 let id = self.generate();
106 XMLBuilder::from(stream)
107 .open_delete(&id, &self.author, &self.date)?
108 .apply_each(&self.children, |ch, b| match ch {
109 DeleteChild::Run(t) => b.add_child(t),
110 DeleteChild::CommentStart(c) => b.add_child(&c),
111 DeleteChild::CommentEnd(c) => b.add_child(c),
112 })?
113 .close()?
114 .into_inner()
115 }
116}
117
118#[cfg(test)]
119mod tests {
120
121 use super::*;
122 #[cfg(test)]
123 use pretty_assertions::assert_eq;
124 use std::str;
125
126 #[test]
127 fn test_delete_default() {
128 let b = Delete::new().add_run(Run::new()).build();
129 assert_eq!(
130 str::from_utf8(&b).unwrap(),
131 r#"<w:del w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:del>"#
132 );
133 }
134}