From 1b98d28058f51833aa9be06423b32c9d5f0ab49c Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 1 Jan 2018 14:14:19 +0200 Subject: Change to handle files internally instead of working with stdin --- src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 291c548..66c3caa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,57 @@ +extern crate structopt; +#[macro_use] +extern crate structopt_derive; +use structopt::StructOpt; + extern crate qif_parser; use qif_parser::qif::*; +use std::io::prelude::*; use std::io::*; +use std::path::PathBuf; -fn main() { - let stdin = stdin(); - let stdin_lock = stdin.lock(); - let mut lines = stdin_lock.lines(); - - let mut file = QifFile::new(lines.next().unwrap().unwrap()); - let mut next_entry = Vec::new(); +use std::fs::File; +use std::process; + +#[derive(StructOpt, Debug)] +#[structopt(name = "Qif Parser", about = "Qif file preprocessor to decrease duplication when importing to gnucash")] +struct CliArgs { + #[structopt(help = "Files to preprocess", required, parse(from_os_str))] + files: Vec +} - for line_result in lines { - let line = line_result.unwrap(); - if line == String::from("^") { - file.append_non_empty(QifEntry::new(&next_entry)); - next_entry.clear(); - } else { - next_entry.push(line); +fn main() { + let args = CliArgs::from_args(); + for filepath in &args.files { + if !filepath.is_file() { + eprintln!("{} is not a file", filepath.display()); + process::exit(1); } } + + for filepath in &args.files { + let qif_result = { + let file = File::open(filepath).unwrap(); + let file_reader = BufReader::new(file); + let mut lines = file_reader.lines(); + + let mut qif_file = QifFile::new(lines.next().unwrap().unwrap()); + let mut next_entry = Vec::new(); - println!("{}", file); + for line_result in lines { + let line = line_result.unwrap(); + if line == String::from("^") { + qif_file.append_non_empty(QifEntry::new(&next_entry)); + next_entry.clear(); + } else { + next_entry.push(line); + } + } + qif_file + }; + + let mut file = File::create(filepath).unwrap(); + writeln!(file, "{}", qif_result).unwrap(); + } + println!("Processed {} files", args.files.len()); } -- cgit v1.2.3