feat: Support errors
parent
b28c482c75
commit
1533de70e2
|
@ -45,6 +45,7 @@ name = "docx-core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"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]]
|
[[package]]
|
||||||
name = "unicode-xid"
|
name = "unicode-xid"
|
||||||
version = "0.2.0"
|
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 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 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 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 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 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"
|
"checksum wasm-bindgen-backend 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "aa2868fa93e5bf36a9364d1277a0f97392748a8217d9aa0ec3f1cdbdf7ad1a60"
|
||||||
|
|
|
@ -9,6 +9,7 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
xml-rs = "0.8.0"
|
xml-rs = "0.8.0"
|
||||||
wasm-bindgen = "0.2.50"
|
wasm-bindgen = "0.2.50"
|
||||||
|
thiserror = "1.0"
|
||||||
zip = { version = "0.5", default-features = false }
|
zip = { version = "0.5", default-features = false }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use docx_core::*;
|
use docx_core::*;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() -> Result<(), DocxError> {
|
||||||
let path = std::path::Path::new("./output/alignment.docx");
|
let path = std::path::Path::new("./output/alignment.docx");
|
||||||
let file = std::fs::File::create(&path).unwrap();
|
let file = std::fs::File::create(&path).unwrap();
|
||||||
Docx::new()
|
Docx::new()
|
||||||
|
@ -11,5 +11,6 @@ pub fn main() {
|
||||||
.align(AlignmentType::Right),
|
.align(AlignmentType::Right),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
.pack(file);
|
.pack(file)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
extern crate docx_core;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
docx_core::simple();
|
|
||||||
}
|
|
|
@ -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 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 path = std::path::Path::new("./output/indent.docx");
|
||||||
let file = std::fs::File::create(&path).unwrap();
|
let file = std::fs::File::create(&path).unwrap();
|
||||||
Docx::new()
|
Docx::new()
|
||||||
|
@ -20,5 +20,6 @@ pub fn main() {
|
||||||
.indent(1560, Some(SpecialIndentType::Hanging(720))),
|
.indent(1560, Some(SpecialIndentType::Hanging(720))),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
.pack(file);
|
.pack(file)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use docx_core::*;
|
use docx_core::*;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() -> Result<(), DocxError> {
|
||||||
let path = std::path::Path::new("./output/size.docx");
|
let path = std::path::Path::new("./output/size.docx");
|
||||||
let file = std::fs::File::create(&path).unwrap();
|
let file = std::fs::File::create(&path).unwrap();
|
||||||
Docx::new()
|
Docx::new()
|
||||||
|
@ -11,5 +11,6 @@ pub fn main() {
|
||||||
.add_run(Run::new("ld")),
|
.add_run(Run::new("ld")),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
.pack(file);
|
.pack(file)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
|
@ -1,11 +1,12 @@
|
||||||
mod documents;
|
mod documents;
|
||||||
|
mod errors;
|
||||||
mod types;
|
mod types;
|
||||||
mod xml_builder;
|
mod xml_builder;
|
||||||
mod zipper;
|
mod zipper;
|
||||||
|
|
||||||
pub use documents::*;
|
pub use documents::*;
|
||||||
|
pub use errors::*;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
pub use zipper::*;
|
|
||||||
|
|
||||||
pub fn simple() {
|
pub fn simple() {
|
||||||
let path = std::path::Path::new("./test.docx");
|
let path = std::path::Path::new("./test.docx");
|
||||||
|
|
|
@ -93,6 +93,7 @@ impl XMLBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write plain text
|
// Write plain text
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn plain_text(mut self, t: &str) -> Self {
|
pub(crate) fn plain_text(mut self, t: &str) -> Self {
|
||||||
self.writer.write(t).unwrap();
|
self.writer.write(t).unwrap();
|
||||||
self
|
self
|
||||||
|
|
|
@ -20,12 +20,14 @@ impl Docx {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(&self) -> Vec<u8> {
|
pub fn build(&self) -> Result<Vec<u8>, JsValue> {
|
||||||
let buf = Vec::new();
|
let buf = Vec::new();
|
||||||
let mut cur = std::io::Cursor::new(buf);
|
let mut cur = std::io::Cursor::new(buf);
|
||||||
let b = self.0.build();
|
let res = self.0.build().pack(&mut cur);
|
||||||
docx_core::zip(&mut cur, b).unwrap();
|
if res.is_err() {
|
||||||
cur.into_inner()
|
return Err(format!("{:?}", res).into());
|
||||||
|
}
|
||||||
|
Ok(cur.into_inner())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test(&self, t: docx_core::StyleType) {
|
pub fn test(&self, t: docx_core::StyleType) {
|
||||||
|
|
Loading…
Reference in New Issue