generated from OBJNULL/Dockerized-Rust
Compare commits
4 commits
a140eb84a8
...
6173c83da5
Author | SHA1 | Date | |
---|---|---|---|
6173c83da5 | |||
78744a104f | |||
1fdb88f3a8 | |||
bf7edc2f97 |
4 changed files with 81 additions and 5 deletions
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
pdf-extract = "0.9.0"
|
pdf-extract = "0.9.0"
|
||||||
clap = "4.5.36"
|
clap = "4.5.36"
|
||||||
|
csv = "1.3.1"
|
||||||
|
|
|
@ -3,10 +3,12 @@ mod action;
|
||||||
mod args;
|
mod args;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod reader;
|
mod reader;
|
||||||
|
mod writer;
|
||||||
|
|
||||||
use args::Arguments;
|
use args::Arguments;
|
||||||
use parser::Parser;
|
use parser::Parser;
|
||||||
use reader::Reader;
|
use reader::Reader;
|
||||||
|
use writer::Writer;
|
||||||
|
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
|
|
||||||
|
@ -15,14 +17,33 @@ fn main() -> Result<()> {
|
||||||
// Reading the Arguments
|
// Reading the Arguments
|
||||||
let args = Arguments::new();
|
let args = Arguments::new();
|
||||||
|
|
||||||
|
// Display Status
|
||||||
|
println!("Reading PDF and extracting Text Content...");
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
|
// Display Status
|
||||||
|
println!("Successfully extracted Text Content!");
|
||||||
|
println!("Parsing the PDF...");
|
||||||
|
|
||||||
// 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);
|
||||||
parser.start();
|
parser.start();
|
||||||
|
|
||||||
|
// Display Status
|
||||||
|
println!("Successfully parsed the PDF!");
|
||||||
|
println!("Saving to a CSV File...");
|
||||||
|
|
||||||
|
// Creating a Writer and saving the file
|
||||||
|
let writer = Writer::new(&parser.transactions, args.file_output.clone());
|
||||||
|
writer.save()?;
|
||||||
|
|
||||||
|
// Display Status
|
||||||
|
println!("Sucessfully Saved to {}", args.file_output);
|
||||||
|
println!("Thank you for using Statement Converter!");
|
||||||
|
|
||||||
// It's ok!
|
// It's ok!
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,10 +134,5 @@ impl Parser {
|
||||||
ParserModes::Credit => 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
59
project/src/writer.rs
Normal file
59
project/src/writer.rs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
// Libraries
|
||||||
|
use super::action::Transaction;
|
||||||
|
|
||||||
|
use std::io::Result;
|
||||||
|
|
||||||
|
use csv;
|
||||||
|
|
||||||
|
// Structures
|
||||||
|
pub struct Writer {
|
||||||
|
transactions: Vec<Transaction>,
|
||||||
|
path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementations
|
||||||
|
impl Writer {
|
||||||
|
// Constructors
|
||||||
|
pub fn new(transactions: &Vec<Transaction>, path: String) -> Self {
|
||||||
|
// Creating own list of transactions
|
||||||
|
let mut self_transactions: Vec<Transaction> = Vec::new();
|
||||||
|
for transaction in transactions {
|
||||||
|
self_transactions.push(Transaction {
|
||||||
|
description: transaction.description.clone(),
|
||||||
|
amount: transaction.amount.clone(),
|
||||||
|
date: transaction.date.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returning result
|
||||||
|
Self {
|
||||||
|
transactions: self_transactions,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
pub fn save(&self) -> Result<()> {
|
||||||
|
// Creating a file based on path
|
||||||
|
let mut writer = csv::Writer::from_path(&self.path)?;
|
||||||
|
|
||||||
|
// Creating the Header
|
||||||
|
writer.write_record(&["Date", "Description", "Amount"])?;
|
||||||
|
|
||||||
|
// Writing all transactions
|
||||||
|
for transaction in &self.transactions {
|
||||||
|
// Creating a record
|
||||||
|
writer.write_record(&[
|
||||||
|
transaction.date.clone(),
|
||||||
|
transaction.description.clone(),
|
||||||
|
transaction.amount.clone().to_string(),
|
||||||
|
])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush writer
|
||||||
|
writer.flush()?;
|
||||||
|
|
||||||
|
// Ok!!
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue