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
main
bokuweb 2019-12-19 16:37:28 +09:00 committed by GitHub
parent 531314aa7c
commit 3a81e88a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 21 deletions

View File

@ -3,9 +3,9 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Delete {
author: String,
date: String,
run: Run,
pub author: String,
pub date: String,
pub run: Run,
}
impl Default for Delete {
@ -19,14 +19,26 @@ impl Default for Delete {
}
impl Delete {
pub fn new() -> Delete {
Default::default()
pub fn new(run: Run) -> Delete {
Self {
run,
..Default::default()
}
}
pub fn run(mut self, run: Run) -> Delete {
self.run = run;
self
}
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
}
}
impl HistoryId for Delete {}
@ -51,7 +63,7 @@ mod tests {
#[test]
fn test_delete_default() {
let b = Delete::new().build();
let b = Delete::new(Run::new()).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:del w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:del>"#

View File

@ -3,9 +3,9 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Insert {
author: String,
date: String,
run: Run,
pub run: Run,
pub author: String,
pub date: String,
}
impl Default for Insert {
@ -19,12 +19,20 @@ impl Default for Insert {
}
impl Insert {
pub fn new() -> Insert {
Default::default()
pub fn new(run: Run) -> Insert {
Self {
run,
..Default::default()
}
}
pub fn run(mut self, run: Run) -> Insert {
self.run = run;
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
}
}
@ -51,7 +59,7 @@ mod tests {
#[test]
fn test_ins_default() {
let b = Insert::new().build();
let b = Insert::new(Run::new()).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:ins w:id="123" w:author="unnamed" w:date="1970-01-01T00:00:00Z"><w:r><w:rPr /></w:r></w:ins>"#

View File

@ -223,8 +223,8 @@ pub fn history() -> Result<(), DocxError> {
Docx::new()
.add_paragraph(
Paragraph::new()
.add_insert(Insert::new().run(Run::new().add_text("Hello")))
.add_delete(Delete::new().run(Run::new().add_delete_text("World"))),
.add_insert(Insert::new(Run::new().add_text("Hello")))
.add_delete(Delete::new(Run::new().add_delete_text("World"))),
)
.build()
.pack(file)?;

View File

@ -7,12 +7,22 @@ use wasm_bindgen::prelude::*;
pub struct Delete(docx_core::Delete);
#[wasm_bindgen(js_name = createDelete)]
pub fn create_delete() -> Delete {
Delete(docx_core::Delete::new())
pub fn create_delete(run: Run) -> Delete {
Delete(docx_core::Delete::new(run.take()))
}
impl Delete {
pub fn take(self) -> docx_core::Delete {
self.0
}
pub fn author(mut self, author: String) -> Delete {
self.0.author = author;
self
}
pub fn date(mut self, date: String) -> Delete {
self.0.date = date;
self
}
}

View File

@ -7,12 +7,22 @@ use wasm_bindgen::prelude::*;
pub struct Insert(docx_core::Insert);
#[wasm_bindgen(js_name = createInsert)]
pub fn create_insert() -> Insert {
Insert(docx_core::Insert::new())
pub fn create_insert(run: Run) -> Insert {
Insert(docx_core::Insert::new(run.take()))
}
impl Insert {
pub fn take(self) -> docx_core::Insert {
self.0
}
pub fn author(mut self, author: String) -> Insert {
self.0.author = author;
self
}
pub fn date(mut self, date: String) -> Insert {
self.0.date = date;
self
}
}