use std::fmt;
use std::ops::{Add, Mul, Sub};
use std::str::FromStr;
pub use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use crate::ExchangeError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rounding {
Down,
Up,
Nearest,
}
pub fn round_to(value: Decimal, increment: Decimal, mode: Rounding) -> Decimal {
if increment <= Decimal::ZERO {
return value;
}
let n = value / increment;
let n = match mode {
Rounding::Down => n.floor(),
Rounding::Up => n.ceil(),
Rounding::Nearest => n.round_dp_with_strategy(0, rust_decimal::RoundingStrategy::MidpointAwayFromZero),
};
(n * increment).normalize()
}
pub fn is_multiple(value: Decimal, increment: Decimal) -> bool {
increment <= Decimal::ZERO || (value % increment).is_zero()
}
macro_rules! decimal_newtype {
($(#[$m:meta])* $name:ident) => {
$(#[$m])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(pub Decimal);
impl $name {
pub const ZERO: Self = Self(Decimal::ZERO);
pub fn new(value: Decimal) -> Self {
Self(value)
}
pub fn parse(s: &str) -> Result<Self, ExchangeError> {
Decimal::from_str_exact(s.trim()).map(Self).map_err(|e| ExchangeError::invalid(format!("{s:?}: {e}")))
}
pub fn value(self) -> Decimal {
self.0
}
pub fn is_zero(self) -> bool {
self.0.is_zero()
}
pub fn is_positive(self) -> bool {
self.0 > Decimal::ZERO
}
pub fn round_to(self, increment: Decimal, mode: Rounding) -> Self {
Self(round_to(self.0, increment, mode))
}
pub fn min(self, other: Self) -> Self {
if other < self { other } else { self }
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0.normalize(), f)
}
}
impl FromStr for $name {
type Err = ExchangeError;
fn from_str(s: &str) -> Result<Self, ExchangeError> {
Self::parse(s)
}
}
impl From<Decimal> for $name {
fn from(d: Decimal) -> Self {
Self(d)
}
}
impl Add for $name {
type Output = Self;
fn add(self, o: Self) -> Self {
Self(self.0 + o.0)
}
}
impl Sub for $name {
type Output = Self;
fn sub(self, o: Self) -> Self {
Self(self.0 - o.0)
}
}
};
}
decimal_newtype!(
Price
);
decimal_newtype!(
Qty
);
impl Mul<Qty> for Price {
type Output = Decimal;
fn mul(self, q: Qty) -> Decimal {
self.0 * q.0
}
}
#[cfg(test)]
mod tests {
use super::*;
fn d(s: &str) -> Decimal {
Decimal::from_str_exact(s).unwrap()
}
#[test]
fn exact_parsing_rounding_and_strings() {
let p = Price::parse("64250.105").unwrap();
assert_eq!(p.round_to(d("0.01"), Rounding::Down), Price::parse("64250.10").unwrap());
assert_eq!(p.round_to(d("0.01"), Rounding::Up).to_string(), "64250.11");
assert_eq!(p.round_to(d("0.01"), Rounding::Nearest).to_string(), "64250.11");
assert_eq!(Qty::parse("0.000123456").unwrap().round_to(d("0.00001"), Rounding::Down).to_string(), "0.00012");
assert!(Price::parse("abc").is_err());
assert!(is_multiple(d("0.30"), d("0.1")) && !is_multiple(d("0.35"), d("0.1")) && is_multiple(d("7"), Decimal::ZERO));
assert_eq!(Price::parse("0.1").unwrap() + Price::parse("0.2").unwrap(), Price::parse("0.3").unwrap());
assert_eq!(Price::parse("2.5").unwrap() * Qty::parse("4").unwrap(), d("10"));
let json = serde_json::to_string(&Price::parse("64250.10").unwrap()).unwrap();
assert_eq!(json, "\"64250.10\"");
assert_eq!(serde_json::from_str::<Qty>("\"0.5\"").unwrap(), Qty::parse("0.5").unwrap());
}
}