From 1b7471dfb8ee3bf67241b40989922cc81bf24860 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 23 Aug 2022 16:50:23 +0200 Subject: [PATCH] processor: implement CLI interface --- src/bin/processor.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/bin/processor.rs b/src/bin/processor.rs index e7a11a9..0fc0464 100644 --- a/src/bin/processor.rs +++ b/src/bin/processor.rs @@ -1,3 +1,40 @@ -fn main() { - println!("Hello, world!"); +use thiserror::Error; + +use processor::{Ledger, Transaction}; + +/// Any kind of error in the pipeline CSV parsing -> payment processing -> final state output. +#[derive(Debug, Error)] +pub enum Error { + #[error("missing input file argument")] + MissingFile, + #[error("error during CSV processing: {0}")] + CsvError(#[from] csv::Error), +} + +fn main() -> Result<(), Error> { + let mut ledger = Ledger::new(); + + let path = std::env::args_os() + // Skip argv[0] + .skip(1) + // Expect a file name here + .next() + .ok_or(Error::MissingFile)?; + + for (tx, index) in Transaction::configured_csv_reader_builder() + .from_path(path)? + .into_deserialize() + .zip(1..) + { + match ledger.process(tx?) { + // All errors are logged but should not stop processing + Err(err) => eprintln!("error during processing: transaction {}: {}", index, err), + _ => {} + } + } + + let mut writer = csv::Writer::from_writer(std::io::stdout()); + ledger.dump_csv(&mut writer)?; + + Ok(()) }