generated from OBJNULL/Dockerized-Rust
Compare commits
No commits in common. "8bfc7695a5ccf1b30be8fc1a3a3d9aa116cd91b0" and "92a666d9bcb62917983b763a265f912d40a89220" have entirely different histories.
8bfc7695a5
...
92a666d9bc
3 changed files with 16 additions and 57 deletions
|
@ -2,9 +2,9 @@
|
|||
|
||||
// Structures
|
||||
pub struct Transaction {
|
||||
pub description: String,
|
||||
pub date: String,
|
||||
pub amount: f64,
|
||||
description: String,
|
||||
date: String,
|
||||
amount: f64,
|
||||
}
|
||||
|
||||
// Implementations
|
||||
|
|
|
@ -18,6 +18,7 @@ fn main() -> Result<()> {
|
|||
// Creating a File Reader & Reading
|
||||
let reader = Reader::new(&args.file_input.clone())?;
|
||||
let text = reader.extract();
|
||||
println!("Text Content: {}", text.clone());
|
||||
|
||||
// Creating a Parser to read the Text Content
|
||||
let mut parser = Parser::new(text);
|
||||
|
|
|
@ -32,27 +32,6 @@ 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" {
|
||||
|
@ -61,38 +40,22 @@ impl Parser {
|
|||
self.mode = ParserModes::Credit;
|
||||
}
|
||||
}
|
||||
fn transaction(&mut self, t_type: ParserModes, line: &str) {
|
||||
// Perform a Pre-Check
|
||||
if !self.pre_check(line) {
|
||||
fn debit(&mut self, line: &str) {
|
||||
// Should be pause?
|
||||
if line.contains("continued") {
|
||||
self.paused = true;
|
||||
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;
|
||||
}
|
||||
_ => {}
|
||||
// Checking if it starts with a number
|
||||
if !line.chars().next().map_or(false, |c| c.is_ascii_digit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Creating a new Transaction
|
||||
let new_transaction = Transaction {
|
||||
description,
|
||||
amount,
|
||||
date,
|
||||
};
|
||||
|
||||
// Adding the transaction to our list
|
||||
self.transactions.push(new_transaction);
|
||||
// Print the line
|
||||
println!("Debit: {}", line);
|
||||
}
|
||||
fn credit(&mut self, line: &str) {}
|
||||
|
||||
fn pause_check(&mut self, line: &str) {
|
||||
// Check to see if the line contains "continued"
|
||||
|
@ -124,14 +87,9 @@ impl Parser {
|
|||
// Switching based on Mode
|
||||
match self.mode {
|
||||
ParserModes::None => self.none(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);
|
||||
ParserModes::Debit => self.debit(line),
|
||||
ParserModes::Credit => self.credit(line),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue