docx_reader/documents/elements/
insert.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use super::*;

use crate::documents::Run;

#[derive(Debug, Clone, PartialEq)]
pub enum InsertChild {
	Run(Box<Run>),
	Delete(Delete),
}

impl Serialize for InsertChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			InsertChild::Run(ref r) => {
				let mut t = serializer.serialize_struct("Run", 2)?;
				t.serialize_field("type", "run")?;
				t.serialize_field("data", r)?;
				t.end()
			}
			InsertChild::Delete(ref r) => {
				let mut t = serializer.serialize_struct("Delete", 2)?;
				t.serialize_field("type", "delete")?;
				t.serialize_field("data", r)?;
				t.end()
			}
		}
	}
}

#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Insert {
	pub children: Vec<InsertChild>,
	pub author: String,
	pub date: String,
}

impl Default for Insert {
	fn default() -> Insert {
		Insert {
			author: "unnamed".to_owned(),
			date: "1970-01-01T00:00:00Z".to_owned(),
			children: vec![],
		}
	}
}

impl Insert {
	pub fn new(run: Run) -> Insert {
		Self {
			children: vec![InsertChild::Run(Box::new(run))],
			..Default::default()
		}
	}

	pub fn new_with_empty() -> Insert {
		Self {
			..Default::default()
		}
	}

	pub fn new_with_del(del: Delete) -> Insert {
		Self {
			children: vec![InsertChild::Delete(del)],
			..Default::default()
		}
	}

	pub fn add_run(mut self, run: Run) -> Insert {
		self.children.push(InsertChild::Run(Box::new(run)));
		self
	}

	pub fn add_delete(mut self, del: Delete) -> Insert {
		self.children.push(InsertChild::Delete(del));
		self
	}

	pub fn add_child(mut self, c: InsertChild) -> Insert {
		self.children.push(c);
		self
	}

	pub fn author(mut self, author: impl Into<String>) -> Insert {
		self.author = author.into();
		self
	}

	pub fn date(mut self, date: impl Into<String>) -> Insert {
		self.date = date.into();
		self
	}
}