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
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) {
// Checking if the line is the beginning of a table
if line == "Other withdrawals, debits and service charges" {
@ -40,22 +61,38 @@ impl Parser {
self.mode = ParserModes::Credit;
}
}
fn debit(&mut self, line: &str) {
// Should be pause?
if line.contains("continued") {
self.paused = true;
fn transaction(&mut self, t_type: ParserModes, line: &str) {
// Perform a Pre-Check
if !self.pre_check(line) {
return;
}
// Checking if it starts with a number
if !line.chars().next().map_or(false, |c| c.is_ascii_digit()) {
return;
// Splitting by Parts
let parts: Vec<&str> = line.split_whitespace().collect();
// 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
println!("Debit: {}", line);
// Creating a new Transaction
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) {
// Check to see if the line contains "continued"
@ -87,9 +124,14 @@ impl Parser {
// Switching based on Mode
match self.mode {
ParserModes::None => self.none(line),
ParserModes::Debit => self.debit(line),
ParserModes::Credit => self.credit(line),
}
ParserModes::Debit => self.transaction(ParserModes::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);
}
}
}