feat: fix core props
parent
fb7cdec1b6
commit
af75b6a41c
|
@ -2,30 +2,30 @@ use crate::documents::BuildXML;
|
|||
use crate::xml_builder::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CoreProps {
|
||||
config: CorePropsConfig,
|
||||
pub struct CoreProps<'a> {
|
||||
config: CorePropsConfig<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CorePropsConfig {
|
||||
created: Option<String>,
|
||||
creator: Option<String>,
|
||||
description: Option<String>,
|
||||
language: Option<String>,
|
||||
last_modified_by: Option<String>,
|
||||
modified: Option<String>,
|
||||
pub struct CorePropsConfig<'a> {
|
||||
created: Option<&'a str>,
|
||||
creator: Option<&'a str>,
|
||||
description: Option<&'a str>,
|
||||
language: Option<&'a str>,
|
||||
last_modified_by: Option<&'a str>,
|
||||
modified: Option<&'a str>,
|
||||
revision: Option<usize>,
|
||||
subject: Option<String>,
|
||||
title: Option<String>,
|
||||
subject: Option<&'a str>,
|
||||
title: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl CoreProps {
|
||||
pub(crate) fn new(config: CorePropsConfig) -> CoreProps {
|
||||
impl<'a> CoreProps<'a> {
|
||||
pub(crate) fn new(config: CorePropsConfig<'a>) -> CoreProps {
|
||||
CoreProps { config }
|
||||
}
|
||||
}
|
||||
|
||||
impl CorePropsConfig {
|
||||
impl<'a> CorePropsConfig<'a> {
|
||||
pub fn new() -> Self {
|
||||
CorePropsConfig {
|
||||
created: None,
|
||||
|
@ -41,7 +41,7 @@ impl CorePropsConfig {
|
|||
}
|
||||
}
|
||||
|
||||
impl BuildXML for CoreProps {
|
||||
impl<'a> BuildXML for CoreProps<'a> {
|
||||
fn build(&self) -> Vec<u8> {
|
||||
let b = XMLBuilder::new();
|
||||
let base = b.declaration(Some(true)).open_core_properties(
|
||||
|
@ -53,37 +53,39 @@ impl BuildXML for CoreProps {
|
|||
);
|
||||
|
||||
let convert = |v: usize| format!("{}", v);
|
||||
let default = || "";
|
||||
|
||||
// base.dcterms_created(
|
||||
// "dcterms:W3CDTF",
|
||||
// c.created.as_ref().map_or_else(default, |v| v),
|
||||
// )
|
||||
// .dc_creator(c.creator.as_ref().map_or_else(default, |v| v))
|
||||
// .dc_description(c.description.as_ref().map_or_else(default, |v| v))
|
||||
// .dc_language(c.language.as_ref().map_or_else(default, |v| v))
|
||||
// .cp_last_modified_by(c.last_modified_by.as_ref().map_or_else(default, |v| v))
|
||||
// .dcterms_modified(
|
||||
// "dcterms:W3CDTF",
|
||||
// c.modified.as_ref().map_or_else(default, |v| v),
|
||||
// )
|
||||
// .cp_revision(&c.revision.map_or_else(|| "".to_owned(), convert))
|
||||
// .dc_subject(c.subject.as_ref().map_or_else(default, |v| v))
|
||||
// .dc_title(c.title.as_ref().map_or_else(default, |v| v))
|
||||
// .close()
|
||||
// .build()
|
||||
|
||||
base.dcterms_created("dcterms:W3CDTF", "2019-09-13T19:25:28Z")
|
||||
.dc_creator("unknown")
|
||||
.dc_description("")
|
||||
.dc_language("")
|
||||
.cp_last_modified_by("unknown")
|
||||
.dcterms_modified("dcterms:W3CDTF", "2019-09-13T19:25:28Z")
|
||||
.cp_revision("1")
|
||||
.dc_subject("")
|
||||
.dc_title("")
|
||||
.close()
|
||||
.build()
|
||||
let mut base = base
|
||||
.dcterms_created(
|
||||
"dcterms:W3CDTF",
|
||||
self.config
|
||||
.created
|
||||
.map_or_else(|| "1970-01-01T00:00:00Z", |v| v),
|
||||
)
|
||||
.dc_creator(self.config.creator.map_or_else(|| "unknown", |v| v))
|
||||
.cp_last_modified_by(
|
||||
self.config
|
||||
.last_modified_by
|
||||
.map_or_else(|| "unknown", |v| v),
|
||||
)
|
||||
.dcterms_modified(
|
||||
"dcterms:W3CDTF",
|
||||
self.config
|
||||
.modified
|
||||
.map_or_else(|| "1970-01-01T00:00:00Z", |v| v),
|
||||
)
|
||||
.cp_revision(&self.config.revision.map_or_else(|| "1".to_owned(), convert));
|
||||
if let Some(v) = self.config.description {
|
||||
base = base.dc_description(v);
|
||||
}
|
||||
if let Some(v) = self.config.language {
|
||||
base = base.dc_language(v);
|
||||
}
|
||||
if let Some(v) = self.config.subject {
|
||||
base = base.dc_subject(v);
|
||||
}
|
||||
if let Some(v) = self.config.title {
|
||||
base = base.dc_title(v);
|
||||
}
|
||||
base.close().build()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,7 +98,7 @@ mod tests {
|
|||
use std::str;
|
||||
|
||||
#[test]
|
||||
fn test_default_doc_props_core_build() {
|
||||
fn test_default_doc_props_core() {
|
||||
let c = CoreProps::new(CorePropsConfig {
|
||||
created: None,
|
||||
creator: None,
|
||||
|
@ -113,49 +115,43 @@ mod tests {
|
|||
str::from_utf8(&b).unwrap(),
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<dcterms:created xsi:type="dcterms:W3CDTF">2019-09-13T19:25:28Z</dcterms:created>
|
||||
<dcterms:created xsi:type="dcterms:W3CDTF">1970-01-01T00:00:00Z</dcterms:created>
|
||||
<dc:creator>unknown</dc:creator>
|
||||
<dc:description></dc:description>
|
||||
<dc:language></dc:language>
|
||||
<cp:lastModifiedBy>unknown</cp:lastModifiedBy>
|
||||
<dcterms:modified xsi:type="dcterms:W3CDTF">2019-09-13T19:25:28Z</dcterms:modified>
|
||||
<dcterms:modified xsi:type="dcterms:W3CDTF">1970-01-01T00:00:00Z</dcterms:modified>
|
||||
<cp:revision>1</cp:revision>
|
||||
<dc:subject></dc:subject>
|
||||
<dc:title></dc:title>
|
||||
</cp:coreProperties>"#
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_configured_doc_props_core_build() {
|
||||
let c = CoreProps::new(CorePropsConfig {
|
||||
created: Some("2019-01-01".to_owned()),
|
||||
creator: Some("foo".to_owned()),
|
||||
description: Some("bar".to_owned()),
|
||||
language: Some("en".to_owned()),
|
||||
last_modified_by: Some("go".to_owned()),
|
||||
modified: Some("2019-01-01".to_owned()),
|
||||
revision: Some(1),
|
||||
subject: Some("subject".to_owned()),
|
||||
title: Some("title".to_owned()),
|
||||
});
|
||||
let b = c.build();
|
||||
assert_eq!(
|
||||
#[test]
|
||||
fn test_configured_doc_props_core_build() {
|
||||
let c = CoreProps::new(CorePropsConfig {
|
||||
created: Some("2019-01-01"),
|
||||
creator: Some("foo"),
|
||||
description: Some("bar"),
|
||||
language: Some("en"),
|
||||
last_modified_by: Some("go"),
|
||||
modified: Some("2019-01-01"),
|
||||
revision: Some(1),
|
||||
subject: Some("subject"),
|
||||
title: Some("title"),
|
||||
});
|
||||
let b = c.build();
|
||||
assert_eq!(
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<dcterms:created xsi:type="dcterms:W3CDTF">2019-01-01</dcterms:created>
|
||||
<dc:creator>foo</dc:creator>
|
||||
<dc:description>bar</dc:description>
|
||||
<dc:language>en</dc:language>
|
||||
<cp:lastModifiedBy>go</cp:lastModifiedBy>
|
||||
<dcterms:modified xsi:type="dcterms:W3CDTF">2019-01-01</dcterms:modified>
|
||||
<cp:revision>1</cp:revision>
|
||||
<dc:subject>subject</dc:subject>
|
||||
<dc:title>title</dc:title>
|
||||
</cp:coreProperties>"#
|
||||
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<dcterms:created xsi:type="dcterms:W3CDTF">2019-01-01</dcterms:created>
|
||||
<dc:creator>foo</dc:creator>
|
||||
<cp:lastModifiedBy>go</cp:lastModifiedBy>
|
||||
<dcterms:modified xsi:type="dcterms:W3CDTF">2019-01-01</dcterms:modified>
|
||||
<cp:revision>1</cp:revision>
|
||||
<dc:description>bar</dc:description>
|
||||
<dc:language>en</dc:language>
|
||||
<dc:subject>subject</dc:subject>
|
||||
<dc:title>title</dc:title>
|
||||
</cp:coreProperties>"#
|
||||
);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ pub use self::core::*;
|
|||
use crate::documents::BuildXML;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct DocProps {
|
||||
pub(crate) struct DocProps<'a> {
|
||||
app: AppProps,
|
||||
core: CoreProps,
|
||||
core: CoreProps<'a>,
|
||||
}
|
||||
|
||||
impl DocProps {
|
||||
impl<'a> DocProps<'a> {
|
||||
pub(crate) fn new(core_config: CorePropsConfig) -> DocProps {
|
||||
let app = AppProps::new();
|
||||
let core = CoreProps::new(core_config);
|
||||
|
|
|
@ -24,18 +24,18 @@ pub use styles::*;
|
|||
pub use xml_docx::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Docx {
|
||||
pub struct Docx<'a> {
|
||||
content_type: ContentTypes,
|
||||
rels: Rels,
|
||||
document_rels: DocumentRels,
|
||||
doc_props: DocProps,
|
||||
doc_props: DocProps<'a>,
|
||||
styles: Styles,
|
||||
document: Document,
|
||||
settings: Settings,
|
||||
font_table: FontTable,
|
||||
}
|
||||
|
||||
impl Default for Docx {
|
||||
impl<'a> Default for Docx<'a> {
|
||||
fn default() -> Self {
|
||||
let content_type = ContentTypes::new();
|
||||
let rels = Rels::new();
|
||||
|
@ -58,17 +58,17 @@ impl Default for Docx {
|
|||
}
|
||||
}
|
||||
|
||||
impl Docx {
|
||||
pub fn new() -> Docx {
|
||||
impl<'a> Docx<'a> {
|
||||
pub fn new() -> Docx<'a> {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
|
||||
pub fn add_paragraph(mut self, p: Paragraph) -> Docx<'a> {
|
||||
self.document = self.document.add_paragraph(p);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_table(mut self, t: Table) -> Docx {
|
||||
pub fn add_table(mut self, t: Table) -> Docx<'a> {
|
||||
self.document = self.document.add_table(t);
|
||||
self
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use wasm_bindgen::prelude::*;
|
|||
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug)]
|
||||
pub struct Docx(docx_core::Docx);
|
||||
pub struct Docx(docx_core::Docx<'static>);
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[allow(non_snake_case)]
|
||||
|
|
Loading…
Reference in New Issue