summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-11-29 22:12:42 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-11-29 22:12:42 +0200
commit56b731a651e96d1caa7d246b1ae8cd555b23536a (patch)
tree3cd14a0174bfee765bca337b1b5851eaa641b319 /src
parent817d06fc2ef4f8e258d906fad96c26f72b32c702 (diff)
Added cleaning up of funny descriptions
Mostly branches and dates messing with import matching
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs4
-rw-r--r--src/qif.rs51
2 files changed, 49 insertions, 6 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 51368c5..7cb52d0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1,5 @@
+extern crate regex;
+
+#[macro_use] extern crate lazy_static;
+
pub mod qif;
diff --git a/src/qif.rs b/src/qif.rs
index 46b2bc3..41f8e6f 100644
--- a/src/qif.rs
+++ b/src/qif.rs
@@ -1,4 +1,5 @@
use std::fmt;
+use regex::Regex;
pub struct QifFile {
header: String,
@@ -54,23 +55,61 @@ impl QifEntry {
let amount = lines.iter().find(|l| l.starts_with(AMOUNT_PREFIX)).expect("No amount");
let description = lines.iter().find(|l| l.starts_with(DESCRIPTION_PREFIX)).expect("No description");
QifEntry {
- date: date.clone(),
- amount: amount.clone(),
- description: description.clone()
+ date: date.chars().skip(1).collect(),
+ amount: amount.chars().skip(1).collect(),
+ description: description.chars().skip(1).collect()
}
}
pub fn is_empty(&self) -> bool {
- self.amount == String::from("T0")
+ self.amount == String::from("0")
}
pub fn clean_description(&self) -> String {
- self.description.clone()
+ let replaced_date = replace_date(&self.description);
+ replace_common(&replaced_date)
}
}
impl fmt::Display for QifEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}\n{}\n{}", self.date, self.amount, 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();
+ }
+ DATE_REGEX.replace_all(text, "").trim().to_string()
+}
+
+fn replace_common(text: &str) -> String {
+ lazy_static! {
+ static ref COMMON_NAMES: Vec<(Regex, &'static str)> = vec!(
+ (Regex::new(r"(?i)(pick n pay|pnp)").unwrap(), "Pick n Pay"),
+ (Regex::new(r"(?i)checkers").unwrap(), "Checkers"),
+ (Regex::new(r"(?i)WOOLWORTHS").unwrap(), "Woolworths"),
+ (Regex::new(r"(?i)spar").unwrap(), "Spar"),
+ (Regex::new(r"(?i)Crazy store").unwrap(), "Crazy Store"),
+ (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)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"),
+ );
+ }
+ COMMON_NAMES.iter().fold(
+ text, |acc, next| if next.0.is_match(acc) {next.1} else {acc}
+ ).to_string()
+}
+