mirror of
https://codeberg.org/JasterV/transactions-processor.git
synced 2026-04-26 18:10:06 +00:00
clippy run
This commit is contained in:
parent
d2ec72ca15
commit
36e1f5dfda
7 changed files with 32 additions and 33 deletions
|
|
@ -1,6 +1,11 @@
|
|||
type,client,tx,amount
|
||||
deposit,1,1,1.0
|
||||
deposit,2,2,2.0
|
||||
deposit,1,1,4.0
|
||||
deposit,2,2,5.0
|
||||
deposit,1,3,2.0
|
||||
withdrawal,1,4,1.5
|
||||
withdrawal,2,5,3.0
|
||||
withdrawal,2,5,3.0
|
||||
deposit,3,6,5.0
|
||||
deposit,3,7,2.0
|
||||
dispute,3,7,
|
||||
chargeback,3,7,
|
||||
|
|
|
@ -44,12 +44,10 @@ impl AccountActor {
|
|||
}
|
||||
|
||||
fn dispute(&mut self, tx_id: u32) -> Result<()> {
|
||||
let client_id = self.account.get_client();
|
||||
let tx = self.transactions
|
||||
.get_mut(&tx_id)
|
||||
.ok_or(AccountError::TxNotFound(
|
||||
tx_id,
|
||||
self.account.get_client()
|
||||
))?;
|
||||
.ok_or(AccountError::TxNotFound(tx_id, client_id))?;
|
||||
if tx.ty == TransactionType::Deposit && !tx.disputed {
|
||||
self.account.held(tx.amount)?;
|
||||
tx.disputed = true;
|
||||
|
|
@ -58,12 +56,10 @@ impl AccountActor {
|
|||
}
|
||||
|
||||
fn resolve(&mut self, tx_id: u32) -> Result<()> {
|
||||
let client_id = self.account.get_client();
|
||||
let tx = self.transactions
|
||||
.get_mut(&tx_id)
|
||||
.ok_or(AccountError::TxNotFound(
|
||||
tx_id,
|
||||
self.account.get_client()
|
||||
))?;
|
||||
.ok_or(AccountError::TxNotFound(tx_id, client_id))?;
|
||||
if tx.disputed {
|
||||
self.account.free(tx.amount)?;
|
||||
tx.disputed = false;
|
||||
|
|
@ -72,12 +68,10 @@ impl AccountActor {
|
|||
}
|
||||
|
||||
fn chargeback(&mut self, tx_id: u32) -> Result<()> {
|
||||
let client_id = self.account.get_client();
|
||||
let tx = self.transactions
|
||||
.get_mut(&tx_id)
|
||||
.ok_or(AccountError::TxNotFound(
|
||||
tx_id,
|
||||
self.account.get_client()
|
||||
))?;
|
||||
.ok_or(AccountError::TxNotFound(tx_id, client_id))?;
|
||||
if tx.disputed {
|
||||
self
|
||||
.account
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ impl From<Transaction> for Command {
|
|||
fn from(tx: Transaction) -> Self {
|
||||
match tx.ty {
|
||||
TransactionType::Chargeback => Command::Chargeback(tx.tx),
|
||||
TransactionType::Deposit => Command::Deposit(tx.tx, tx.amount),
|
||||
TransactionType::Withdrawal => Command::Withdraw(tx.tx, tx.amount),
|
||||
TransactionType::Deposit => Command::Deposit(tx.tx, tx.amount.unwrap()),
|
||||
TransactionType::Withdrawal => Command::Withdraw(tx.tx, tx.amount.unwrap()),
|
||||
TransactionType::Dispute => Command::Dispute(tx.tx),
|
||||
TransactionType::Resolve => Command::Resolve(tx.tx)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,15 +15,12 @@ impl TxProcessor {
|
|||
}
|
||||
|
||||
async fn send_tx(&mut self, transaction: Transaction) -> Result<()> {
|
||||
if self.accounts.contains_key(&transaction.client) {
|
||||
self.accounts.get(&transaction.client).unwrap().send(transaction.into()).await?;
|
||||
} else {
|
||||
let actor = AccountActor::new(transaction.client);
|
||||
let addr = run_actor(actor);
|
||||
self.accounts.insert(transaction.client, addr.clone());
|
||||
addr.send(transaction.into()).await?;
|
||||
}
|
||||
Ok(())
|
||||
let addr = self.accounts.entry(transaction.client).or_insert_with(|| {
|
||||
let actor = AccountActor::new(transaction.client);
|
||||
run_actor(actor)
|
||||
});
|
||||
addr.send(transaction.into()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn stop_actors(&self) -> Result<Vec<Account>> {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ mod actors;
|
|||
|
||||
use anyhow::Result;
|
||||
use csv_async::DeserializeRecordsStream;
|
||||
use models::transaction::TransactionType;
|
||||
use std::env;
|
||||
use tokio::{fs::File, sync::{mpsc::Sender, oneshot}};
|
||||
use tokio_stream::StreamExt;
|
||||
|
|
@ -27,9 +28,11 @@ async fn _main(mut records: DeserializeRecordsStream<'_, File, Transaction>) ->
|
|||
|
||||
while let Some(record) = records.next().await {
|
||||
let transaction = record?;
|
||||
if (transaction.ty == TransactionType::Withdrawal || transaction.ty == TransactionType::Deposit) && transaction.amount.is_none() {
|
||||
panic!("Transaction {} does not have a valid amount", transaction.tx)
|
||||
}
|
||||
tx_processor_addr.send(Command::SendTx(transaction)).await?;
|
||||
}
|
||||
|
||||
let accounts = send_stop(&tx_processor_addr).await?;
|
||||
display_accounts(&accounts);
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ impl Account {
|
|||
pub fn withdraw(&mut self, amount: f32) -> Result<()> {
|
||||
self.assert_lock()?;
|
||||
if amount > self.available {
|
||||
Err(TransactionError::WithdrawError)?;
|
||||
return Err(TransactionError::WithdrawError.into());
|
||||
}
|
||||
self.available -= amount;
|
||||
self.total -= amount;
|
||||
|
|
@ -53,7 +53,7 @@ impl Account {
|
|||
pub fn held(&mut self, amount: f32) -> Result<()> {
|
||||
self.assert_lock()?;
|
||||
if amount > self.available {
|
||||
Err(TransactionError::HeldError)?;
|
||||
return Err(TransactionError::HeldError.into());
|
||||
}
|
||||
self.available -= amount;
|
||||
self.held += amount;
|
||||
|
|
@ -63,7 +63,7 @@ impl Account {
|
|||
pub fn free(&mut self, amount: f32) -> Result<()> {
|
||||
self.assert_lock()?;
|
||||
if amount > self.held {
|
||||
Err(TransactionError::UnheldError)?;
|
||||
return Err(TransactionError::UnheldError.into());
|
||||
}
|
||||
self.held -= amount;
|
||||
self.available += amount;
|
||||
|
|
@ -73,7 +73,7 @@ impl Account {
|
|||
pub fn chargeback(&mut self, amount: f32) -> Result<()> {
|
||||
self.assert_lock()?;
|
||||
if amount > self.held {
|
||||
Err(TransactionError::UnheldError)?;
|
||||
return Err(TransactionError::UnheldError.into());
|
||||
}
|
||||
self.held -= amount;
|
||||
self.total -= amount;
|
||||
|
|
@ -83,7 +83,7 @@ impl Account {
|
|||
|
||||
fn assert_lock(&self) -> Result<()> {
|
||||
if self.locked {
|
||||
Err(TransactionError::AccountLocked(self.client))?
|
||||
Err(TransactionError::AccountLocked(self.client).into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,6 @@ pub struct Transaction {
|
|||
pub ty: TransactionType,
|
||||
pub client: u16,
|
||||
pub tx: u32,
|
||||
#[serde(default)]
|
||||
pub amount: f32,
|
||||
pub amount: Option<f32>,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue