-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(processor): trait default implementation
- Loading branch information
Showing
2 changed files
with
29 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters