Skip to content

Commit

Permalink
feat: Use String for wasm
Browse files Browse the repository at this point in the history
This is because wasm-bindgen does not support 'static for function
Pleaase see rustwasm/wasm-bindgen#1187
  • Loading branch information
bokuweb committed Dec 8, 2019
1 parent 9a1b92f commit 4450e75
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 301 deletions.
12 changes: 6 additions & 6 deletions docx-core/src/documents/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug)]
pub struct Comments<'a> {
comments: Vec<Comment<'a>>,
pub struct Comments {
comments: Vec<Comment>,
}

impl<'a> Comments<'a> {
impl Comments {
pub fn new() -> Self {
Default::default()
}
pub(crate) fn add_comments(&mut self, comments: Vec<Comment<'a>>) {
pub(crate) fn add_comments(&mut self, comments: Vec<Comment>) {
self.comments = comments;
}
}

impl<'a> Default for Comments<'a> {
impl Default for Comments {
fn default() -> Self {
Self { comments: vec![] }
}
}

impl<'a> BuildXML for Comments<'a> {
impl BuildXML for Comments {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new().declaration(Some(true)).open_comments();
for c in &self.comments {
Expand Down
78 changes: 43 additions & 35 deletions docx-core/src/documents/doc_props/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug)]
pub struct CoreProps<'a> {
config: CorePropsConfig<'a>,
pub struct CoreProps {
config: CorePropsConfig,
}

#[derive(Debug)]
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>,
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<&'a str>,
title: Option<&'a str>,
subject: Option<String>,
title: Option<String>,
}

impl<'a> CoreProps<'a> {
pub(crate) fn new(config: CorePropsConfig<'a>) -> CoreProps {
impl CoreProps {
pub(crate) fn new(config: CorePropsConfig) -> CoreProps {
CoreProps { config }
}
}

impl<'a> CorePropsConfig<'a> {
impl CorePropsConfig {
pub fn new() -> Self {
CorePropsConfig {
created: None,
Expand All @@ -41,7 +41,7 @@ impl<'a> CorePropsConfig<'a> {
}
}

impl<'a> BuildXML for CoreProps<'a> {
impl BuildXML for CoreProps {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let base = b.declaration(Some(true)).open_core_properties(
Expand All @@ -58,32 +58,40 @@ impl<'a> BuildXML for CoreProps<'a> {
"dcterms:W3CDTF",
self.config
.created
.map_or_else(|| "1970-01-01T00:00:00Z", |v| v),
.as_ref()
.map_or_else(|| "1970-01-01T00:00:00Z", |v| &v),
)
.dc_creator(
self.config
.creator
.as_ref()
.map_or_else(|| "unknown", |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),
.as_ref()
.map_or_else(|| "unknown", |v| &v),
)
.dcterms_modified(
"dcterms:W3CDTF",
self.config
.modified
.map_or_else(|| "1970-01-01T00:00:00Z", |v| v),
.as_ref()
.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.description.as_ref() {
base = base.dc_description(&v);
}
if let Some(v) = self.config.language {
base = base.dc_language(v);
if let Some(v) = self.config.language.as_ref() {
base = base.dc_language(&v);
}
if let Some(v) = self.config.subject {
base = base.dc_subject(v);
if let Some(v) = self.config.subject.as_ref() {
base = base.dc_subject(&v);
}
if let Some(v) = self.config.title {
base = base.dc_title(v);
if let Some(v) = self.config.title.as_ref() {
base = base.dc_title(&v);
}
base.close().build()
}
Expand Down Expand Up @@ -127,15 +135,15 @@ mod tests {
#[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"),
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"),
title: Some("title"),
subject: Some("subject".to_owned()),
title: Some("title".to_owned()),
});
let b = c.build();
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions docx-core/src/documents/doc_props/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pub use self::core::*;
use crate::documents::BuildXML;

#[derive(Debug)]
pub(crate) struct DocProps<'a> {
pub(crate) struct DocProps {
app: AppProps,
core: CoreProps<'a>,
core: CoreProps,
}

impl<'a> DocProps<'a> {
impl DocProps {
pub(crate) fn new(core_config: CorePropsConfig) -> DocProps {
let app = AppProps::new();
let core = CoreProps::new(core_config);
Expand Down
22 changes: 11 additions & 11 deletions docx-core/src/documents/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug)]
pub struct Document<'a> {
pub(crate) children: Vec<DocumentChild<'a>>,
pub struct Document {
pub(crate) children: Vec<DocumentChild>,
}

#[derive(Debug, Clone)]
pub enum DocumentChild<'a> {
Paragraph(Paragraph<'a>),
Table(Table<'a>),
pub enum DocumentChild {
Paragraph(Paragraph),
Table(Table),
}

impl<'a> Document<'a> {
pub fn new() -> Document<'a> {
impl Document {
pub fn new() -> Document {
Default::default()
}

pub fn add_paragraph(mut self, p: Paragraph<'a>) -> Self {
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
self.children.push(DocumentChild::Paragraph(p));
self
}

pub fn add_table(mut self, t: Table<'a>) -> Self {
pub fn add_table(mut self, t: Table) -> Self {
self.children.push(DocumentChild::Table(t));
self
}
}

impl<'a> Default for Document<'a> {
impl Default for Document {
fn default() -> Self {
Self {
children: Vec::new(),
}
}
}

impl<'a> BuildXML for Document<'a> {
impl BuildXML for Document {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new()
.declaration(Some(true))
Expand Down
12 changes: 6 additions & 6 deletions docx-core/src/documents/elements/bookmark_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct BookmarkEnd<'a> {
id: &'a str,
pub struct BookmarkEnd {
id: String,
}

impl<'a> BookmarkEnd<'a> {
pub fn new(id: &'a str) -> BookmarkEnd<'a> {
BookmarkEnd { id }
impl BookmarkEnd {
pub fn new(id: impl Into<String>) -> BookmarkEnd {
BookmarkEnd { id: id.into() }
}
}

impl<'a> BuildXML for BookmarkEnd<'a> {
impl BuildXML for BookmarkEnd {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.bookmark_end(&self.id).build()
Expand Down
17 changes: 10 additions & 7 deletions docx-core/src/documents/elements/bookmark_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct BookmarkStart<'a> {
id: &'a str,
name: &'a str,
pub struct BookmarkStart {
id: String,
name: String,
}

impl<'a> BookmarkStart<'a> {
pub fn new(id: &'a str, name: &'a str) -> BookmarkStart<'a> {
BookmarkStart { id, name }
impl BookmarkStart {
pub fn new(id: impl Into<String>, name: impl Into<String>) -> BookmarkStart {
BookmarkStart {
id: id.into(),
name: name.into(),
}
}
}

impl<'a> BuildXML for BookmarkStart<'a> {
impl BuildXML for BookmarkStart {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.bookmark_start(&self.id, &self.name).build()
Expand Down
44 changes: 22 additions & 22 deletions docx-core/src/documents/elements/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@ use crate::documents::{BuildXML, Paragraph};
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct Comment<'a> {
id: &'a str,
author: &'a str,
date: &'a str,
paragraph: Paragraph<'a>,
pub struct Comment {
id: String,
author: String,
date: String,
paragraph: Paragraph,
}

impl<'a> Default for Comment<'a> {
fn default() -> Comment<'a> {
impl Default for Comment {
fn default() -> Comment {
Comment {
id: "invalidId",
author: "unnamed",
date: "1970-01-01T00:00:00Z",
id: "invalidId".to_owned(),
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
paragraph: Paragraph::new(),
}
}
}

impl<'a> Comment<'a> {
pub fn new(id: &'a str) -> Comment<'a> {
impl Comment {
pub fn new(id: impl Into<String>) -> Comment {
Self {
id,
id: id.into(),
..Default::default()
}
}

pub fn author(mut self, author: &'a str) -> Comment<'a> {
self.author = author;
pub fn author(mut self, author: impl Into<String>) -> Comment {
self.author = author.into();
self
}

pub fn date(mut self, date: &'a str) -> Comment<'a> {
self.date = date;
pub fn date(mut self, date: impl Into<String>) -> Comment {
self.date = date.into();
self
}

pub fn paragraph(mut self, p: Paragraph<'a>) -> Comment<'a> {
pub fn paragraph(mut self, p: Paragraph) -> Comment {
self.paragraph = p;
self
}

pub fn id(&self) -> &'a str {
self.id
pub fn id(&self) -> String {
self.id.clone()
}
}

impl<'a> BuildXML for Comment<'a> {
impl BuildXML for Comment {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_comment(&self.id, self.author, self.date, "")
.open_comment(&self.id, &self.author, &self.date, "")
.add_child(&self.paragraph)
.close()
.build()
Expand Down
12 changes: 6 additions & 6 deletions docx-core/src/documents/elements/comment_range_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct CommentRangeEnd<'a> {
id: &'a str,
pub struct CommentRangeEnd {
id: String,
}

impl<'a> CommentRangeEnd<'a> {
pub fn new(id: &'a str) -> CommentRangeEnd<'a> {
CommentRangeEnd { id }
impl CommentRangeEnd {
pub fn new(id: impl Into<String>) -> CommentRangeEnd {
CommentRangeEnd { id: id.into() }
}
}

impl<'a> BuildXML for CommentRangeEnd<'a> {
impl BuildXML for CommentRangeEnd {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.open_run()
Expand Down
Loading

0 comments on commit 4450e75

Please sign in to comment.