Skip to content

Commit

Permalink
refactor(processor): trait default implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pbillaut authored Sep 15, 2024
1 parent ce9ad58 commit 3c1d524
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
33 changes: 29 additions & 4 deletions src/processor.rs
Original file line number Diff line number Diff line change
@@ -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<I>(&self, activities: I) -> Vec<Account>
where
I: Iterator<Item=Result<AccountActivity, Self::Error>>;

fn process<R, W>(&self, input: R, output: W) -> Result<(), Self::Error>
where
R: io::Read,
W: io::Write;

fn process_account_activity<I>(&self, activities: I) -> Vec<Account>
where
I: Iterator<Item=Result<AccountActivity, Self::Error>>,
{
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()
}
}
29 changes: 0 additions & 29 deletions src/processors/csv/processor.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,31 +21,6 @@ impl Default for CsvProcessor {
impl Processor for CsvProcessor {
type Error = CsvProcessorError;

fn process_account_activity<I>(&self, activities: I) -> Vec<Account>
where
I: Iterator<Item=Result<AccountActivity, Self::Error>>,
{
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<R, W>(&self, input: R, output: W) -> Result<(), Self::Error>
where
R: Read,
Expand Down

0 comments on commit 3c1d524

Please sign in to comment.