Skip to content

Commit 3a81e88

Browse files
authored
fix: Add run, author and date to insert and delete (#20)
* fix: Add run, author and date to insert and delete * fix:insert / delete interface
1 parent 531314a commit 3a81e88

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

docx-core/src/documents/elements/delete.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::xml_builder::*;
33

44
#[derive(Debug, Clone)]
55
pub struct Delete {
6-
author: String,
7-
date: String,
8-
run: Run,
6+
pub author: String,
7+
pub date: String,
8+
pub run: Run,
99
}
1010

1111
impl Default for Delete {
@@ -19,14 +19,26 @@ impl Default for Delete {
1919
}
2020

2121
impl Delete {
22-
pub fn new() -> Delete {
23-
Default::default()
22+
pub fn new(run: Run) -> Delete {
23+
Self {
24+
run,
25+
..Default::default()
26+
}
2427
}
25-
2628
pub fn run(mut self, run: Run) -> Delete {
2729
self.run = run;
2830
self
2931
}
32+
33+
pub fn author(mut self, author: impl Into<String>) -> Delete {
34+
self.author = author.into();
35+
self
36+
}
37+
38+
pub fn date(mut self, date: impl Into<String>) -> Delete {
39+
self.date = date.into();
40+
self
41+
}
3042
}
3143

3244
impl HistoryId for Delete {}
@@ -51,7 +63,7 @@ mod tests {
5163

5264
#[test]
5365
fn test_delete_default() {
54-
let b = Delete::new().build();
66+
let b = Delete::new(Run::new()).build();
5567
assert_eq!(
5668
str::from_utf8(&b).unwrap(),
5769
r#"<w:del w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:del>"#

docx-core/src/documents/elements/insert.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::xml_builder::*;
33

44
#[derive(Debug, Clone)]
55
pub struct Insert {
6-
author: String,
7-
date: String,
8-
run: Run,
6+
pub run: Run,
7+
pub author: String,
8+
pub date: String,
99
}
1010

1111
impl Default for Insert {
@@ -19,12 +19,20 @@ impl Default for Insert {
1919
}
2020

2121
impl Insert {
22-
pub fn new() -> Insert {
23-
Default::default()
22+
pub fn new(run: Run) -> Insert {
23+
Self {
24+
run,
25+
..Default::default()
26+
}
27+
}
28+
29+
pub fn author(mut self, author: impl Into<String>) -> Insert {
30+
self.author = author.into();
31+
self
2432
}
2533

26-
pub fn run(mut self, run: Run) -> Insert {
27-
self.run = run;
34+
pub fn date(mut self, date: impl Into<String>) -> Insert {
35+
self.date = date.into();
2836
self
2937
}
3038
}
@@ -51,7 +59,7 @@ mod tests {
5159

5260
#[test]
5361
fn test_ins_default() {
54-
let b = Insert::new().build();
62+
let b = Insert::new(Run::new()).build();
5563
assert_eq!(
5664
str::from_utf8(&b).unwrap(),
5765
r#"<w:ins w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:ins>"#

docx-core/tests/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ pub fn history() -> Result<(), DocxError> {
223223
Docx::new()
224224
.add_paragraph(
225225
Paragraph::new()
226-
.add_insert(Insert::new().run(Run::new().add_text("Hello")))
227-
.add_delete(Delete::new().run(Run::new().add_delete_text("World"))),
226+
.add_insert(Insert::new(Run::new().add_text("Hello")))
227+
.add_delete(Delete::new(Run::new().add_delete_text("World"))),
228228
)
229229
.build()
230230
.pack(file)?;

docx-wasm/src/delete.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ use wasm_bindgen::prelude::*;
77
pub struct Delete(docx_core::Delete);
88

99
#[wasm_bindgen(js_name = createDelete)]
10-
pub fn create_delete() -> Delete {
11-
Delete(docx_core::Delete::new())
10+
pub fn create_delete(run: Run) -> Delete {
11+
Delete(docx_core::Delete::new(run.take()))
1212
}
1313

1414
impl Delete {
1515
pub fn take(self) -> docx_core::Delete {
1616
self.0
1717
}
18+
19+
pub fn author(mut self, author: String) -> Delete {
20+
self.0.author = author;
21+
self
22+
}
23+
24+
pub fn date(mut self, date: String) -> Delete {
25+
self.0.date = date;
26+
self
27+
}
1828
}

docx-wasm/src/insert.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ use wasm_bindgen::prelude::*;
77
pub struct Insert(docx_core::Insert);
88

99
#[wasm_bindgen(js_name = createInsert)]
10-
pub fn create_insert() -> Insert {
11-
Insert(docx_core::Insert::new())
10+
pub fn create_insert(run: Run) -> Insert {
11+
Insert(docx_core::Insert::new(run.take()))
1212
}
1313

1414
impl Insert {
1515
pub fn take(self) -> docx_core::Insert {
1616
self.0
1717
}
18+
19+
pub fn author(mut self, author: String) -> Insert {
20+
self.0.author = author;
21+
self
22+
}
23+
24+
pub fn date(mut self, date: String) -> Insert {
25+
self.0.date = date;
26+
self
27+
}
1828
}

0 commit comments

Comments
 (0)