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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use num::{BigInt, ToPrimitive};
use std::{
    cmp::Ordering,
    convert::TryInto,
    ops::{Add, AddAssign, Mul, Sub},
};

#[derive(Clone, Copy, Debug)]
pub struct Cents {
    value: u64,
}

impl Cents {
    pub fn new(dollars: i64, cents: u8) -> Self {
        let dollars: u64 = num::abs(dollars).try_into().unwrap();

        Self {
            value: (dollars * 100) + cents as u64,
        }
    }
}

impl From<Cents> for u64 {
    fn from(cents: Cents) -> u64 {
        cents.value
    }
}

impl From<u64> for Cents {
    fn from(int: u64) -> Self {
        Self { value: int }
    }
}

impl From<BigInt> for Cents {
    fn from(int: BigInt) -> Self {
        Self {
            value: num::abs(int).to_u64().unwrap(),
        }
    }
}

impl From<&BigInt> for Cents {
    fn from(int: &BigInt) -> Self {
        Self {
            value: num::abs(int.to_owned()).to_u64().unwrap(),
        }
    }
}

impl From<&Cents> for BigInt {
    fn from(cents: &Cents) -> BigInt {
        cents.value.into()
    }
}

impl From<Cents> for BigInt {
    fn from(cents: Cents) -> BigInt {
        cents.value.into()
    }
}

impl Add for Cents {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            value: self.value + other.value,
        }
    }
}

impl Add<u64> for Cents {
    type Output = Self;

    fn add(self, other: u64) -> Self {
        Self {
            value: self.value + other,
        }
    }
}

impl AddAssign for Cents {
    fn add_assign(&mut self, other: Self) {
        *self = Self {
            value: self.value + other.value,
        }
    }
}

impl AddAssign<u64> for Cents {
    fn add_assign(&mut self, other: u64) {
        *self = Self {
            value: self.value + other,
        }
    }
}

impl Mul for Cents {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        Self {
            value: self.value + rhs.value,
        }
    }
}

impl Mul<u64> for Cents {
    type Output = Self;

    fn mul(self, rhs: u64) -> Self::Output {
        Self {
            value: self.value * rhs,
        }
    }
}

impl PartialEq for Cents {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl Eq for Cents {}

impl PartialOrd for Cents {
    fn partial_cmp(&self, other: &Cents) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Cents {
    fn cmp(&self, other: &Cents) -> Ordering {
        self.value.cmp(&other.value)
    }
}

impl Sub for Cents {
    type Output = Cents;

    fn sub(self, other: Cents) -> Cents {
        Self {
            value: self.value - other.value,
        }
    }
}

#[cfg(test)]
mod test {
    use super::Cents;
    use num::bigint::BigInt;

    #[test]
    fn test_add_to_u64() {
        let actual = Cents::new(21, 21) + 2121;
        let expected = Cents::new(42, 42);

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_compare_to_u64() {
        assert_eq!(Cents::new(42, 42), 4242.into());
    }

    #[test]
    fn test_add_two_cents() {
        assert_eq!(Cents::from(2121) + Cents::from(2121), Cents::new(42, 42));
    }

    #[test]
    fn test_one_cent_should_equal_one() {
        assert_eq!(Cents::new(0, 1), 1.into());
    }

    #[test]
    fn test_from_big_int() {
        let forty_two: Cents = BigInt::from(42).into();
        assert_eq!(forty_two, Cents::new(0, 42));
    }

    #[test]
    fn test_to_big_int() {
        let forty_two: BigInt = Cents::new(0, 42).into();
        assert_eq!(forty_two, BigInt::from(42));
    }
}