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)] #[derive(Debug, Clone)]
pub struct Delete { pub struct Delete {
author: String, pub author: String,
date: String, pub date: String,
run: Run, pub run: Run,
} }
impl Default for Delete { impl Default for Delete {
@ -19,14 +19,26 @@ impl Default for Delete {
} }
impl Delete { impl Delete {
pub fn new() -> Delete { pub fn new(run: Run) -> Delete {
Default::default() Self {
run,
..Default::default()
}
} }
pub fn run(mut self, run: Run) -> Delete { pub fn run(mut self, run: Run) -> Delete {
self.run = run; self.run = run;
self 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 {} impl HistoryId for Delete {}
@ -51,7 +63,7 @@ mod tests {
#[test] #[test]
fn test_delete_default() { fn test_delete_default() {
let b = Delete::new().build(); let b = Delete::new(Run::new()).build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), 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>"# 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)] #[derive(Debug, Clone)]
pub struct Insert { pub struct Insert {
author: String, pub run: Run,
date: String, pub author: String,
run: Run, pub date: String,
} }
impl Default for Insert { impl Default for Insert {
@ -19,12 +19,20 @@ impl Default for Insert {
} }
impl Insert { impl Insert {
pub fn new() -> Insert { pub fn new(run: Run) -> Insert {
Default::default() Self {
run,
..Default::default()
}
} }
pub fn run(mut self, run: Run) -> Insert { pub fn author(mut self, author: impl Into<String>) -> Insert {
self.run = run; self.author = author.into();
self
}
pub fn date(mut self, date: impl Into<String>) -> Insert {
self.date = date.into();
self self
} }
} }
@ -51,7 +59,7 @@ mod tests {
#[test] #[test]
fn test_ins_default() { fn test_ins_default() {
let b = Insert::new().build(); let b = Insert::new(Run::new()).build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), 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>"# 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() Docx::new()
.add_paragraph( .add_paragraph(
Paragraph::new() Paragraph::new()
.add_insert(Insert::new().run(Run::new().add_text("Hello"))) .add_insert(Insert::new(Run::new().add_text("Hello")))
.add_delete(Delete::new().run(Run::new().add_delete_text("World"))), .add_delete(Delete::new(Run::new().add_delete_text("World"))),
) )
.build() .build()
.pack(file)?; .pack(file)?;

View File

@ -7,12 +7,22 @@ use wasm_bindgen::prelude::*;
pub struct Delete(docx_core::Delete); pub struct Delete(docx_core::Delete);
#[wasm_bindgen(js_name = createDelete)] #[wasm_bindgen(js_name = createDelete)]
pub fn create_delete() -> Delete { pub fn create_delete(run: Run) -> Delete {
Delete(docx_core::Delete::new()) Delete(docx_core::Delete::new(run.take()))
} }
impl Delete { impl Delete {
pub fn take(self) -> docx_core::Delete { pub fn take(self) -> docx_core::Delete {
self.0 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); pub struct Insert(docx_core::Insert);
#[wasm_bindgen(js_name = createInsert)] #[wasm_bindgen(js_name = createInsert)]
pub fn create_insert() -> Insert { pub fn create_insert(run: Run) -> Insert {
Insert(docx_core::Insert::new()) Insert(docx_core::Insert::new(run.take()))
} }
impl Insert { impl Insert {
pub fn take(self) -> docx_core::Insert { pub fn take(self) -> docx_core::Insert {
self.0 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
}
} }