generated from OBJNULL/Dockerized-Rust
Compare commits
4 commits
d293452929
...
8483733678
Author | SHA1 | Date | |
---|---|---|---|
8483733678 | |||
c2f6dd47b4 | |||
820b024d10 | |||
bfa80e061c |
4 changed files with 81 additions and 3 deletions
|
@ -5,6 +5,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
pdf-extract = "0.9.0"
|
pdf-extract = "0.9.0"
|
||||||
|
walkdir = "2.5.0"
|
||||||
colored = "3.0.0"
|
colored = "3.0.0"
|
||||||
clap = "4.5.36"
|
clap = "4.5.36"
|
||||||
csv = "1.3.1"
|
csv = "1.3.1"
|
||||||
|
|
|
@ -60,6 +60,13 @@ impl Arguments {
|
||||||
.help("The path where your CSV files are")
|
.help("The path where your CSV files are")
|
||||||
.action(ArgAction::Set)
|
.action(ArgAction::Set)
|
||||||
.required(true),
|
.required(true),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("file_out")
|
||||||
|
.id("file_out")
|
||||||
|
.help("The path where the master CSV is saved")
|
||||||
|
.action(ArgAction::Set)
|
||||||
|
.required(true),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
@ -93,8 +100,11 @@ impl Arguments {
|
||||||
.get_one::<String>("directory")
|
.get_one::<String>("directory")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
file_output = package_matches
|
||||||
|
.get_one::<String>("file_out")
|
||||||
|
.unwrap()
|
||||||
|
.to_owned();
|
||||||
file_input = String::new();
|
file_input = String::new();
|
||||||
file_output = String::new();
|
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,11 @@ fn convert(args: &Arguments) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn package(args: &Arguments) -> Result<()> {
|
fn package(args: &Arguments) -> Result<()> {
|
||||||
//
|
// Creating a Packager
|
||||||
|
let packager = Packager::new(args.csv_directory.clone(), args.file_output.clone());
|
||||||
|
|
||||||
|
// Starting it
|
||||||
|
packager.start()?;
|
||||||
|
|
||||||
// Ok!!
|
// Ok!!
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
use std::io::Result;
|
use std::fs::File;
|
||||||
|
use std::io::{self, Result};
|
||||||
|
|
||||||
|
use csv::{self, StringRecord};
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
pub struct Packager {
|
pub struct Packager {
|
||||||
|
@ -18,7 +22,66 @@ impl Packager {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
|
fn read_csv(&self, path: &str) -> Result<Vec<StringRecord>> {
|
||||||
|
// Opening the File & Reading
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let mut rdr = csv::Reader::from_reader(file);
|
||||||
|
|
||||||
|
// Creating a list of string records
|
||||||
|
let mut result: Vec<StringRecord> = Vec::new();
|
||||||
|
|
||||||
|
// Iterate through records
|
||||||
|
for record in rdr.records() {
|
||||||
|
result.push(record?);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ok!
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
fn save(&self, records: Vec<StringRecord>) -> Result<()> {
|
||||||
|
// Writing to the path we want
|
||||||
|
let mut writer = csv::Writer::from_path(&self.file_out)?;
|
||||||
|
|
||||||
|
// Creating the Header
|
||||||
|
writer.write_record(&["Date", "Description", "Amount"])?;
|
||||||
|
|
||||||
|
// Adding all Records
|
||||||
|
for record in &records {
|
||||||
|
writer.write_record(record)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flushing!!
|
||||||
|
writer.flush()?;
|
||||||
|
|
||||||
|
// Ok!!
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start(&self) -> Result<()> {
|
pub fn start(&self) -> Result<()> {
|
||||||
|
// Holding all Records
|
||||||
|
let mut records: Vec<StringRecord> = Vec::new();
|
||||||
|
|
||||||
|
// Using WalkDir to go through the Directory
|
||||||
|
for entry in WalkDir::new(&self.directory) {
|
||||||
|
// Reference the Path
|
||||||
|
let entry = entry?;
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
// Is it a file?
|
||||||
|
if !path.is_file() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reading the CSV from the Path
|
||||||
|
let file = self.read_csv(path.to_str().unwrap())?;
|
||||||
|
|
||||||
|
// Adding it to the record list
|
||||||
|
records.extend(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Saving the Records
|
||||||
|
self.save(records)?;
|
||||||
|
|
||||||
// Ok!!
|
// Ok!!
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue