generated from OBJNULL/Dockerized-Rust
Compare commits
3 commits
92a666d9bc
...
8bfc7695a5
Author | SHA1 | Date | |
---|---|---|---|
8bfc7695a5 | |||
2150cb7f7a | |||
8c18164dbd |
3 changed files with 57 additions and 16 deletions
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
description: String,
|
pub description: String,
|
||||||
date: String,
|
pub date: String,
|
||||||
amount: f64,
|
pub amount: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementations
|
// Implementations
|
||||||
|
|
|
@ -18,7 +18,6 @@ fn main() -> Result<()> {
|
||||||
// Creating a File Reader & Reading
|
// Creating a File Reader & Reading
|
||||||
let reader = Reader::new(&args.file_input.clone())?;
|
let reader = Reader::new(&args.file_input.clone())?;
|
||||||
let text = reader.extract();
|
let text = reader.extract();
|
||||||
println!("Text Content: {}", text.clone());
|
|
||||||
|
|
||||||
// Creating a Parser to read the Text Content
|
// Creating a Parser to read the Text Content
|
||||||
let mut parser = Parser::new(text);
|
let mut parser = Parser::new(text);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue