clippy run

This commit is contained in:
JasterV 2021-07-29 20:01:25 +02:00
parent d2ec72ca15
commit 36e1f5dfda
7 changed files with 32 additions and 33 deletions

View file

@ -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,
1 type client tx amount
2 deposit 1 1 1.0
3 deposit 2 1 2 1 2.0 4.0
4 deposit 2 2 5.0
5 deposit 1 3 2.0
6 withdrawal 1 4 1.5
7 withdrawal 2 5 3.0
8 deposit 3 6 5.0
9 deposit 3 7 2.0
10 dispute 3 7
11 chargeback 3 7

View file

@ -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

View file

@ -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)
}

View file

@ -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>> {

View file

@ -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(())

View file

@ -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(())
}

View file

@ -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>,
}