Use string for wasm (#1)

* feat: Add numberings

* feat: Use String for wasm

This is because wasm-bindgen does not support 'static for function
Pleaase see https://github.com/rustwasm/wasm-bindgen/issues/1187
main
bokuweb 2019-12-09 04:14:27 +09:00 committed by GitHub
parent 182f82e9a0
commit 73001dc720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 339 additions and 278 deletions

View File

@ -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 {

View File

@ -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,
@ -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(
@ -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()
}
@ -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!(

View File

@ -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);

View File

@ -3,33 +3,33 @@ 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(),
@ -37,7 +37,7 @@ impl<'a> Default for Document<'a> {
}
}
impl<'a> BuildXML for Document<'a> {
impl BuildXML for Document {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new()
.declaration(Some(true))

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -3,25 +3,25 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct CommentRangeStart<'a> {
id: &'a str,
comment: Comment<'a>,
pub struct CommentRangeStart {
id: String,
comment: Comment,
}
impl<'a> CommentRangeStart<'a> {
pub fn new(comment: Comment<'a>) -> CommentRangeStart<'a> {
impl CommentRangeStart {
pub fn new(comment: Comment) -> CommentRangeStart {
CommentRangeStart {
id: comment.id(),
comment,
}
}
pub(crate) fn comment(&self) -> Comment<'a> {
pub(crate) fn comment(&self) -> Comment {
self.comment.clone()
}
}
impl<'a> BuildXML for CommentRangeStart<'a> {
impl BuildXML for CommentRangeStart {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.comment_range_start(&self.id).build()

View File

@ -2,39 +2,39 @@ use crate::documents::{BuildXML, HistoryId, Run};
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Delete<'a> {
author: &'a str,
date: &'a str,
run: Run<'a>,
pub struct Delete {
author: String,
date: String,
run: Run,
}
impl<'a> Default for Delete<'a> {
fn default() -> Delete<'a> {
impl Default for Delete {
fn default() -> Delete {
Delete {
author: "unnamed",
date: "1970-01-01T00:00:00Z",
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
run: Run::new(),
}
}
}
impl<'a> Delete<'a> {
pub fn new() -> Delete<'a> {
impl Delete {
pub fn new() -> Delete {
Default::default()
}
pub fn run(mut self, run: Run<'a>) -> Delete<'a> {
pub fn run(mut self, run: Run) -> Delete {
self.run = run;
self
}
}
impl<'a> HistoryId for Delete<'a> {}
impl HistoryId for Delete {}
impl<'a> BuildXML for Delete<'a> {
impl BuildXML for Delete {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_delete(&self.generate(), self.author, self.date)
.open_delete(&self.generate(), &self.author, &self.date)
.add_child(&self.run)
.close()
.build()

View File

@ -2,39 +2,39 @@ use crate::documents::{BuildXML, HistoryId, Run};
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Insert<'a> {
author: &'a str,
date: &'a str,
run: Run<'a>,
pub struct Insert {
author: String,
date: String,
run: Run,
}
impl<'a> Default for Insert<'a> {
fn default() -> Insert<'a> {
impl Default for Insert {
fn default() -> Insert {
Insert {
author: "unnamed",
date: "1970-01-01T00:00:00Z",
author: "unnamed".to_owned(),
date: "1970-01-01T00:00:00Z".to_owned(),
run: Run::new(),
}
}
}
impl<'a> Insert<'a> {
pub fn new() -> Insert<'a> {
impl Insert {
pub fn new() -> Insert {
Default::default()
}
pub fn run(mut self, run: Run<'a>) -> Insert<'a> {
pub fn run(mut self, run: Run) -> Insert {
self.run = run;
self
}
}
impl<'a> HistoryId for Insert<'a> {}
impl HistoryId for Insert {}
impl<'a> BuildXML for Insert<'a> {
impl BuildXML for Insert {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_insert(&self.generate(), self.author, self.date)
.open_insert(&self.generate(), &self.author, &self.date)
.add_child(&self.run)
.close()
.build()

View File

@ -3,23 +3,23 @@ use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Level<'a> {
pub struct Level {
level: usize,
start: Start,
format: NumberFormat<'a>,
text: LevelText<'a>,
jc: LevelJc<'a>,
format: NumberFormat,
text: LevelText,
jc: LevelJc,
paragraph_property: ParagraphProperty,
}
impl<'a> Level<'a> {
impl Level {
pub fn new(
level: usize,
start: Start,
format: NumberFormat<'a>,
text: LevelText<'a>,
jc: LevelJc<'a>,
) -> Level<'a> {
format: NumberFormat,
text: LevelText,
jc: LevelJc,
) -> Level {
Self {
level,
start,
@ -36,7 +36,7 @@ impl<'a> Level<'a> {
}
}
impl<'a> BuildXML for Level<'a> {
impl BuildXML for Level {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_level(&format!("{}", self.level))

View File

@ -2,20 +2,20 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct LevelJc<'a> {
val: &'a str,
pub struct LevelJc {
val: String,
}
impl<'a> LevelJc<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
impl LevelJc {
pub fn new(val: impl Into<String>) -> Self {
Self { val: val.into() }
}
}
impl<'a> BuildXML for LevelJc<'a> {
impl BuildXML for LevelJc {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.level_justification(self.val).build()
b.level_justification(&self.val).build()
}
}

View File

@ -2,20 +2,20 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct LevelText<'a> {
val: &'a str,
pub struct LevelText {
val: String,
}
impl<'a> LevelText<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
impl LevelText {
pub fn new(val: impl Into<String>) -> Self {
Self { val: val.into() }
}
}
impl<'a> BuildXML for LevelText<'a> {
impl BuildXML for LevelText {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.level_text(self.val).build()
b.level_text(&self.val).build()
}
}

View File

@ -2,20 +2,20 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct NumberFormat<'a> {
val: &'a str,
pub struct NumberFormat {
val: String,
}
impl<'a> NumberFormat<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
impl NumberFormat {
pub fn new(val: impl Into<String>) -> Self {
Self { val: val.into() }
}
}
impl<'a> BuildXML for NumberFormat<'a> {
impl BuildXML for NumberFormat {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.number_format(self.val).build()
b.number_format(&self.val).build()
}
}

View File

@ -2,23 +2,23 @@ use crate::documents::{BuildXML, Level};
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Numbering<'a> {
pub struct Numbering {
id: usize,
levels: Vec<Level<'a>>,
levels: Vec<Level>,
}
impl<'a> Numbering<'a> {
impl Numbering {
pub fn new(id: usize) -> Self {
Self { id, levels: vec![] }
}
pub fn add_level(mut self, level: Level<'a>) -> Self {
pub fn add_level(mut self, level: Level) -> Self {
self.levels.push(level);
self
}
}
impl<'a> BuildXML for Numbering<'a> {
impl BuildXML for Numbering {
fn build(&self) -> Vec<u8> {
let id = format!("{}", self.id);
let mut b = XMLBuilder::new();

View File

@ -4,13 +4,13 @@ use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Paragraph<'a> {
pub(crate) children: Vec<ParagraphChild<'a>>,
pub struct Paragraph {
pub(crate) children: Vec<ParagraphChild>,
property: ParagraphProperty,
attrs: Vec<(String, String)>,
}
impl<'a> Default for Paragraph<'a> {
impl Default for Paragraph {
fn default() -> Self {
Self {
children: Vec::new(),
@ -21,17 +21,17 @@ impl<'a> Default for Paragraph<'a> {
}
#[derive(Debug, Clone)]
pub enum ParagraphChild<'a> {
Run(Run<'a>),
Insert(Insert<'a>),
Delete(Delete<'a>),
BookmarkStart(BookmarkStart<'a>),
BookmarkEnd(BookmarkEnd<'a>),
CommentStart(CommentRangeStart<'a>),
CommentEnd(CommentRangeEnd<'a>),
pub enum ParagraphChild {
Run(Run),
Insert(Insert),
Delete(Delete),
BookmarkStart(BookmarkStart),
BookmarkEnd(BookmarkEnd),
CommentStart(CommentRangeStart),
CommentEnd(CommentRangeEnd),
}
impl<'a> BuildXML for ParagraphChild<'a> {
impl BuildXML for ParagraphChild {
fn build(&self) -> Vec<u8> {
match self {
ParagraphChild::Run(v) => v.build(),
@ -45,8 +45,8 @@ impl<'a> BuildXML for ParagraphChild<'a> {
}
}
impl<'a> Paragraph<'a> {
pub fn new() -> Paragraph<'a> {
impl Paragraph {
pub fn new() -> Paragraph {
Default::default()
}
@ -54,39 +54,43 @@ impl<'a> Paragraph<'a> {
&self.children
}
pub fn add_run(mut self, run: Run<'a>) -> Paragraph<'a> {
pub fn add_run(mut self, run: Run) -> Paragraph {
self.children.push(ParagraphChild::Run(run));
self
}
pub fn add_insert(mut self, insert: Insert<'a>) -> Paragraph<'a> {
pub fn add_insert(mut self, insert: Insert) -> Paragraph {
self.children.push(ParagraphChild::Insert(insert));
self
}
pub fn add_delete(mut self, delete: Delete<'a>) -> Paragraph<'a> {
pub fn add_delete(mut self, delete: Delete) -> Paragraph {
self.children.push(ParagraphChild::Delete(delete));
self
}
pub fn add_attr(mut self, key: impl Into<String>, val: impl Into<String>) -> Paragraph<'a> {
pub fn add_attr(mut self, key: impl Into<String>, val: impl Into<String>) -> Paragraph {
self.attrs.push((key.into(), val.into()));
self
}
pub fn add_bookmark_start(mut self, id: &'a str, name: &'a str) -> Paragraph<'a> {
pub fn add_bookmark_start(
mut self,
id: impl Into<String>,
name: impl Into<String>,
) -> Paragraph {
self.children
.push(ParagraphChild::BookmarkStart(BookmarkStart::new(id, name)));
self
}
pub fn add_bookmark_end(mut self, id: &'a str) -> Paragraph<'a> {
pub fn add_bookmark_end(mut self, id: impl Into<String>) -> Paragraph {
self.children
.push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id)));
self
}
pub fn add_comment_start(mut self, comment: Comment<'a>) -> Paragraph<'a> {
pub fn add_comment_start(mut self, comment: Comment) -> Paragraph {
self.children
.push(ParagraphChild::CommentStart(CommentRangeStart::new(
comment,
@ -94,27 +98,23 @@ impl<'a> Paragraph<'a> {
self
}
pub fn add_comment_end(mut self, id: &'a str) -> Paragraph<'a> {
pub fn add_comment_end(mut self, id: impl Into<String>) -> Paragraph {
self.children
.push(ParagraphChild::CommentEnd(CommentRangeEnd::new(id)));
self
}
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph<'a> {
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph {
self.property = self.property.align(alignment_type);
self
}
pub fn style(mut self, style_id: &str) -> Paragraph<'a> {
pub fn style(mut self, style_id: &str) -> Paragraph {
self.property = self.property.style(style_id);
self
}
pub fn indent(
mut self,
left: usize,
special_indent: Option<SpecialIndentType>,
) -> Paragraph<'a> {
pub fn indent(mut self, left: usize, special_indent: Option<SpecialIndentType>) -> Paragraph {
self.property = self.property.indent(left, special_indent);
self
}
@ -125,7 +125,7 @@ impl<'a> Paragraph<'a> {
}
}
impl<'a> BuildXML for Paragraph<'a> {
impl BuildXML for Paragraph {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_paragraph(&self.attrs)

View File

@ -1,6 +1,4 @@
use super::{
Indent, IndentLevel, Justification, NumberingId, NumberingProperty, ParagraphStyle, RunProperty,
};
use super::*;
use crate::documents::BuildXML;
use crate::types::{AlignmentType, SpecialIndentType};
use crate::xml_builder::*;

View File

@ -4,12 +4,12 @@ use crate::types::BreakType;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Run<'a> {
pub struct Run {
run_property: RunProperty,
children: Vec<RunChild<'a>>,
children: Vec<RunChild>,
}
impl<'a> Default for Run<'a> {
impl Default for Run {
fn default() -> Self {
let run_property = RunProperty::new();
Self {
@ -20,72 +20,72 @@ impl<'a> Default for Run<'a> {
}
#[derive(Debug, Clone)]
pub enum RunChild<'a> {
Text(Text<'a>),
pub enum RunChild {
Text(Text),
DeleteText(DeleteText),
Tab(Tab),
Break(Break),
}
impl<'a> Run<'a> {
pub fn new() -> Run<'a> {
impl Run {
pub fn new() -> Run {
Run {
..Default::default()
}
}
pub fn add_text(mut self, text: &'a str) -> Run<'a> {
pub fn add_text(mut self, text: impl Into<String>) -> Run {
self.children.push(RunChild::Text(Text::new(text)));
self
}
pub fn add_delete_text(mut self, text: &'a str) -> Run<'a> {
pub fn add_delete_text(mut self, text: impl Into<String>) -> Run {
self.children.push(RunChild::Text(Text::new(text)));
self
}
pub fn add_tab(mut self) -> Run<'a> {
pub fn add_tab(mut self) -> Run {
self.children.push(RunChild::Tab(Tab::new()));
self
}
pub fn add_break(mut self, break_type: BreakType) -> Run<'a> {
pub fn add_break(mut self, break_type: BreakType) -> Run {
self.children.push(RunChild::Break(Break::new(break_type)));
self
}
pub fn size(mut self, size: usize) -> Run<'a> {
pub fn size(mut self, size: usize) -> Run {
self.run_property = self.run_property.size(size);
self
}
pub fn color(mut self, color: &'a str) -> Run<'a> {
pub fn color(mut self, color: impl Into<String>) -> Run {
self.run_property = self.run_property.color(color);
self
}
pub fn highlight(mut self, color: &'a str) -> Run<'a> {
pub fn highlight(mut self, color: impl Into<String>) -> Run {
self.run_property = self.run_property.highlight(color);
self
}
pub fn bold(mut self) -> Run<'a> {
pub fn bold(mut self) -> Run {
self.run_property = self.run_property.bold();
self
}
pub fn italic(mut self) -> Run<'a> {
pub fn italic(mut self) -> Run {
self.run_property = self.run_property.italic();
self
}
pub fn underline(mut self, line_type: &'a str) -> Run<'a> {
pub fn underline(mut self, line_type: impl Into<String>) -> Run {
self.run_property = self.run_property.underline(line_type);
self
}
}
impl<'a> BuildXML for Run<'a> {
impl BuildXML for Run {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let mut b = b.open_run().add_child(&self.run_property);

View File

@ -26,12 +26,12 @@ impl RunProperty {
self
}
pub fn color(mut self, color: &str) -> RunProperty {
pub fn color(mut self, color: impl Into<String>) -> RunProperty {
self.color = Some(Color::new(color));
self
}
pub fn highlight(mut self, color: &str) -> RunProperty {
pub fn highlight(mut self, color: impl Into<String>) -> RunProperty {
self.highlight = Some(Highlight::new(color));
self
}
@ -48,7 +48,7 @@ impl RunProperty {
self
}
pub fn underline(mut self, line_type: &str) -> RunProperty {
pub fn underline(mut self, line_type: impl Into<String>) -> RunProperty {
self.underline = Some(Underline::new(line_type));
self
}

View File

@ -3,14 +3,14 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Table<'a> {
pub(crate) rows: Vec<TableRow<'a>>,
pub struct Table {
pub(crate) rows: Vec<TableRow>,
property: TableProperty,
grid: Vec<usize>,
}
impl<'a> Table<'a> {
pub fn new(rows: Vec<TableRow<'a>>) -> Table<'a> {
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
let grid = vec![];
Self {
@ -20,13 +20,13 @@ impl<'a> Table<'a> {
}
}
pub fn set_grid(mut self, grid: Vec<usize>) -> Table<'a> {
pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
self.grid = grid;
self
}
}
impl<'a> BuildXML for Table<'a> {
impl BuildXML for Table {
fn build(&self) -> Vec<u8> {
let grid = TableGrid::new(self.grid.clone());
let b = XMLBuilder::new()

View File

@ -4,40 +4,40 @@ use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableCell<'a> {
pub(crate) contents: Vec<TableCellContent<'a>>,
pub struct TableCell {
pub(crate) contents: Vec<TableCellContent>,
property: TableCellProperty,
}
#[derive(Debug, Clone)]
pub enum TableCellContent<'a> {
Paragraph(Paragraph<'a>),
pub enum TableCellContent {
Paragraph(Paragraph),
}
impl<'a> TableCell<'a> {
pub fn new() -> TableCell<'a> {
impl TableCell {
pub fn new() -> TableCell {
let property = TableCellProperty::new();
let contents = vec![];
Self { property, contents }
}
pub fn add_paragraph(mut self, p: Paragraph<'a>) -> TableCell<'a> {
pub fn add_paragraph(mut self, p: Paragraph) -> TableCell {
self.contents.push(TableCellContent::Paragraph(p));
self
}
pub fn vertical_merge(mut self, t: VMergeType) -> TableCell<'a> {
pub fn vertical_merge(mut self, t: VMergeType) -> TableCell {
self.property = self.property.vertical_merge(t);
self
}
pub fn grid_span(mut self, v: usize) -> TableCell<'a> {
pub fn grid_span(mut self, v: usize) -> TableCell {
self.property = self.property.grid_span(v);
self
}
}
impl<'a> BuildXML for TableCell<'a> {
impl BuildXML for TableCell {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let mut b = b.open_table_cell().add_child(&self.property);

View File

@ -3,19 +3,19 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRow<'a> {
pub(crate) cells: Vec<TableCell<'a>>,
pub struct TableRow {
pub(crate) cells: Vec<TableCell>,
property: TableRowProperty,
}
impl<'a> TableRow<'a> {
pub fn new(cells: Vec<TableCell<'a>>) -> TableRow<'a> {
impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow {
let property = TableRowProperty::new();
Self { property, cells }
}
}
impl<'a> BuildXML for TableRow<'a> {
impl BuildXML for TableRow {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new()
.open_table_row()

View File

@ -2,13 +2,13 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Text<'a> {
text: &'a str,
pub struct Text {
text: String,
preserve_space: bool,
}
impl<'a> Text<'a> {
pub fn new(text: &'a str) -> Text {
impl Text {
pub fn new(text: impl Into<String>) -> Text {
Text {
text: text.into(),
preserve_space: true,
@ -16,7 +16,7 @@ impl<'a> Text<'a> {
}
}
impl<'a> BuildXML for Text<'a> {
impl BuildXML for Text {
fn build(&self) -> Vec<u8> {
XMLBuilder::new().text(&self.text, true).build()
}

View File

@ -30,20 +30,20 @@ pub use styles::*;
pub use xml_docx::*;
#[derive(Debug)]
pub struct Docx<'a> {
pub struct Docx {
content_type: ContentTypes,
rels: Rels,
document_rels: DocumentRels,
doc_props: DocProps<'a>,
doc_props: DocProps,
styles: Styles,
document: Document<'a>,
comments: Comments<'a>,
numberings: Numberings<'a>,
document: Document,
comments: Comments,
numberings: Numberings,
settings: Settings,
font_table: FontTable,
}
impl<'a> Default for Docx<'a> {
impl Default for Docx {
fn default() -> Self {
let content_type = ContentTypes::new();
let rels = Rels::new();
@ -70,22 +70,22 @@ impl<'a> Default for Docx<'a> {
}
}
impl<'a> Docx<'a> {
pub fn new() -> Docx<'a> {
impl Docx {
pub fn new() -> Docx {
Default::default()
}
pub fn add_paragraph(mut self, p: Paragraph<'a>) -> Docx<'a> {
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
self.document = self.document.add_paragraph(p);
self
}
pub fn add_table(mut self, t: Table<'a>) -> Docx<'a> {
pub fn add_table(mut self, t: Table) -> Docx {
self.document = self.document.add_table(t);
self
}
pub fn add_numbering(mut self, num: Numbering<'a>) -> Docx<'a> {
pub fn add_numbering(mut self, num: Numbering) -> Docx {
self.numberings = self.numberings.add_numbering(num);
self
}
@ -108,7 +108,7 @@ impl<'a> Docx<'a> {
// Traverse and clone comments from document and add to comments node.
fn update_comments(&mut self) {
let mut comments: Vec<Comment<'a>> = vec![];
let mut comments: Vec<Comment> = vec![];
for child in &self.document.children {
match child {
DocumentChild::Paragraph(paragraph) => {

View File

@ -4,28 +4,28 @@ use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug)]
pub struct Numberings<'a> {
numberings: Vec<Numbering<'a>>,
pub struct Numberings {
numberings: Vec<Numbering>,
}
impl<'a> Numberings<'a> {
impl Numberings {
pub fn new() -> Self {
Default::default()
}
pub fn add_numbering(mut self, n: Numbering<'a>) -> Self {
pub fn add_numbering(mut self, n: Numbering) -> Self {
self.numberings.push(n);
self
}
}
impl<'a> Default for Numberings<'a> {
impl Default for Numberings {
fn default() -> Self {
Self { numberings: vec![] }
}
}
impl<'a> BuildXML for Numberings<'a> {
impl BuildXML for Numberings {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new().declaration(Some(true)).open_numbering();
b = b.add_child(&create_default_numbering());
@ -36,7 +36,7 @@ impl<'a> BuildXML for Numberings<'a> {
}
}
fn create_default_numbering() -> Numbering<'static> {
fn create_default_numbering() -> Numbering {
Numbering::new(0)
.add_level(
Level::new(

View File

@ -1 +1,22 @@
<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>
<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15">
<w:docDefaults>
<w:rPrDefault>
<w:rPr />
</w:rPrDefault>
</w:docDefaults>
<w:style w:type="paragraph" w:styleId="Normal">
<w:name w:val="Normal" />
<w:rPr />
<w:pPr>
<w:pStyle w:val="Normal" />
<w:rPr />
</w:pPr>
<w:basedOn w:val="Normal" />
<w:next w:val="Normal" />
<w:qFormat />
</w:style>
</w:styles>

View File

@ -6,7 +6,11 @@ const rust = import("./pkg");
rust
.then(m => {
let docx = m.createDocx().add_paragraph();
const p = m
.createParagraph()
.add_run(m.createRun().add_text("Hello World!!"));
let docx = m.createDocx().add_paragraph(p);
saveAs(new Blob([docx.build()]), "example.docx");
docx.free();
})
.catch(console.error);

View File

@ -3,20 +3,17 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug)]
pub struct Docx(docx_core::Docx<'static>);
pub struct Docx(docx_core::Docx);
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn createDocx() -> Docx {
#[wasm_bindgen(js_name = createDocx)]
pub fn create_docx() -> Docx {
Docx(docx_core::Docx::new())
}
#[wasm_bindgen]
impl Docx {
pub fn add_paragraph(mut self) -> Self {
self.0 = self.0.add_paragraph(
docx_core::Paragraph::new().add_run(docx_core::Run::new().add_text("Hello")),
);
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
self.0 = self.0.add_paragraph(p.0);
self
}
@ -29,8 +26,38 @@ impl Docx {
}
Ok(cur.into_inner())
}
}
pub fn test(&self, t: docx_core::StyleType) {
()
#[wasm_bindgen]
#[derive(Debug)]
pub struct Paragraph(docx_core::Paragraph);
#[wasm_bindgen(js_name = createParagraph)]
pub fn create_paragraph() -> Paragraph {
Paragraph(docx_core::Paragraph::new())
}
#[wasm_bindgen]
impl Paragraph {
pub fn add_run(mut self, run: Run) -> Self {
self.0 = self.0.add_run(run.0);
self
}
}
#[wasm_bindgen]
#[derive(Debug)]
pub struct Run(docx_core::Run);
#[wasm_bindgen(js_name = createRun)]
pub fn create_run() -> Run {
Run(docx_core::Run::new())
}
#[wasm_bindgen]
impl Run {
pub fn add_text(mut self, text: &str) -> Self {
self.0 = self.0.add_text(text);
self
}
}