rex_shared/
models.rs

1use std::cmp::Ordering;
2use std::fmt;
3use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
4
5impl Add<i64> for Cent {
6    type Output = Cent;
7
8    fn add(self, rhs: i64) -> Self::Output {
9        Cent(self.0 + rhs)
10    }
11}
12
13impl Add<f64> for Dollar {
14    type Output = Dollar;
15
16    fn add(self, rhs: f64) -> Self::Output {
17        Dollar(self.0 + rhs)
18    }
19}
20
21impl Sub<i64> for Cent {
22    type Output = Cent;
23
24    fn sub(self, rhs: i64) -> Self::Output {
25        Cent(self.0 - rhs)
26    }
27}
28
29impl Sub<f64> for Dollar {
30    type Output = Dollar;
31
32    fn sub(self, rhs: f64) -> Self::Output {
33        Dollar(self.0 - rhs)
34    }
35}
36
37impl Sub for Dollar {
38    type Output = Dollar;
39
40    fn sub(self, rhs: Dollar) -> Self::Output {
41        Dollar(self.0 - rhs.0)
42    }
43}
44
45impl Div for Dollar {
46    type Output = f64;
47
48    fn div(self, rhs: Dollar) -> Self::Output {
49        self.0 / rhs.0
50    }
51}
52
53impl Mul<i64> for Cent {
54    type Output = Cent;
55
56    fn mul(self, rhs: i64) -> Self::Output {
57        Cent(self.0 * rhs)
58    }
59}
60
61impl Mul<f64> for Dollar {
62    type Output = Dollar;
63
64    fn mul(self, rhs: f64) -> Self::Output {
65        Dollar(self.0 * rhs)
66    }
67}
68
69impl AddAssign<f64> for Dollar {
70    fn add_assign(&mut self, rhs: f64) {
71        self.0 += rhs;
72    }
73}
74
75impl AddAssign<i64> for Cent {
76    fn add_assign(&mut self, rhs: i64) {
77        self.0 += rhs;
78    }
79}
80
81impl SubAssign<i64> for Cent {
82    fn sub_assign(&mut self, rhs: i64) {
83        self.0 -= rhs;
84    }
85}
86
87impl SubAssign for Cent {
88    fn sub_assign(&mut self, rhs: Self) {
89        self.0 -= rhs.0;
90    }
91}
92
93impl AddAssign for Cent {
94    fn add_assign(&mut self, other: Self) {
95        self.0 += other.0;
96    }
97}
98
99impl AddAssign<Dollar> for f64 {
100    fn add_assign(&mut self, other: Dollar) {
101        *self += other.0;
102    }
103}
104
105impl Div<i64> for Cent {
106    type Output = Cent;
107
108    fn div(self, rhs: i64) -> Self::Output {
109        Cent(self.0 / rhs)
110    }
111}
112
113impl Div<f64> for Dollar {
114    type Output = Dollar;
115
116    fn div(self, rhs: f64) -> Self::Output {
117        Dollar(self.0 / rhs)
118    }
119}
120
121impl AddAssign<Cent> for i64 {
122    fn add_assign(&mut self, rhs: Cent) {
123        *self += rhs.0;
124    }
125}
126
127impl PartialEq<Cent> for i64 {
128    fn eq(&self, other: &Cent) -> bool {
129        *self == other.0
130    }
131}
132
133impl SubAssign<Cent> for i64 {
134    fn sub_assign(&mut self, rhs: Cent) {
135        *self -= rhs.0;
136    }
137}
138
139impl PartialEq<i64> for Cent {
140    fn eq(&self, other: &i64) -> bool {
141        self.0 == *other
142    }
143}
144
145impl PartialEq<f64> for Dollar {
146    fn eq(&self, other: &f64) -> bool {
147        self.0 == *other
148    }
149}
150
151impl PartialEq for Cent {
152    fn eq(&self, other: &Self) -> bool {
153        self.0 == other.0
154    }
155}
156
157impl PartialEq for Dollar {
158    fn eq(&self, other: &Self) -> bool {
159        self.0 == other.0
160    }
161}
162
163impl PartialOrd for Cent {
164    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
165        self.0.partial_cmp(&other.0)
166    }
167}
168
169impl PartialOrd for Dollar {
170    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
171        self.0.partial_cmp(&other.0)
172    }
173}
174
175impl PartialOrd<i64> for Cent {
176    fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
177        self.0.partial_cmp(other)
178    }
179}
180
181impl PartialOrd<f64> for Dollar {
182    fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
183        self.0.partial_cmp(other)
184    }
185}
186
187impl fmt::Display for Dollar {
188    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189        write!(f, "{:.2}", self.0)
190    }
191}
192
193#[derive(Debug, Clone, Copy, Default)]
194pub struct Cent(i64);
195
196#[derive(Debug, Clone, Copy, Default)]
197pub struct Dollar(f64);
198
199impl Cent {
200    #[must_use]
201    pub fn new(value: i64) -> Self {
202        Self(value)
203    }
204
205    #[must_use]
206    pub fn dollar(&self) -> Dollar {
207        Dollar::new(self.0 as f64 / 100.0)
208    }
209
210    #[must_use]
211    pub fn value(&self) -> i64 {
212        self.0
213    }
214}
215
216impl Dollar {
217    #[must_use]
218    pub fn new(value: f64) -> Self {
219        Self(value)
220    }
221
222    #[must_use]
223    pub fn value(&self) -> f64 {
224        self.0
225    }
226
227    #[must_use]
228    pub fn cent(&self) -> Cent {
229        Cent::new((self.0 * 100.0) as i64)
230    }
231}