From 3c1d524f5202bdb84b25e78e0a0ec10b99f13c36 Mon Sep 17 00:00:00 2001 From: Patrice Billaut <57354406+pbillaut@users.noreply.github.com> Date: Sun, 15 Sep 2024 10:35:38 +0200 Subject: [PATCH] refactor(processor): trait default implementation --- src/processor.rs | 33 +++++++++++++++++++++++++++++---- src/processors/csv/processor.rs | 29 ----------------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/processor.rs b/src/processor.rs index 6ab428c..3523ba2 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -1,17 +1,42 @@ use crate::account::Account; use crate::account_activity::AccountActivity; +use std::collections::HashMap; use std::error::Error; use std::io; +use tracing::{error, warn}; pub trait Processor { type Error: Error; - fn process_account_activity(&self, activities: I) -> Vec - where - I: Iterator>; - fn process(&self, input: R, output: W) -> Result<(), Self::Error> where R: io::Read, W: io::Write; + + fn process_account_activity(&self, activities: I) -> Vec + where + I: Iterator>, + { + let mut accounts = HashMap::new(); + for transaction in activities { + match transaction { + Err(err) => { + error!(error = ?err, "error parsing account activity record") + } + Ok(transaction) => { + let account = accounts + .entry(transaction.client_id()) + .or_insert_with(|| Account::new(transaction.client_id())); + if let Err(err) = account.transaction(transaction) { + warn!( + transaction.id = %transaction.transaction_id(), + client.id = %transaction.client_id(), + "error processing account activity: {}",err + ); + } + } + } + } + accounts.into_values().collect() + } } diff --git a/src/processors/csv/processor.rs b/src/processors/csv/processor.rs index db165f8..297f4e3 100644 --- a/src/processors/csv/processor.rs +++ b/src/processors/csv/processor.rs @@ -1,12 +1,8 @@ -use crate::account::Account; -use crate::account_activity::AccountActivity; use crate::processor::Processor; use crate::processors::csv::reader::CsvReader; use crate::processors::csv::writer::CsvWriter; use crate::processors::csv::CsvProcessorError; -use std::collections::HashMap; use std::io::{Read, Write}; -use tracing::{error, warn}; pub struct CsvProcessor; @@ -25,31 +21,6 @@ impl Default for CsvProcessor { impl Processor for CsvProcessor { type Error = CsvProcessorError; - fn process_account_activity(&self, activities: I) -> Vec - where - I: Iterator>, - { - let mut accounts = HashMap::new(); - for transaction in activities { - match transaction { - Err(err) => error!("error parsing account activity {:?}", err), - Ok(transaction) => { - let account = accounts - .entry(transaction.client_id()) - .or_insert_with(|| Account::new(transaction.client_id())); - if let Err(err) = account.transaction(transaction) { - warn!( - transaction_id = %transaction.transaction_id(), - client_id = %transaction.client_id(), - "error processing account activity: {}",err - ); - } - } - } - } - accounts.into_values().collect() - } - fn process(&self, input: R, output: W) -> Result<(), Self::Error> where R: Read,