From 6afe10b7ab6bc3ab6e096cdd219ea078d4a02b44 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 4 May 2021 23:22:07 +0200 Subject: Add context to errors Remove `unwrap`s and add context to errors with 'anyhow'. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e20b14b..20315d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[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" @@ -245,6 +251,7 @@ dependencies = [ name = "pdf-form-replace-font" version = "0.0.1" dependencies = [ + "anyhow", "getopts", "lopdf", ] diff --git a/Cargo.toml b/Cargo.toml index 4f190e2..91ced45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.0.1" edition = "2018" [dependencies] +anyhow = "1.0.40" getopts = "0.2.21" lopdf = "0.26.0" diff --git a/src/main.rs b/src/main.rs index 7c1e077..492707f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,20 @@ +use anyhow::{self, Context}; use getopts::Options; use lopdf::{Document, Object}; use std::env; -fn main() -> Result<(), Box> { +fn main() { + match run() { + Ok(_) => (), + Err(e) => { + eprintln!("error: {}", e); + }, + }; +} + +fn run () -> Result<(), anyhow::Error> { let args: Vec = env::args().collect(); let mut opts = Options::new(); @@ -23,27 +33,34 @@ fn main() -> Result<(), Box> { &opt_matches.free[0] }; - let find = opt_matches.opt_str("find").unwrap(); - let replace = opt_matches.opt_str("replace").unwrap(); - let output_pdf = opt_matches.opt_str("output").unwrap_or("-".to_owned()); + let find = opt_matches.opt_str("find") + .ok_or(anyhow::anyhow!("no original font"))?; + let replace = opt_matches.opt_str("replace") + .ok_or(anyhow::anyhow!("no replacement font"))?; + let output_pdf = opt_matches.opt_str("output") + .unwrap_or("-".to_owned()); let mut doc = if input_pdf == "=" { - Document::load_from(&mut std::io::stdin()).unwrap() + Document::load_from(&mut std::io::stdin()) + .context("failed reading from stdin")? } else { - Document::load(input_pdf).unwrap() + Document::load(input_pdf) + .with_context(|| format!("failed to read PDF '{}'", input_pdf))? }; for (_, mut obj) in &mut doc.objects { match &mut obj { Object::Dictionary(ref mut d) => { for (k, v) in d.iter_mut() { - let key = std::str::from_utf8(k).unwrap(); + let key = std::str::from_utf8(k) + .context("unable to convert PDF object key to UTF-8")?; if key == "DA" { - let properties = v.as_str_mut().unwrap(); + let properties = v.as_str_mut() + .context("unable to get properties of form field")?; let new_properties = std::str::from_utf8(properties) - .unwrap() + .context("unable to convert form field properties to UTF-8")? .replace(&find, &replace); *properties = new_properties.into_bytes(); @@ -55,9 +72,11 @@ fn main() -> Result<(), Box> { } if output_pdf == "-" { - doc.save_to(&mut std::io::stdout()).unwrap(); + doc.save_to(&mut std::io::stdout()) + .context("failed writing to stdout")?; } else { - doc.save(output_pdf).unwrap(); + doc.save(&output_pdf) + .with_context(|| format!("failed to write PDF '{}'", output_pdf))?; } Ok(()) -- cgit v1.2.3