Parser now has the ability to parse lines

This commit is contained in:
Maddox Werts 2025-04-18 15:55:21 -04:00
parent 2150cb7f7a
commit 8bfc7695a5

View file

@ -32,6 +32,27 @@ impl Parser {
} }
// Functions // Functions
fn pre_check(&mut self, line: &str) -> bool {
// Should be pause?
if line.contains("continued") {
self.paused = true;
return false;
}
// End of Section?
if line.contains("Total") {
self.mode = ParserModes::None;
return false;
}
// Checking if it starts with a number
if !line.chars().next().map_or(false, |c| c.is_ascii_digit()) {
return false;
}
true
}
fn none(&mut self, line: &str) { fn none(&mut self, line: &str) {
// Checking if the line is the beginning of a table // Checking if the line is the beginning of a table
if line == "Other withdrawals, debits and service charges" { if line == "Other withdrawals, debits and service charges" {
@ -40,22 +61,38 @@ impl Parser {
self.mode = ParserModes::Credit; self.mode = ParserModes::Credit;
} }
} }
fn debit(&mut self, line: &str) { fn transaction(&mut self, t_type: ParserModes, line: &str) {
// Should be pause? // Perform a Pre-Check
if line.contains("continued") { if !self.pre_check(line) {
self.paused = true;
return; return;
} }
// Checking if it starts with a number // Splitting by Parts
if !line.chars().next().map_or(false, |c| c.is_ascii_digit()) { let parts: Vec<&str> = line.split_whitespace().collect();
return;
// Collecting based on Parts
let description = parts[2..parts.len() - 1].join(" ");
let mut amount: f64 = parts.last().unwrap().parse().unwrap_or(0.0);
let date = parts[0].to_owned();
// Is it a debit?
match t_type {
ParserModes::Debit => {
amount = -amount;
}
_ => {}
} }
// Print the line // Creating a new Transaction
println!("Debit: {}", line); let new_transaction = Transaction {
description,
amount,
date,
};
// Adding the transaction to our list
self.transactions.push(new_transaction);
} }
fn credit(&mut self, line: &str) {}
fn pause_check(&mut self, line: &str) { fn pause_check(&mut self, line: &str) {
// Check to see if the line contains "continued" // Check to see if the line contains "continued"
@ -87,9 +124,14 @@ impl Parser {
// Switching based on Mode // Switching based on Mode
match self.mode { match self.mode {
ParserModes::None => self.none(line), ParserModes::None => self.none(line),
ParserModes::Debit => self.debit(line), ParserModes::Debit => self.transaction(ParserModes::Credit, line),
ParserModes::Credit => self.credit(line), ParserModes::Credit => self.transaction(ParserModes::Credit, line),
} }
}
// DEBUG: Print all transactions
for t in &self.transactions {
println!("Transaction {}: {} on {}", t.description, t.amount, t.date);
} }
} }
} }