docx-rs/docx-wasm/src/lib.rs

64 lines
1.3 KiB
Rust
Raw Normal View History

2019-11-07 11:45:03 +02:00
use docx_core;
2019-09-11 21:28:14 +03:00
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug)]
pub struct Docx(docx_core::Docx);
2019-09-11 21:28:14 +03:00
#[wasm_bindgen(js_name = createDocx)]
pub fn create_docx() -> Docx {
2019-11-07 11:45:03 +02:00
Docx(docx_core::Docx::new())
2019-09-11 21:28:14 +03:00
}
#[wasm_bindgen]
2019-11-07 11:45:03 +02:00
impl Docx {
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
self.0 = self.0.add_paragraph(p.0);
2019-11-07 11:45:03 +02:00
self
}
2019-12-05 08:44:18 +02:00
pub fn build(&mut self) -> Result<Vec<u8>, JsValue> {
2019-11-07 11:45:03 +02:00
let buf = Vec::new();
let mut cur = std::io::Cursor::new(buf);
2019-11-11 11:34:55 +02:00
let res = self.0.build().pack(&mut cur);
if res.is_err() {
return Err(format!("{:?}", res).into());
}
Ok(cur.into_inner())
2019-09-11 21:28:14 +03:00
}
}
#[wasm_bindgen]
#[derive(Debug)]
pub struct Paragraph(docx_core::Paragraph);
2019-09-11 21:28:14 +03:00
#[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
2019-09-11 21:28:14 +03:00
}
}