From fef39650efa65b4033b262e440d0f85999602412 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Wed, 3 Mar 2021 20:20:13 +0200 Subject: Update to modern Rust --- src/main.rs | 39 ++++++++++++++++++--------------------- src/qif.rs | 55 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 8874ea9..03bcff3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,15 @@ -extern crate regex; -#[macro_use] extern crate lazy_static; -extern crate structopt; -#[macro_use] extern crate structopt_derive; - pub mod qif; -use structopt::StructOpt; - use qif::*; -use std::io::prelude::*; -use std::io::*; - -use std::ffi::{OsString, OsStr}; -use std::path::PathBuf; -use std::result::Result; - -use std::fs::File; -use std::error::Error; +use std::{ + error::Error, + ffi::{OsStr, OsString}, + fs::File, + io::{prelude::*, *}, + path::PathBuf, + result::Result, +}; +use structopt::StructOpt; fn parse_filepath(str: &OsStr) -> Result { let path: PathBuf = ::std::convert::From::from(str); @@ -28,15 +21,19 @@ fn parse_filepath(str: &OsStr) -> Result { } #[derive(StructOpt, Debug)] -#[structopt(name = "Qif Parser", about = "Qif file preprocessor to decrease duplication when importing to gnucash")] +#[structopt( + name = "Qif Parser", + about = "Qif file preprocessor to decrease duplication when importing to gnucash" +)] struct CliArgs { - #[structopt(help = "Files to preprocess", required, parse(try_from_os_str="parse_filepath"))] - files: Vec + /// Files to preprocess + #[structopt(parse(try_from_os_str = parse_filepath))] + files: Vec, } -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let args = CliArgs::from_args(); - + for filepath in &args.files { let file = File::open(filepath)?; let file_reader = BufReader::new(file); diff --git a/src/qif.rs b/src/qif.rs index 5249677..9b46af0 100644 --- a/src/qif.rs +++ b/src/qif.rs @@ -1,18 +1,19 @@ -use std::fmt; +use lazy_static::lazy_static; use regex::Regex; -use std::result::Result; use std::error::Error; +use std::fmt; +use std::result::Result; pub struct QifFile { header: String, - entries: Vec + entries: Vec, } impl QifFile { pub fn new(header: String) -> QifFile { QifFile { - header: header, - entries: Vec::new() + header, + entries: Vec::new(), } } @@ -40,7 +41,7 @@ impl fmt::Display for QifFile { pub struct QifEntry { date: String, amount: String, - description: String + description: String, } const DATE_PREFIX: &str = "D"; @@ -56,7 +57,7 @@ impl QifEntry { (Some(date), Some(amount), Some(description)) => Ok(QifEntry { date: date.chars().skip(1).collect(), amount: amount.chars().skip(1).collect(), - description: description.chars().skip(1).collect() + description: description.chars().skip(1).collect(), }), (None, _, _) => Err(QifParsingError::MissingDate), (_, None, _) => Err(QifParsingError::MissingAmount), @@ -76,17 +77,24 @@ impl QifEntry { impl fmt::Display for QifEntry { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}{}\n{}{}\n{}{}", - DATE_PREFIX, self.date, - AMOUNT_PREFIX, self.amount, - DESCRIPTION_PREFIX, self.clean_description() + write!( + f, + "{}{}\n{}{}\n{}{}", + DATE_PREFIX, + self.date, + AMOUNT_PREFIX, + self.amount, + DESCRIPTION_PREFIX, + self.clean_description() ) } } fn replace_date(text: &str) -> String { lazy_static! { - static ref DATE_REGEX: Regex = Regex::new(r"(?i)(\d{2} )?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)( \d{4})?").unwrap(); + static ref DATE_REGEX: Regex = + Regex::new(r"(?i)(\d{2} )?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)( \d{4})?") + .unwrap(); } DATE_REGEX.replace_all(text, "").trim().to_string() } @@ -103,27 +111,36 @@ fn replace_common(text: &str) -> String { (Regex::new(r"^PNA").unwrap(), "PNA"), (Regex::new(r"(?i)sahl").unwrap(), "SA Home Loans"), (Regex::new(r"(?i)gautrain").unwrap(), "Gautrain"), - (Regex::new(r"(?i)BANK YOUR CHANGE DEBI").unwrap(), "TO SAVINGS POCKET"), + ( + Regex::new(r"(?i)BANK YOUR CHANGE DEBI").unwrap(), + "TO SAVINGS POCKET" + ), (Regex::new(r"(?i)AFRIHOST").unwrap(), "Afrihost"), (Regex::new(r"(?i)DIALDIRECT").unwrap(), "Dialdirect"), (Regex::new(r"(?i)STEERS").unwrap(), "Steers"), (Regex::new(r"(?i)CELL C").unwrap(), "Cell C"), (Regex::new(r"(?i)ELECTRICITY").unwrap(), "Electricity"), - (Regex::new(r"(?i)(COUNTRY VIEW|STAR STOP|Shell|Sasol)").unwrap(), "Petrol"), + ( + Regex::new(r"(?i)(COUNTRY VIEW|STAR STOP|Shell|Sasol)").unwrap(), + "Petrol" + ), (Regex::new(r"(?i)kung ?-?fu").unwrap(), "Kungfu Kitchen"), ); } - COMMON_NAMES.iter().fold( - text, |acc, next| if next.0.is_match(acc) {next.1} else {acc} - ).to_string() + COMMON_NAMES + .iter() + .fold( + text, + |acc, next| if next.0.is_match(acc) { next.1 } else { acc }, + ) + .to_string() } - #[derive(Debug)] pub enum QifParsingError { MissingDate, MissingAmount, - MissingDescription + MissingDescription, } impl fmt::Display for QifParsingError { -- cgit v1.2.3