1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use derive_more::Display;
use fpdec::{Dec, Decimal};

use crate::types::errors::{Error, Result};

/// Allows the quick construction of `Leverage`
///
/// # Panics:
/// if a value < 1 is provided.
#[macro_export]
macro_rules! leverage {
    ( $a:literal ) => {{
        use $crate::prelude::fpdec::Decimal;
        $crate::prelude::Leverage::new($crate::prelude::fpdec::Dec!($a))
            .expect("I have read the panic comment and know the leverage must be > 0.")
    }};
}

/// Leverage
#[derive(Default, Debug, Clone, Copy, PartialEq, Display, Eq)]
pub struct Leverage(Decimal);

impl Leverage {
    /// Create a new instance from a `Decimal` value
    pub fn new(val: Decimal) -> Result<Self> {
        if val < Dec!(1) {
            Err(Error::InvalidLeverage)?
        }
        Ok(Self(val))
    }
}

impl AsRef<Decimal> for Leverage {
    fn as_ref(&self) -> &Decimal {
        &self.0
    }
}