architect_api/utils/
dir_pair.rs

1use crate::Dir;
2use rust_decimal::Decimal;
3use rust_decimal_macros::dec;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7/// A dirpair is a structure for holding things that depend on trading direction.
8///
9/// For example one might hold one's position in a particular coin in a `DirPair<Decimal>`.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
11pub struct DirPair<T: 'static> {
12    pub buy: T,
13    pub sell: T,
14}
15
16impl<T: Default + 'static> Default for DirPair<T> {
17    fn default() -> Self {
18        Self { buy: T::default(), sell: T::default() }
19    }
20}
21
22impl<T: 'static> DirPair<T> {
23    /// get a shared reference to the field specified by dir
24    pub fn get(&self, dir: Dir) -> &T {
25        match dir {
26            Dir::Buy => &self.buy,
27            Dir::Sell => &self.sell,
28        }
29    }
30
31    /// get a mutable reference to field side specified by dir
32    pub fn get_mut(&mut self, dir: Dir) -> &mut T {
33        match dir {
34            Dir::Buy => &mut self.buy,
35            Dir::Sell => &mut self.sell,
36        }
37    }
38}
39
40impl DirPair<Decimal> {
41    /// true if both sides are 0
42    pub fn is_empty(&self) -> bool {
43        self.buy == dec!(0) && self.sell == dec!(0)
44    }
45
46    /// net the buy and the sell side (buy - sell)
47    pub fn net(&self) -> Decimal {
48        self.buy - self.sell
49    }
50}