diff options
author | Teddy Wing | 2021-04-11 06:01:06 +0200 |
---|---|---|
committer | Teddy Wing | 2021-04-11 06:01:06 +0200 |
commit | 848b0cf0f677e9ecd7d3952c6e5afee8341ca246 (patch) | |
tree | 3941510f7fd758a4ed407d4caec06141218abb6b /src/main.rs | |
parent | 720b6e76858ab73028c1e9b718b25f7d74127ded (diff) | |
download | formurapid-848b0cf0f677e9ecd7d3952c6e5afee8341ca246.tar.bz2 |
Write a base TOML file including the fields
Write a file that can be modified by a user to fill in the form fields.
A separate procedure will take the TOML file and fill in the appropriate
fields.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 2713df4..a943a9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,63 @@ +use derive_builder::Builder; use pdf_forms::{Form, FieldType}; +use serde::Serialize; + +use std::fs::OpenOptions; +use std::io::Write; + +#[derive(Debug, Default, Serialize)] +struct TextForm<'a> { + fields: Vec<Field<'a>>, +} + +#[derive(Debug, Builder, Serialize)] +struct Field<'a> { + id: usize, + + #[builder(default)] + value: Option<&'a str>, + + #[builder(default)] + state: Option<bool>, +} fn main() { let mut form = Form::load("./f1040.pdf").unwrap(); + let mut data = TextForm::default(); + for i in 0..form.len() { let field_type = form.get_type(i); - if let FieldType::Text = field_type { - form.set_text(i, format!("{}", i)).unwrap(); + match field_type { + FieldType::Text => { + form.set_text(i, format!("{}", i)).unwrap(); + + data.fields.push(FieldBuilder::default() + .id(i) + .value(Some("")) + .build() + .unwrap() + ); + }, + FieldType::CheckBox => { + data.fields.push(FieldBuilder::default() + .id(i) + .state(Some(false)) + .build() + .unwrap() + ); + }, + _ => (), } } + let mut toml_file = OpenOptions::new() + .create_new(true) + .write(true) + .open("f1040.toml") + .unwrap(); + toml_file.write_all(&toml::to_vec(&data).unwrap()).unwrap(); + form.save("./f1040-new.pdf").unwrap(); } |