trader_bot/domain/
money.rs1use rust_decimal::Decimal;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
9pub struct Money {
10 amount: Decimal,
12 currency: String,
14}
15
16impl Money {
17 #[must_use]
19 pub fn new(amount: Decimal, currency: impl Into<String>) -> Self {
20 Self {
21 amount,
22 currency: currency.into(),
23 }
24 }
25
26 #[must_use]
28 pub fn zero(currency: impl Into<String>) -> Self {
29 Self {
30 amount: Decimal::ZERO,
31 currency: currency.into(),
32 }
33 }
34
35 #[must_use]
37 pub const fn amount(&self) -> Decimal {
38 self.amount
39 }
40
41 #[must_use]
43 pub fn currency(&self) -> &str {
44 &self.currency
45 }
46
47 #[must_use]
49 pub fn is_zero(&self) -> bool {
50 self.amount.is_zero()
51 }
52
53 #[must_use]
55 pub fn is_positive(&self) -> bool {
56 self.amount.is_sign_positive() && !self.amount.is_zero()
57 }
58
59 #[must_use]
61 pub fn is_negative(&self) -> bool {
62 self.amount.is_sign_negative()
63 }
64
65 #[must_use]
68 pub fn checked_add(&self, other: &Self) -> Option<Self> {
69 if self.currency != other.currency {
70 return None;
71 }
72 Some(Self {
73 amount: self.amount + other.amount,
74 currency: self.currency.clone(),
75 })
76 }
77
78 #[must_use]
81 pub fn checked_sub(&self, other: &Self) -> Option<Self> {
82 if self.currency != other.currency {
83 return None;
84 }
85 Some(Self {
86 amount: self.amount - other.amount,
87 currency: self.currency.clone(),
88 })
89 }
90
91 #[must_use]
93 pub fn multiply(&self, scalar: Decimal) -> Self {
94 Self {
95 amount: self.amount * scalar,
96 currency: self.currency.clone(),
97 }
98 }
99}
100
101impl fmt::Display for Money {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 write!(f, "{} {}", self.amount, self.currency)
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use rust_decimal_macros::dec;
111
112 #[test]
113 fn test_money_creation() {
114 let money = Money::new(dec!(100.50), "USD");
115 assert_eq!(money.amount(), dec!(100.50));
116 assert_eq!(money.currency(), "USD");
117 }
118
119 #[test]
120 fn test_money_zero() {
121 let money = Money::zero("RUB");
122 assert!(money.is_zero());
123 assert_eq!(money.currency(), "RUB");
124 }
125
126 #[test]
127 fn test_money_add_same_currency() {
128 let a = Money::new(dec!(100), "USD");
129 let b = Money::new(dec!(50), "USD");
130 let result = a.checked_add(&b).unwrap();
131 assert_eq!(result.amount(), dec!(150));
132 }
133
134 #[test]
135 fn test_money_add_different_currency() {
136 let a = Money::new(dec!(100), "USD");
137 let b = Money::new(dec!(50), "EUR");
138 assert!(a.checked_add(&b).is_none());
139 }
140
141 #[test]
142 fn test_money_positive_negative() {
143 let positive = Money::new(dec!(100), "USD");
144 let negative = Money::new(dec!(-50), "USD");
145 let zero = Money::zero("USD");
146
147 assert!(positive.is_positive());
148 assert!(!positive.is_negative());
149
150 assert!(negative.is_negative());
151 assert!(!negative.is_positive());
152
153 assert!(zero.is_zero());
154 assert!(!zero.is_positive());
155 assert!(!zero.is_negative());
156 }
157
158 #[test]
159 fn test_money_multiply() {
160 let money = Money::new(dec!(100), "USD");
161 let result = money.multiply(dec!(1.5));
162 assert_eq!(result.amount(), dec!(150));
163 assert_eq!(result.currency(), "USD");
164 }
165}