aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-04-11 20:10:39 +0200
committerTeddy Wing2021-04-11 20:10:39 +0200
commit01c82770392b23176b4693d4456109f41a50fe45 (patch)
treeb43b4e6513855d5dcc47d09782fe422f2eaeb655
parent0acbf02b7e380ac352f3e090023d9f9b28fbdb30 (diff)
downloadformurapid-01c82770392b23176b4693d4456109f41a50fe45.tar.bz2
fill(): Remove `unwrap`s
Replace these with 'anyhow' `Result`s.
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs19
3 files changed, 19 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index ec576e4..4f228f3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,6 +13,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]]
+name = "anyhow"
+version = "1.0.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
+
+[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -337,6 +343,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
name = "formurapid"
version = "0.0.1"
dependencies = [
+ "anyhow",
"derive_builder",
"exitcode",
"getopts",
diff --git a/Cargo.toml b/Cargo.toml
index d3479a4..916b6e3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.0.1"
edition = "2018"
[dependencies]
+anyhow = "1.0.40"
derive_builder = "0.10.0"
exitcode = "1.1.2"
getopts = "0.2.21"
diff --git a/src/main.rs b/src/main.rs
index 8961809..e064bfc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+use anyhow;
use derive_builder::Builder;
use getopts::Options;
use pdf_forms::{Form, FieldType};
@@ -61,7 +62,7 @@ fn main() {
toml_path,
form_path.with_file_name(output_file_name),
&mut form,
- );
+ ).unwrap();
} else if opt_matches.opt_present("generate") {
let ids_path = form_path.with_file_name(
format!("{}-ids.pdf", form_file_prefix)
@@ -126,20 +127,22 @@ fn fill<P: AsRef<Path>>(
data_path: P,
output_path: P,
form: &mut Form,
-) {
+) -> anyhow::Result<()> {
let mut buf = Vec::new();
- let mut file = File::open(data_path).unwrap();
- file.read_to_end(&mut buf).unwrap();
+ let mut file = File::open(data_path)?;
+ file.read_to_end(&mut buf)?;
- let data: TextForm = toml::from_slice(&buf).unwrap();
+ let data: TextForm = toml::from_slice(&buf)?;
for field in data.fields {
if let Some(value) = field.value {
- form.set_text(field.id, value.to_owned()).unwrap();
+ form.set_text(field.id, value.to_owned())?;
} else if let Some(state) = field.state {
- form.set_check_box(field.id, state).unwrap();
+ form.set_check_box(field.id, state)?;
}
}
- form.save(output_path).unwrap();
+ form.save(output_path)?;
+
+ Ok(())
}