diff --git a/Cargo.lock b/Cargo.lock index 2685b6e..bb05bc1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,6 +45,7 @@ name = "docx-core" version = "0.1.0" dependencies = [ "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "thiserror 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "wasm-bindgen 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)", "xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "zip 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -121,6 +122,24 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thiserror" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thiserror-impl 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-xid" version = "0.2.0" @@ -223,6 +242,8 @@ dependencies = [ "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum syn 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0e7bedb3320d0f3035594b0b723c8a28d7d336a3eda3881db79e61d676fb644c" +"checksum thiserror 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f9fb62ff737e573b1e677459bea6fd023cd5d6e868c3242d3cdf3ef2f0554824" +"checksum thiserror-impl 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "24069c0ba08aab54289d6a25f5036d94afc61e1538bbc42ae5501df141c9027d" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum wasm-bindgen 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "4c29d57d5c3b3bc53bbe35c5a4f4a0df994d870b7d3cb0ad1c2065e21822ae41" "checksum wasm-bindgen-backend 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "aa2868fa93e5bf36a9364d1277a0f97392748a8217d9aa0ec3f1cdbdf7ad1a60" diff --git a/docx-core/Cargo.toml b/docx-core/Cargo.toml index bc797c3..7a003fa 100644 --- a/docx-core/Cargo.toml +++ b/docx-core/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] xml-rs = "0.8.0" wasm-bindgen = "0.2.50" +thiserror = "1.0" zip = { version = "0.5", default-features = false } [dev-dependencies] diff --git a/docx-core/examples/alignment.rs b/docx-core/examples/alignment.rs index ade39aa..be34a2e 100644 --- a/docx-core/examples/alignment.rs +++ b/docx-core/examples/alignment.rs @@ -1,6 +1,6 @@ use docx_core::*; -pub fn main() { +pub fn main() -> Result<(), DocxError> { let path = std::path::Path::new("./output/alignment.docx"); let file = std::fs::File::create(&path).unwrap(); Docx::new() @@ -11,5 +11,6 @@ pub fn main() { .align(AlignmentType::Right), ) .build() - .pack(file); + .pack(file)?; + Ok(()) } diff --git a/docx-core/examples/example.rs b/docx-core/examples/example.rs deleted file mode 100644 index 9a850eb..0000000 --- a/docx-core/examples/example.rs +++ /dev/null @@ -1,5 +0,0 @@ -extern crate docx_core; - -fn main() { - docx_core::simple(); -} \ No newline at end of file diff --git a/docx-core/examples/indent.rs b/docx-core/examples/indent.rs index e0e4aa0..1cfa8e6 100644 --- a/docx-core/examples/indent.rs +++ b/docx-core/examples/indent.rs @@ -2,7 +2,7 @@ use docx_core::*; pub const DUMMY: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; -pub fn main() { +pub fn main() -> Result<(), DocxError> { let path = std::path::Path::new("./output/indent.docx"); let file = std::fs::File::create(&path).unwrap(); Docx::new() @@ -20,5 +20,6 @@ pub fn main() { .indent(1560, Some(SpecialIndentType::Hanging(720))), ) .build() - .pack(file); + .pack(file)?; + Ok(()) } diff --git a/docx-core/examples/size.rs b/docx-core/examples/size.rs index 6a36a0b..40f39f6 100644 --- a/docx-core/examples/size.rs +++ b/docx-core/examples/size.rs @@ -1,6 +1,6 @@ use docx_core::*; -pub fn main() { +pub fn main() -> Result<(), DocxError> { let path = std::path::Path::new("./output/size.docx"); let file = std::fs::File::create(&path).unwrap(); Docx::new() @@ -11,5 +11,6 @@ pub fn main() { .add_run(Run::new("ld")), ) .build() - .pack(file); + .pack(file)?; + Ok(()) } diff --git a/docx-core/src/errors/mod.rs b/docx-core/src/errors/mod.rs new file mode 100644 index 0000000..9c0e5ea --- /dev/null +++ b/docx-core/src/errors/mod.rs @@ -0,0 +1,11 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum DocxError { + #[error("Failed to write XML to buffer.")] + EmitterError(#[from] xml::writer::Error), + #[error("Failed to zip XML documents.")] + ZipError(#[from] zip::result::ZipError), + #[error("Unknown error")] + Unknown, +} diff --git a/docx-core/src/lib.rs b/docx-core/src/lib.rs index bf5883d..9bdd3fe 100644 --- a/docx-core/src/lib.rs +++ b/docx-core/src/lib.rs @@ -1,11 +1,12 @@ mod documents; +mod errors; mod types; mod xml_builder; mod zipper; pub use documents::*; +pub use errors::*; pub use types::*; -pub use zipper::*; pub fn simple() { let path = std::path::Path::new("./test.docx"); diff --git a/docx-core/src/xml_builder/mod.rs b/docx-core/src/xml_builder/mod.rs index 5119a13..32d74d9 100644 --- a/docx-core/src/xml_builder/mod.rs +++ b/docx-core/src/xml_builder/mod.rs @@ -93,6 +93,7 @@ impl XMLBuilder { } // Write plain text + #[allow(dead_code)] pub(crate) fn plain_text(mut self, t: &str) -> Self { self.writer.write(t).unwrap(); self diff --git a/docx-wasm/src/lib.rs b/docx-wasm/src/lib.rs index 1bbd9a6..e4aa435 100644 --- a/docx-wasm/src/lib.rs +++ b/docx-wasm/src/lib.rs @@ -20,12 +20,14 @@ impl Docx { self } - pub fn build(&self) -> Vec { + pub fn build(&self) -> Result, JsValue> { let buf = Vec::new(); let mut cur = std::io::Cursor::new(buf); - let b = self.0.build(); - docx_core::zip(&mut cur, b).unwrap(); - cur.into_inner() + let res = self.0.build().pack(&mut cur); + if res.is_err() { + return Err(format!("{:?}", res).into()); + } + Ok(cur.into_inner()) } pub fn test(&self, t: docx_core::StyleType) {