feat: Add core
parent
490efa47c2
commit
fee2dfeac7
|
@ -0,0 +1,3 @@
|
|||
pub trait BuildXML {
|
||||
fn build(&self) -> Vec<u8>;
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
use crate::documents::BuildXML;
|
||||
use crate::xml_builder::*;
|
||||
|
||||
pub struct ContentTypes {}
|
||||
|
@ -6,8 +7,10 @@ impl ContentTypes {
|
|||
pub fn new() -> ContentTypes {
|
||||
ContentTypes {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Vec<u8> {
|
||||
impl BuildXML for ContentTypes {
|
||||
fn build(&self) -> Vec<u8> {
|
||||
let b = XMLBuilder::new();
|
||||
b.declaration(None)
|
||||
.open_types("http://schemas.openxmlformats.org/package/2006/content-types")
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::documents::BuildXML;
|
||||
use crate::xml_builder::*;
|
||||
|
||||
pub struct AppProps {
|
||||
|
@ -19,8 +20,10 @@ impl AppProps {
|
|||
pub fn new(config: Option<AppPropsConfig>) -> AppProps {
|
||||
AppProps { config }
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn build(&self) -> Vec<u8> {
|
||||
impl BuildXML for AppProps {
|
||||
fn build(&self) -> Vec<u8> {
|
||||
let b = XMLBuilder::new();
|
||||
let base = b.declaration(Some(true)).open_properties(
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties",
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
use crate::documents::BuildXML;
|
||||
use crate::xml_builder::*;
|
||||
|
||||
pub struct CoreProps {
|
||||
config: Option<CorePropsConfig>,
|
||||
}
|
||||
|
||||
pub struct CorePropsConfig {
|
||||
created: Option<String>,
|
||||
creator: Option<String>,
|
||||
description: Option<String>,
|
||||
language: Option<String>,
|
||||
last_modified_by: Option<String>,
|
||||
modified: Option<String>,
|
||||
revision: Option<usize>,
|
||||
subject: Option<String>,
|
||||
title: Option<String>,
|
||||
}
|
||||
|
||||
impl CoreProps {
|
||||
pub(crate) fn new(config: Option<CorePropsConfig>) -> CoreProps {
|
||||
CoreProps { config }
|
||||
}
|
||||
}
|
||||
|
||||
impl BuildXML for CoreProps {
|
||||
fn build(&self) -> Vec<u8> {
|
||||
let b = XMLBuilder::new();
|
||||
let base = b.declaration(Some(true)).open_core_properties(
|
||||
"http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
|
||||
"http://purl.org/dc/elements/1.1/",
|
||||
"http://purl.org/dc/terms/",
|
||||
"http://purl.org/dc/dcmitype/",
|
||||
"http://www.w3.org/2001/XMLSchema-instance",
|
||||
);
|
||||
|
||||
let convert = |v: usize| format!("{}", v);
|
||||
let default = || "";
|
||||
|
||||
if let Some(c) = &self.config {
|
||||
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()
|
||||
} else {
|
||||
base.dcterms_created("dcterms:W3CDTF", "")
|
||||
.dc_creator("")
|
||||
.dc_description("")
|
||||
.dc_language("")
|
||||
.cp_last_modified_by("")
|
||||
.dcterms_modified("dcterms:W3CDTF", "")
|
||||
.cp_revision("")
|
||||
.dc_subject("")
|
||||
.dc_title("")
|
||||
.close()
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use std::str;
|
||||
|
||||
#[test]
|
||||
fn test_default_doc_props_core_build() {
|
||||
let c = CoreProps::new(None);
|
||||
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"></dcterms:created>
|
||||
<dc:creator></dc:creator>
|
||||
<dc:description></dc:description>
|
||||
<dc:language></dc:language>
|
||||
<cp:lastModifiedBy></cp:lastModifiedBy>
|
||||
<dcterms:modified xsi:type="dcterms:W3CDTF"></dcterms:modified>
|
||||
<cp:revision></cp:revision>
|
||||
<dc:subject></dc:subject>
|
||||
<dc:title></dc:title>
|
||||
</cp:coreProperties>"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_configured_doc_props_core_build() {
|
||||
let c = CoreProps::new(Some(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!(
|
||||
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>"#
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,14 +1,21 @@
|
|||
mod app;
|
||||
mod core;
|
||||
|
||||
pub use app::*;
|
||||
pub use self::app::*;
|
||||
pub use self::core::*;
|
||||
|
||||
pub(crate) struct DocProps {
|
||||
app: AppProps,
|
||||
core: CoreProps,
|
||||
}
|
||||
|
||||
impl DocProps {
|
||||
pub(crate) fn new(appConfig: Option<AppPropsConfig>) -> DocProps {
|
||||
let app = AppProps::new(appConfig);
|
||||
DocProps { app }
|
||||
pub(crate) fn new(
|
||||
app_config: Option<AppPropsConfig>,
|
||||
core_config: Option<CorePropsConfig>,
|
||||
) -> DocProps {
|
||||
let app = AppProps::new(app_config);
|
||||
let core = CoreProps::new(core_config);
|
||||
DocProps { app, core }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
mod build_xml;
|
||||
mod content_types;
|
||||
mod doc_props;
|
||||
mod rels;
|
||||
mod xml_document;
|
||||
|
||||
use build_xml::*;
|
||||
use content_types::*;
|
||||
use doc_props::*;
|
||||
use rels::*;
|
||||
|
@ -17,7 +19,7 @@ impl Document {
|
|||
pub fn new() -> Document {
|
||||
let content_type = ContentTypes::new();
|
||||
let rels = Rels::new();
|
||||
let doc_props = DocProps::new(None /* TODO: */);
|
||||
let doc_props = DocProps::new(None, None /* TODO: */);
|
||||
Document {
|
||||
content_type,
|
||||
rels,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::documents::BuildXML;
|
||||
use crate::xml_builder::*;
|
||||
|
||||
pub struct Rels {}
|
||||
|
@ -6,8 +7,10 @@ impl Rels {
|
|||
pub fn new() -> Rels {
|
||||
Rels {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Vec<u8> {
|
||||
impl BuildXML for Rels {
|
||||
fn build(&self) -> Vec<u8> {
|
||||
let b = XMLBuilder::new();
|
||||
b.declaration(None)
|
||||
.open_relationships("http://schemas.openxmlformats.org/package/2006/relationships")
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use super::Document;
|
||||
use crate::documents::BuildXML;
|
||||
|
||||
pub(crate) struct XMLDocument {
|
||||
content_type: Vec<u8>,
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
use super::XMLBuilder;
|
||||
|
||||
impl XMLBuilder {
|
||||
// i.e. <cp:properties xmlns:vt="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
opened_el!(
|
||||
open_core_properties,
|
||||
"cp:coreProperties",
|
||||
"xmlns:cp",
|
||||
"xmlns:dc",
|
||||
"xmlns:dcterms",
|
||||
"xmlns:dcmitype",
|
||||
"xmlns:xsi"
|
||||
);
|
||||
closed_el_with_child!(dcterms_created, "dcterms:created", "xsi:type");
|
||||
closed_el_with_child!(dc_creator, "dc:creator");
|
||||
closed_el_with_child!(dc_description, "dc:description");
|
||||
closed_el_with_child!(dc_language, "dc:language");
|
||||
closed_el_with_child!(cp_last_modified_by, "cp:lastModifiedBy");
|
||||
closed_el_with_child!(dcterms_modified, "dcterms:modified", "xsi:type");
|
||||
closed_el_with_child!(cp_revision, "cp:revision");
|
||||
closed_el_with_child!(dc_subject, "dc:subject");
|
||||
closed_el_with_child!(dc_title, "dc:title");
|
||||
}
|
|
@ -15,6 +15,30 @@ macro_rules! opened_el {
|
|||
self
|
||||
}
|
||||
};
|
||||
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr) => {
|
||||
pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str) -> Self {
|
||||
self.writer
|
||||
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2))
|
||||
.expect("should write to buf");
|
||||
self
|
||||
}
|
||||
};
|
||||
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr, $attr3: expr) => {
|
||||
pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, arg3: &str) -> Self {
|
||||
self.writer
|
||||
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2).attr($attr3, arg3))
|
||||
.expect("should write to buf");
|
||||
self
|
||||
}
|
||||
};
|
||||
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr, $attr3: expr, $attr4: expr) => {
|
||||
pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, arg3: &str, arg4: &str) -> Self {
|
||||
self.writer
|
||||
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2).attr($attr3, arg3).attr($attr4, arg4))
|
||||
.expect("should write to buf");
|
||||
self
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! closed_el_with_child {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#[macro_use]
|
||||
mod macros;
|
||||
mod core_properties;
|
||||
mod declaration;
|
||||
mod properties;
|
||||
mod relationship;
|
||||
|
|
Loading…
Reference in New Issue