use std::ops::Div;
use derive_more::Display;
use fpdec::{Dec, Decimal};
use super::{BaseCurrency, ConfigError, Currency, QuoteCurrency};
#[macro_export]
macro_rules! leverage {
( $a:literal ) => {{
$crate::prelude::Leverage::new($a)
.expect("I have read the panic comment and know the leverage must be > 0.")
}};
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Display, Eq)]
pub struct Leverage(u8);
impl Leverage {
pub fn new(val: u8) -> Result<Self, ConfigError> {
if val < Dec!(1) {
Err(ConfigError::InvalidLeverage)?
}
Ok(Self(val))
}
}
impl Div<Leverage> for Decimal {
type Output = Decimal;
fn div(self, rhs: Leverage) -> Self::Output {
self / Decimal::from(rhs.0)
}
}
impl Div<Leverage> for BaseCurrency {
type Output = Self;
fn div(self, rhs: Leverage) -> Self::Output {
BaseCurrency::new(self.as_ref() / Decimal::from(rhs.0))
}
}
impl Div<Leverage> for QuoteCurrency {
type Output = Self;
fn div(self, rhs: Leverage) -> Self::Output {
QuoteCurrency::new(self.as_ref() / Decimal::from(rhs.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_leverage() {
assert_eq!(std::mem::size_of::<Leverage>(), 1);
}
}