From c7e64692e6d5c34f3922d5599ca465d48dbe36e2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 22 Aug 2022 17:51:31 +0200 Subject: [PATCH] lib: add 'Transaction' variants Represent each enum variant with its own type to allow stronger typing when processing only a singular type of transactions. --- src/lib.rs | 3 +++ src/transaction.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/transaction.rs diff --git a/src/lib.rs b/src/lib.rs index 77a59f8..f82bd39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,5 @@ pub mod core; pub use crate::core::*; + +pub mod transaction; +pub use crate::transaction::*; diff --git a/src/transaction.rs b/src/transaction.rs new file mode 100644 index 0000000..a9f8110 --- /dev/null +++ b/src/transaction.rs @@ -0,0 +1,57 @@ +//! Define all supported transactions. +use crate::core::{ClientId, TxAmount, TxId}; + +/// A generic [Transaction]. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Transaction { + Deposit(Deposit), + Withdrawal(Withdrawal), + Dispute(Dispute), + Resolve(Resolve), + Chargeback(Chargeback), +} + +/// Deposit funds into an account, i.e: increase its balance by the amount given. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Deposit { + pub client: ClientId, + pub tx: TxId, + pub amount: TxAmount, +} + +/// Withdraw funds from an account, i.e: the opposite of a [Deposit]. It is not allowed to withdraw +/// more than is available on the given account, and should result in a no-op. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Withdrawal { + pub client: ClientId, + pub tx: TxId, + pub amount: TxAmount, +} + +/// Hold funds for an erroneous transaction that should be reversed. Extract the amount of funds +/// corresponding to the given transaction into a held funds envelop by transfering it from their +/// available funds. If the given transaction does not exist, this results in a no-op. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Dispute { + pub client: ClientId, + pub tx: TxId, +} + +/// Resolve a [Dispute] in favor of the client: move the held funds for the diputed transaction +/// back to the available funds. If either the given transaction does not exist, or is not +/// disputed, this results in a no-op. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Resolve { + pub client: ClientId, + pub tx: TxId, +} + +/// Resolve [Dispute] by withdrawing held funds. The held funds are decreased by the amount of the +/// transaction. An account which succesffully executed a chargeback is subsequently frozen. If +/// either the transaction does not exist, or is not disputed, this results in a no-op and the +/// account is *not* frozen. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Chargeback { + pub client: ClientId, + pub tx: TxId, +}