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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::{cmp, fmt, mem};

use crate::{Error, Result};

const DENOM_INT_SHIFT: u8 = 8;
const DENOM_EXP_MAX: u32 = 19;
const DENOM_BASE: u64 = 10;

/// Represents the currency denomination.
///
/// ## Format
///
/// Field  | Integer | Exponent
/// -------|---------|---------
/// Length | 1 byte  | 1 byte
///
/// Denominations representable by a [`u8`] will have a zero exponent, e.g. `100 = 100 * 10^0`.
///
/// Any denomination above [`u8::MAX`] will have a non-zero exponent, e.g. `500 = 50 * 10^1`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Denomination(u16);

impl Denomination {
    /// Creates a new [Denomination].
    pub const fn new() -> Self {
        Self(0x0100)
    }

    /// Gets the `integer` field of the [Denomination].
    pub const fn integer(&self) -> u8 {
        (self.0 >> DENOM_INT_SHIFT) as u8
    }

    /// Gets the `exponent` field of the [Denomination].
    pub const fn exponent(&self) -> u8 {
        self.0 as u8
    }

    /// Gets the value of the [Denomination].
    ///
    /// # Example
    ///
    /// ```
    /// use jcm::Denomination;
    ///
    /// let denom = Denomination::new();
    /// assert_eq!(denom.integer(), 1);
    /// assert_eq!(denom.exponent(), 0);
    /// assert_eq!(denom.value(), 1);
    ///
    /// let denom = Denomination::from_value(500);
    /// assert_eq!(denom.integer(), 50);
    /// assert_eq!(denom.exponent(), 1);
    /// assert_eq!(denom.value(), 500);
    /// ```
    pub fn value(&self) -> u64 {
        let exp = cmp::min(self.exponent() as u32, DENOM_EXP_MAX);
        (self.integer() as u64).saturating_mul(DENOM_BASE.pow(exp))
    }

    /// Infallible function that converts a value into a [Denomination].
    ///
    /// # Example
    ///
    /// ```
    /// use jcm::Denomination;
    ///
    /// let denom = Denomination::from_value(2000);
    /// assert_eq!(denom.integer(), 200);
    /// assert_eq!(denom.exponent(), 1);
    /// assert_eq!(denom.value(), 2000);
    /// ```
    pub fn from_value(val: u64) -> Self {
        match val {
            v if v <= u8::MAX as u64 => Self((val << 8) as u16),
            v if v % 10 == 0 => {
                let exp = (val as f64).log10().floor() as u32;
                let (int, exp) = match val.saturating_div(10u64.pow(exp)) {
                    i if i == 1 || i == 2 => (i * 100, exp - 2),
                    i if i == 5 || i == 25 => (i * 10, exp - 1),
                    i => (i, exp),
                };

                Self(((int << 8) as u16) | exp as u16)
            }
            _ => Self(0),
        }
    }

    /// Gets the length of the [Denomination].
    pub const fn len() -> usize {
        mem::size_of::<u16>()
    }

    /// Gets whether the [Denomination] is empty.
    pub const fn is_empty(&self) -> bool {
        self.0 == 0
    }

    /// Gets whether the [Denomination] is valid.
    pub const fn is_valid(&self) -> bool {
        matches!(self.integer(), 1 | 2 | 5 | 10 | 20 | 50 | 100 | 200 | 250)
    }

    /// Converts the [Denomination] to a [`u16`].
    pub const fn to_u16(&self) -> u16 {
        self.0
    }

    /// Converts the [Denomination] to a [`u16`].
    pub const fn into_u16(self) -> u16 {
        self.0
    }

    /// Infallible function to convert a byte buffer into a [Denomination].
    pub fn from_bytes(val: &[u8]) -> Self {
        match val.len() {
            0 => Self(0),
            1 => Self((val[0] as u16) << DENOM_INT_SHIFT),
            _ => Self(((val[0] as u16) << DENOM_INT_SHIFT) | val[1] as u16),
        }
    }

    /// Gets whether the value is a valid [Denomination].
    pub fn valid_value(val: u64) -> bool {
        [1, 2, 5, 10, 20, 50, 100, 200, 250]
            .into_iter()
            .any(|v| val % v == 0 && (val <= 10 || val % 10 == 0))
    }

    /// Writes the [Denomination] to a byte buffer.
    pub fn to_bytes(&self, buf: &mut [u8]) -> Result<()> {
        let len = Self::len();
        let buf_len = buf.len();

        if buf_len < len {
            Err(Error::InvalidDenominationLen((buf_len, len)))
        } else {
            buf.copy_from_slice(self.to_u16().to_be_bytes().as_ref());
            Ok(())
        }
    }
}

impl TryFrom<u64> for Denomination {
    type Error = Error;

    fn try_from(val: u64) -> Result<Self> {
        match Self::from_value(val) {
            d if d.is_valid() => Ok(d),
            d => Err(Error::InvalidDenomination((d.integer(), d.exponent()))),
        }
    }
}

impl TryFrom<u32> for Denomination {
    type Error = Error;

    fn try_from(val: u32) -> Result<Self> {
        (val as u64).try_into()
    }
}

impl TryFrom<u16> for Denomination {
    type Error = Error;

    fn try_from(val: u16) -> Result<Self> {
        (val as u64).try_into()
    }
}

impl TryFrom<u8> for Denomination {
    type Error = Error;

    fn try_from(val: u8) -> Result<Self> {
        (val as u64).try_into()
    }
}

impl TryFrom<&[u8]> for Denomination {
    type Error = Error;

    fn try_from(val: &[u8]) -> Result<Self> {
        match Self::from_bytes(val) {
            d if d.is_valid() => Ok(d),
            d => Err(Error::InvalidDenomination((d.integer(), d.exponent()))),
        }
    }
}

impl<const N: usize> TryFrom<[u8; N]> for Denomination {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        val.as_ref().try_into()
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for Denomination {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        val.as_ref().try_into()
    }
}

impl Default for Denomination {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for Denomination {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(f, r#""integer": {:#x}, "#, self.integer())?;
        write!(f, r#""exponent": {:#x}, "#, self.exponent())?;
        write!(f, r#""value": {:#x}"#, self.value())?;
        write!(f, "}}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_denomination() {
        let raw_vals = [1, 2, 5, 10, 20, 50, 100, 250, 500, 1000, 10_000u64];
        let exp_ints = [1, 2, 5, 10, 20, 50, 100, 250, 50, 100, 100];
        let exp_exps = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2];
        let exp_denoms = [
            Denomination(0x0100),
            Denomination(0x0200),
            Denomination(0x0500),
            Denomination(0x0a00),
            Denomination(0x1400),
            Denomination(0x3200),
            Denomination(0x6400),
            Denomination(0xfa00),
            Denomination(0x3201),
            Denomination(0x6401),
            Denomination(0x6402),
        ];

        raw_vals.into_iter().enumerate().for_each(|(i, val)| {
            assert_eq!(Denomination::try_from(val), Ok(exp_denoms[i]));
            assert_eq!(
                Denomination::try_from([exp_ints[i], exp_exps[i]]),
                Ok(exp_denoms[i])
            );

            let denom = Denomination::from_value(val);

            assert_eq!(denom, exp_denoms[i]);
            assert_eq!(denom.integer(), exp_ints[i]);
            assert_eq!(denom.exponent(), exp_exps[i]);
            assert_eq!(denom.value(), val);

            assert!(denom.is_valid());
            assert!(!denom.is_empty());
        });
    }

    #[test]
    fn test_denomination_invalid() {
        let zero_denom = Denomination::from_value(0);

        assert!(zero_denom.is_empty());
        assert!(!zero_denom.is_valid());

        (0..=u16::MAX)
            .filter(|&v| !Denomination::valid_value(v as u64))
            .for_each(|val| {
                assert!(!Denomination::from_value(val as u64).is_valid());
                assert!(Denomination::try_from(val).is_err());
            });
    }
}