diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2021-03-03 20:20:13 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2021-03-03 20:20:13 +0200 |
commit | fef39650efa65b4033b262e440d0f85999602412 (patch) | |
tree | ba3777a6bffd3df8c4d2c0be1f8b9e1bf27c3f2e /src/qif.rs | |
parent | e12dbad5e0be71e579692ab18a7ffacac153fcb4 (diff) |
Update to modern Rust
Diffstat (limited to 'src/qif.rs')
-rw-r--r-- | src/qif.rs | 55 |
1 files changed, 36 insertions, 19 deletions
@@ -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<QifEntry> + entries: Vec<QifEntry>, } 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 { |