aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-04-11 18:38:15 +0200
committerTeddy Wing2021-04-11 18:38:15 +0200
commitec3bec5c679a48cf28badcb515db2f346a88c6c4 (patch)
tree618a728f71b09482d26eee70d593c7a6e39a49db /src/main.rs
parent521b13211aac9e5f0c005bd0a1f804ec1e19b187 (diff)
downloadformurapid-ec3bec5c679a48cf28badcb515db2f346a88c6c4.tar.bz2
Make all file names dynamic based on input form path
Write files in the same directory as the input form. Files: * PDF form with IDs filled * TOML field file * PDF with fields filled from TOML
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index ff765e1..455c795 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,12 +37,20 @@ fn main() {
return;
}
- let form_path = &opt_matches.free[0];
+ let form_path = Path::new(&opt_matches.free[0]);
+ let form_file_prefix = form_path.file_stem().unwrap().to_str().unwrap();
+ let toml_file_name = format!("{}.toml", form_file_prefix);
+ let toml_path = form_path.with_file_name(toml_file_name);
+ let output_file_name = format!("{}-filled.pdf", form_file_prefix);
let mut form = Form::load(form_path).unwrap();
if opt_matches.opt_present("fill") {
- fill("./f1040.toml", &mut form);
+ fill(
+ toml_path,
+ form_path.with_file_name(output_file_name),
+ &mut form,
+ );
return;
}
@@ -78,14 +86,22 @@ fn main() {
let mut toml_file = OpenOptions::new()
.create_new(true)
.write(true)
- .open("f1040.toml")
+ .open(toml_path)
.unwrap();
toml_file.write_all(&toml::to_vec(&data).unwrap()).unwrap();
- form.save("./f1040-new.pdf").unwrap();
+ form.save(
+ form_path.with_file_name(
+ format!("{}-ids.pdf", form_file_prefix)
+ ),
+ ).unwrap();
}
-fn fill<P: AsRef<Path>>(data_path: P, form: &mut Form) {
+fn fill<P: AsRef<Path>>(
+ data_path: P,
+ output_path: P,
+ form: &mut Form,
+) {
let mut buf = Vec::new();
let mut file = File::open(data_path).unwrap();
file.read_to_end(&mut buf).unwrap();
@@ -100,5 +116,5 @@ fn fill<P: AsRef<Path>>(data_path: P, form: &mut Form) {
}
}
- form.save("./f1040-filled.pdf").unwrap();
+ form.save(output_path).unwrap();
}