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
#[cfg(not(feature = "std"))]
use alloc::string::String;

use crate::std;
use std::fmt;

/// Cash denominations
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Denomination {
    Zero = 0,
    One = 1,
    Two = 2,
    Five = 5,
    Ten = 10,
    Twenty = 20,
    Fifty = 50,
    Hundred = 100,
    TwoHundred = 200,
    FiveHundred = 500,
    Thousand = 1000,
    TwoThousand = 2000,
    FiveThousand = 5000,
    TenThousand = 10_000,
    TwentyThousand = 20_000,
    FiftyThousand = 50_000,
    HundredThousand = 100_000,
}

impl From<Denomination> for &'static str {
    fn from(d: Denomination) -> Self {
        match d {
            Denomination::Zero => "Zero",
            Denomination::One => "One",
            Denomination::Two => "Two",
            Denomination::Five => "Five",
            Denomination::Ten => "Ten",
            Denomination::Twenty => "Twenty",
            Denomination::Fifty => "Fifty",
            Denomination::Hundred => "Hundred",
            Denomination::TwoHundred => "Two hundred",
            Denomination::FiveHundred => "Five hundred",
            Denomination::Thousand => "Thousand",
            Denomination::TwoThousand => "Two thousand",
            Denomination::FiveThousand => "Five thousand",
            Denomination::TenThousand => "Ten thousand",
            Denomination::TwentyThousand => "Twenty thousand",
            Denomination::FiftyThousand => "Fifty thousand",
            Denomination::HundredThousand => "Hundred thousand",
        }
    }
}

impl From<&Denomination> for &'static str {
    fn from(d: &Denomination) -> Self {
        (*d).into()
    }
}

impl fmt::Display for Denomination {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", <&'static str>::from(self))
    }
}

impl From<u32> for Denomination {
    fn from(digit: u32) -> Self {
        match digit {
            0 => Self::Zero,
            1 => Self::One,
            2 => Self::Two,
            5 => Self::Five,
            10 => Self::Ten,
            20 => Self::Twenty,
            50 => Self::Fifty,
            100 => Self::Hundred,
            200 => Self::TwoHundred,
            500 => Self::FiveHundred,
            1000 => Self::Thousand,
            2000 => Self::TwoThousand,
            5000 => Self::FiveThousand,
            10_000 => Self::TenThousand,
            20_000 => Self::TwentyThousand,
            50_000 => Self::FiftyThousand,
            100_000 => Self::HundredThousand,
            _ => Self::Zero,
        }
    }
}

impl From<Denomination> for u32 {
    fn from(d: Denomination) -> Self {
        d as u32
    }
}

bitfield! {
    /// Enable/disable note denominations while in base note mode
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub struct StandardDenomination(u8);
    u8;
    pub one, set_one: 0;
    pub two, set_two: 1;
    pub three, set_three: 2;
    pub four, set_four: 3;
    pub five, set_five: 4;
    pub six, set_six: 5;
    pub seven, set_seven: 6;
}

mod bitmask {
    pub const DENOMINATION: u8 = 0b111_1111;
}

impl StandardDenomination {
    /// Creates a [Denomination] with all denominations set.
    pub const fn all() -> Self {
        Self(bitmask::DENOMINATION)
    }

    /// Creates a [Denomination] with no denominations set.
    pub const fn none() -> Self {
        Self(0)
    }

    /// Converts from the [ExceptionStatus](crate::status::ExceptionStatus) `note_value` field.
    pub const fn from_note_value(note_value: u8) -> Self {
        match note_value {
            0b000 => Self::none(),
            0b001..=0b111 => Self(1 << (note_value - 1)),
            _ => Self::none(),
        }
    }

    /// Sets all denomintations.
    pub fn set_all(&mut self) {
        self.0 |= bitmask::DENOMINATION;
    }

    /// Inverts all the denomination bits.
    pub fn set_inverted(&mut self) {
        self.0 ^= bitmask::DENOMINATION;
    }

    /// Inverts all the denomination bits.
    pub fn invert(&self) -> Self {
        Self(self.0 ^ bitmask::DENOMINATION)
    }
}

fn denom_delimiter(has_denom: bool) -> &'static str {
    if has_denom {
        ","
    } else {
        ""
    }
}

impl fmt::Display for StandardDenomination {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut dis = String::new();
        let mut has_denom = false;

        if self.one() {
            dis += "Denom1";
            has_denom = true;
        }
        if self.two() {
            dis = dis + denom_delimiter(has_denom) + "Denom2";
            has_denom = true;
        }
        if self.three() {
            dis = dis + denom_delimiter(has_denom) + "Denom3";
            has_denom = true;
        }
        if self.four() {
            dis = dis + denom_delimiter(has_denom) + "Denom4";
            has_denom = true;
        }
        if self.five() {
            dis = dis + denom_delimiter(has_denom) + "Denom5";
            has_denom = true;
        }
        if self.six() {
            dis = dis + denom_delimiter(has_denom) + "Denom6";
            has_denom = true;
        }
        if self.seven() {
            dis = dis + denom_delimiter(has_denom) + "Denom7";
            has_denom = true;
        }

        if has_denom {
            write!(f, "{}", dis)
        } else {
            write!(f, "None")
        }
    }
}

impl From<StandardDenomination> for u8 {
    fn from(d: StandardDenomination) -> Self {
        d.0
    }
}

impl From<&StandardDenomination> for u8 {
    fn from(d: &StandardDenomination) -> Self {
        d.0
    }
}

impl From<u8> for StandardDenomination {
    fn from(b: u8) -> Self {
        Self(b & bitmask::DENOMINATION)
    }
}

impl From<StandardDenominationFlag> for StandardDenomination {
    fn from(f: StandardDenominationFlag) -> Self {
        Self(f as u8)
    }
}

/// Bit flags for [StandardDenomination]s.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StandardDenominationFlag {
    Denom1 = 0b000_0001,
    Denom2 = 0b000_0010,
    Denom3 = 0b000_0100,
    Denom4 = 0b000_1000,
    Denom5 = 0b001_0000,
    Denom6 = 0b010_0000,
    Denom7 = 0b100_0000,
    Zero = 0b000_0000,
}

impl StandardDenominationFlag {
    pub const fn default() -> Self {
        Self::Zero
    }
}

impl From<StandardDenomination> for StandardDenominationFlag {
    fn from(d: StandardDenomination) -> Self {
        // matches lowest value first
        if d.one() {
            Self::Denom1
        } else if d.two() {
            Self::Denom2
        } else if d.three() {
            Self::Denom3
        } else if d.four() {
            Self::Denom4
        } else if d.five() {
            Self::Denom5
        } else if d.six() {
            Self::Denom6
        } else if d.seven() {
            Self::Denom7
        } else {
            Self::Zero
        }
    }
}