From a27396e262d99d3ee21d075f1d07c2d5aae41ab6 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Sun, 15 Dec 2019 00:39:15 +0900 Subject: [PATCH] feat: Support date (#11) --- docx-core/src/documents/doc_props/core.rs | 10 ++++++++++ docx-core/src/documents/doc_props/mod.rs | 10 ++++++++++ docx-core/src/documents/mod.rs | 10 ++++++++++ docx-core/tests/lib.rs | 13 +++++++++++++ 4 files changed, 43 insertions(+) diff --git a/docx-core/src/documents/doc_props/core.rs b/docx-core/src/documents/doc_props/core.rs index b012584..7a4e466 100644 --- a/docx-core/src/documents/doc_props/core.rs +++ b/docx-core/src/documents/doc_props/core.rs @@ -23,6 +23,16 @@ impl CoreProps { pub(crate) fn new(config: CorePropsConfig) -> CoreProps { CoreProps { config } } + + pub fn created_at(mut self, date: &str) -> Self { + self.config.created = Some(date.to_owned()); + self + } + + pub fn updated_at(mut self, date: &str) -> Self { + self.config.modified = Some(date.to_owned()); + self + } } impl CorePropsConfig { diff --git a/docx-core/src/documents/doc_props/mod.rs b/docx-core/src/documents/doc_props/mod.rs index 8301a6f..e05b3aa 100644 --- a/docx-core/src/documents/doc_props/mod.rs +++ b/docx-core/src/documents/doc_props/mod.rs @@ -18,6 +18,16 @@ impl DocProps { DocProps { app, core } } + pub fn created_at(mut self, date: &str) -> Self { + self.core = self.core.created_at(date); + self + } + + pub fn updated_at(mut self, date: &str) -> Self { + self.core = self.core.updated_at(date); + self + } + pub(crate) fn build(&self) -> XMLDocProps { XMLDocProps { app: self.app.build(), diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index f108d59..f7ca25f 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -90,6 +90,16 @@ impl Docx { self } + pub fn created_at(mut self, date: &str) -> Self { + self.doc_props = self.doc_props.created_at(date); + self + } + + pub fn updated_at(mut self, date: &str) -> Self { + self.doc_props = self.doc_props.updated_at(date); + self + } + pub fn build(&mut self) -> XMLDocx { self.update_comments(); XMLDocx { diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index 5647b64..e2b4f52 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -402,3 +402,16 @@ pub fn vanish() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn date() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/date.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) + .created_at("2019-01-01T00:00:00Z") + .updated_at("2019-01-02T10:00:00Z") + .build() + .pack(file)?; + Ok(()) +}