-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbanknote.rs
835 lines (724 loc) · 23.2 KB
/
banknote.rs
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
use alloc::string::{String, ToString};
use crate::{
std::{self, fmt},
Currency,
};
pub type ISOCode = Currency;
/// A three character ASCII coded decimal value
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct BaseValue(u16);
impl BaseValue {
pub const LEN: usize = 3;
pub const fn default() -> Self {
Self(0)
}
}
impl From<BaseValue> for u16 {
fn from(b: BaseValue) -> Self {
b.0
}
}
impl From<&BaseValue> for u16 {
fn from(b: &BaseValue) -> Self {
(*b).into()
}
}
impl From<BaseValue> for f32 {
fn from(b: BaseValue) -> Self {
b.0 as f32
}
}
impl From<&BaseValue> for f32 {
fn from(b: &BaseValue) -> Self {
(*b).into()
}
}
impl From<&[u8]> for BaseValue {
fn from(b: &[u8]) -> Self {
if b.len() < Self::LEN {
Self(0)
} else {
// try to parse decimal value from the byte buffer
// any failure results in a default (0) value
let val = std::str::from_utf8(b[..Self::LEN].as_ref())
.unwrap_or("0")
.parse::<u16>()
.unwrap_or(0);
Self(val)
}
}
}
impl<const N: usize> From<[u8; N]> for BaseValue {
fn from(b: [u8; N]) -> Self {
b.as_ref().into()
}
}
impl<const N: usize> From<&[u8; N]> for BaseValue {
fn from(b: &[u8; N]) -> Self {
b.as_ref().into()
}
}
/// An ASCII coded sign value for the Exponent.
/// This field is either a “+” or a “-“
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Sign {
#[default]
Positive,
Negative,
}
impl Sign {
pub const LEN: usize = 1;
pub const fn default() -> Self {
Self::Positive
}
}
impl From<u8> for Sign {
fn from(b: u8) -> Self {
match b {
b'+' => Self::Positive,
b'-' => Self::Negative,
_ => Self::Positive,
}
}
}
impl From<Sign> for &'static str {
fn from(sign: Sign) -> Self {
match sign {
Sign::Negative => "-",
Sign::Positive => "+",
}
}
}
/// ASCII coded decimal value for the power of ten
/// that the base is to either be multiplied by (if Sign
/// is “+”) or divided by (if Sign is “-“)
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Exponent(u8);
impl Exponent {
pub const LEN: usize = 2;
pub const fn new() -> Self {
Self(1)
}
}
impl Default for Exponent {
fn default() -> Self {
Self::new()
}
}
impl From<u8> for Exponent {
fn from(b: u8) -> Self {
Self(b)
}
}
impl From<Exponent> for u8 {
fn from(e: Exponent) -> Self {
e.0
}
}
impl From<&Exponent> for u8 {
fn from(e: &Exponent) -> Self {
(*e).into()
}
}
impl From<Exponent> for f32 {
fn from(e: Exponent) -> Self {
e.0 as f32
}
}
impl From<&Exponent> for f32 {
fn from(e: &Exponent) -> Self {
(*e).into()
}
}
impl From<&[u8]> for Exponent {
fn from(b: &[u8]) -> Self {
if b.len() < Exponent::LEN {
Exponent(1)
} else {
let exp = std::str::from_utf8(b[..Exponent::LEN].as_ref())
.unwrap_or("1")
.parse::<u8>()
.unwrap_or(1);
Self(exp)
}
}
}
impl<const N: usize> From<[u8; N]> for Exponent {
fn from(b: [u8; N]) -> Self {
b.as_ref().into()
}
}
impl<const N: usize> From<&[u8; N]> for Exponent {
fn from(b: &[u8; N]) -> Self {
b.as_ref().into()
}
}
/// A single character binary field that encodes the
/// orientation of the bank note.
///
/// * 0x00 = Right Edge, Face Up
/// * 0x01 = Right Edge, Face Down
/// * 0x02 = Left Edge, Face Up
/// * 0x03 = Left Edge, Face Down
///
/// Note: In general, this field is only correct if the
/// Extended orientation bit is set in device
/// capabilities map.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum BanknoteOrientation {
#[default]
RightEdgeFaceUp = 0x00,
RightEdgeFaceDown = 0x01,
LeftEdgeFaceUp = 0x02,
LeftEdgeFaceDown = 0x03,
}
impl BanknoteOrientation {
pub const LEN: usize = 1;
pub const MASK: u8 = 0b11;
pub const fn default() -> Self {
Self::RightEdgeFaceUp
}
}
impl From<u8> for BanknoteOrientation {
fn from(b: u8) -> Self {
match b & Self::MASK {
0x00 => Self::RightEdgeFaceUp,
0x01 => Self::RightEdgeFaceDown,
0x02 => Self::LeftEdgeFaceUp,
0x03 => Self::LeftEdgeFaceDown,
// Computationally unreachable, but add the default case in the off chance the laws of
// computation break
//
// Avoid the use of the `unreachable` macro because it panics if ever hit, and there is
// a sane default here
_ => Self::default(),
}
}
}
impl From<BanknoteOrientation> for &'static str {
fn from(b: BanknoteOrientation) -> Self {
match b {
BanknoteOrientation::RightEdgeFaceUp => "Right edge face up",
BanknoteOrientation::RightEdgeFaceDown => "Right edge face down",
BanknoteOrientation::LeftEdgeFaceUp => "Left edge face up",
BanknoteOrientation::LeftEdgeFaceDown => "Left edge face down",
}
}
}
impl From<&BanknoteOrientation> for &'static str {
fn from(b: &BanknoteOrientation) -> Self {
(*b).into()
}
}
impl fmt::Display for BanknoteOrientation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bstr: &'static str = self.into();
write!(f, "{bstr}")
}
}
macro_rules! ascii_tuple_struct {
($name:ident, $base:tt, $doc:tt) => {
#[doc = $doc]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct $name($base);
impl $name {
pub const LEN: usize = std::mem::size_of::<$base>();
pub const fn new() -> Self {
Self(0)
}
pub fn to_string(&self) -> String {
std::str::from_utf8(&[self.0]).unwrap_or("").to_string()
}
}
impl From<$base> for $name {
fn from(b: $base) -> Self {
Self(b)
}
}
impl From<$name> for String {
fn from(n: $name) -> String {
n.to_string()
}
}
impl From<&$name> for String {
fn from(n: &$name) -> String {
(*n).into()
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
};
}
ascii_tuple_struct!(
NoteType,
u8,
r"
An ASCII letter that documents the note type.
This corresponds to the data in the variant identity card.
"
);
ascii_tuple_struct!(
NoteSeries,
u8,
r"
An ASCII letter that documents the note series.
This corresponds to the data in the variant identity card.
"
);
ascii_tuple_struct!(
NoteCompatibility,
u8,
r"
An ASCII letter that documents the revision of the recognition core used.
This corresponds to the data in the variant identity card.
"
);
ascii_tuple_struct!(
NoteVersion,
u8,
r"
An ASCII letter that documents the version of the note's recognition criteria.
This corresponds to the data in the variant identity card.
"
);
/// Represents how the BAU device classifies a [Banknote].
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum BanknoteClassification {
/// Sent for any the following:
///
/// - In response to a Host Query Extended Note Specification Command (i.e. host requests a note table element).
/// - In response to a note escrowed or stacked event while device is in extended note mode and classification is
/// - Supported by the device but disabled.
/// - NOT supported by the device.
#[default]
DisabledOrNotSupported = 0x00,
/// Class 1 (unidentified banknote)
Unidentified = 0x01,
/// Class 2 (suspected counterfeit)
SuspectedCounterfeit = 0x02,
/// Class 3 (suspected zero value note)
SuspectedZero = 0x03,
/// Class 4 (genuine banknote)
Genuine = 0x04,
}
impl BanknoteClassification {
pub const fn new() -> Self {
Self::DisabledOrNotSupported
}
}
impl From<u8> for BanknoteClassification {
fn from(b: u8) -> Self {
match b {
0x00 => Self::DisabledOrNotSupported,
0x01 => Self::Unidentified,
0x02 => Self::SuspectedCounterfeit,
0x03 => Self::SuspectedZero,
0x04 => Self::Genuine,
_ => {
log::trace!("Unknown banknote classification: 0x{b:x}");
Self::default()
}
}
}
}
impl From<BanknoteClassification> for &'static str {
fn from(b: BanknoteClassification) -> Self {
match b {
BanknoteClassification::DisabledOrNotSupported => "Disabled or not supported",
BanknoteClassification::Unidentified => "Unidentified",
BanknoteClassification::SuspectedCounterfeit => "Suspected counterfeit",
BanknoteClassification::SuspectedZero => "Suspected zero",
BanknoteClassification::Genuine => "Genuine",
}
}
}
impl From<&BanknoteClassification> for &'static str {
fn from(b: &BanknoteClassification) -> Self {
(*b).into()
}
}
impl fmt::Display for BanknoteClassification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", <&'static str>::from(self))
}
}
/// The banknote value
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Banknote {
/// The banknote value.
pub(crate) value: f32,
/// The banknote ISO code.
pub(crate) iso_code: ISOCode,
/// The banknote type.
pub(crate) note_type: NoteType,
/// The banknote series.
pub(crate) note_series: NoteSeries,
/// The banknote compatibility.
pub(crate) note_compatibility: NoteCompatibility,
/// The banknote version.
pub(crate) note_version: NoteVersion,
/// The banknote classification, see [BanknoteClassification].
pub(crate) banknote_classification: BanknoteClassification,
}
impl Banknote {
/// Creates a new [Banknote] with the provided parameters.
pub const fn new(
value: f32,
iso_code: ISOCode,
note_type: NoteType,
note_series: NoteSeries,
note_compatibility: NoteCompatibility,
note_version: NoteVersion,
banknote_classification: BanknoteClassification,
) -> Self {
Self {
value,
iso_code,
note_type,
note_series,
note_compatibility,
note_version,
banknote_classification,
}
}
/// Creates a new null [Banknote].
pub const fn null() -> Self {
Self {
value: 0.0,
iso_code: ISOCode::new(),
note_type: NoteType::new(),
note_series: NoteSeries::new(),
note_compatibility: NoteCompatibility::new(),
note_version: NoteVersion::new(),
banknote_classification: BanknoteClassification::new(),
}
}
/// Get the banknote value.
pub fn value(&self) -> f32 {
self.value
}
/// Set the banknote value.
pub fn set_value(&mut self, value: u32) {
self.value = value as f32;
}
/// Sets the [Banknote] value, consumes and returns the [Banknote].
pub fn with_value(mut self, value: u32) -> Self {
self.set_value(value);
self
}
/// Get the banknote ISO code.
pub fn iso_code(&self) -> ISOCode {
self.iso_code
}
/// Set the banknote ISO code.
pub fn set_iso_code(&mut self, iso_code: ISOCode) {
self.iso_code = iso_code;
}
/// Get the banknote type.
pub fn note_type(&self) -> NoteType {
self.note_type
}
/// Set the banknote type.
pub fn set_note_type(&mut self, note_type: NoteType) {
self.note_type = note_type;
}
/// Get the banknote series.
pub fn note_series(&self) -> NoteSeries {
self.note_series
}
/// Set the banknote series.
pub fn set_note_series(&mut self, note_series: NoteSeries) {
self.note_series = note_series;
}
/// Get the banknote compatibility.
pub fn note_compatibility(&self) -> NoteCompatibility {
self.note_compatibility
}
/// Set the banknote compatibility.
pub fn set_note_compatibility(&mut self, note_compatibility: NoteCompatibility) {
self.note_compatibility = note_compatibility;
}
/// Get the banknote version.
pub fn note_version(&self) -> NoteVersion {
self.note_version
}
/// Set the banknote version.
pub fn set_note_version(&mut self, note_version: NoteVersion) {
self.note_version = note_version;
}
/// Get the banknote classification.
pub fn banknote_classification(&self) -> BanknoteClassification {
self.banknote_classification
}
/// Set the banknote classification.
pub fn set_banknote_classification(&mut self, banknote_classification: BanknoteClassification) {
self.banknote_classification = banknote_classification;
}
}
impl fmt::Display for Banknote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Value: {} ISO Code: {} Note Type: {} Note Series: {} Note Compatibility: {} Note Version: {} Banknote Classification: {}",
self.value as u64,
self.iso_code,
self.note_type,
self.note_series,
self.note_compatibility,
self.note_version,
self.banknote_classification,
)
}
}
/// Extended note table item
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NoteTableItem {
pub(crate) note_index: usize,
pub(crate) banknote: Banknote,
}
impl NoteTableItem {
/// Creates a new [NoteTableItem].
pub const fn new(note_index: usize, banknote: Banknote) -> Self {
Self {
note_index,
banknote,
}
}
/// Get whether the [NoteTableItem] is null, indicating the end of the note table
pub fn is_null(&self) -> bool {
self == &Self::default()
}
/// Get the note index.
pub fn note_index(&self) -> usize {
self.note_index
}
/// Set the note index.
pub fn set_note_index(&mut self, note_index: usize) {
self.note_index = note_index;
}
/// Get a reference to the banknote.
pub fn banknote(&self) -> &Banknote {
&self.banknote
}
/// Get a mutable reference to the banknote.
pub fn banknote_mut(&mut self) -> &mut Banknote {
&mut self.banknote
}
/// Set the banknote.
pub fn set_banknote(&mut self, banknote: Banknote) {
self.banknote = banknote;
}
}
impl fmt::Display for NoteTableItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Index: {} {}", self.note_index, self.banknote)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iso_code() {
let iso_aud = ISOCode::AUD;
let iso_amd = ISOCode::AMD;
let iso_cad = ISOCode::CAD;
let iso_eur = ISOCode::EUR;
let iso_gbp = ISOCode::GBP;
let iso_mxn = ISOCode::MXN;
let iso_cny = ISOCode::CNY;
let iso_usd = ISOCode::USD;
let iso_xxx = ISOCode::XXX;
assert_eq!(<&str>::from(iso_aud), "AUD");
assert_eq!(<&str>::from(iso_amd), "AMD");
assert_eq!(<&str>::from(iso_cad), "CAD");
assert_eq!(<&str>::from(iso_eur), "EUR");
assert_eq!(<&str>::from(iso_gbp), "GBP");
assert_eq!(<&str>::from(iso_mxn), "MXN");
assert_eq!(<&str>::from(iso_cny), "CNY");
assert_eq!(<&str>::from(iso_usd), "USD");
assert_eq!(<&str>::from(iso_xxx), "XXX");
assert_eq!(ISOCode::from("AUD"), iso_aud);
assert_eq!(ISOCode::from("AMD"), iso_amd);
assert_eq!(ISOCode::from("CAD"), iso_cad);
assert_eq!(ISOCode::from("EUR"), iso_eur);
assert_eq!(ISOCode::from("GBP"), iso_gbp);
assert_eq!(ISOCode::from("MXN"), iso_mxn);
assert_eq!(ISOCode::from("CNY"), iso_cny);
assert_eq!(ISOCode::from("USD"), iso_usd);
assert_eq!(ISOCode::from("XXX"), iso_xxx);
assert_eq!(ISOCode::from(""), iso_xxx);
assert_eq!(ISOCode::from(b"AUD"), iso_aud);
assert_eq!(ISOCode::from(b"AMD"), iso_amd);
assert_eq!(ISOCode::from(b"CAD"), iso_cad);
assert_eq!(ISOCode::from(b"EUR"), iso_eur);
assert_eq!(ISOCode::from(b"GBP"), iso_gbp);
assert_eq!(ISOCode::from(b"MXN"), iso_mxn);
assert_eq!(ISOCode::from(b"CNY"), iso_cny);
assert_eq!(ISOCode::from(b"USD"), iso_usd);
assert_eq!(ISOCode::from(b"XXX"), iso_xxx);
assert_eq!(ISOCode::from(b""), iso_xxx);
}
#[test]
fn test_base_value() {
let base_value = BaseValue(42);
assert_eq!(u16::from(base_value), 42);
assert_eq!(f32::from(base_value), 42.0);
assert_eq!(BaseValue::from(b"042"), base_value);
// Check that values that are too short get parsed as the default value
for i in 0..=u8::MAX {
assert_eq!(BaseValue::from([i]), BaseValue::default());
for j in 0..=u8::MAX {
assert_eq!(BaseValue::from([i, j]), BaseValue::default());
}
}
// Check that values that are too long only parse the first BaseValue::LEN bytes
assert_eq!(BaseValue::from(b"042f"), base_value);
}
#[test]
fn test_sign() {
let sign_pos = Sign::Positive;
let sign_neg = Sign::Negative;
assert_eq!(Sign::from(b'+'), sign_pos);
assert_eq!(Sign::from(b'-'), sign_neg);
for b in 0..=u8::MAX {
if b != b'-' {
assert_eq!(Sign::from(b), sign_pos);
}
}
assert_eq!(<&'static str>::from(sign_pos), "+");
assert_eq!(<&'static str>::from(sign_neg), "-");
}
#[test]
fn test_exponent() {
let exp_max = Exponent(99);
let exp_def = Exponent(1);
let exp_min = Exponent(0);
assert_eq!(Exponent::default(), exp_def);
assert_eq!(Exponent::from(b"99"), exp_max);
assert_eq!(Exponent::from(b"00"), exp_min);
// Check that values that are too short are parsed as the default value
assert_eq!(Exponent::from([]), exp_def);
for i in 0..=u8::MAX {
// Check that values that are too short are parsed as the default value
assert_eq!(Exponent::from([i]), exp_def);
for j in 0..=u8::MAX {
if i == b'+' && j.is_ascii_digit() {
assert_eq!(
Exponent::from([i, j]),
Exponent(std::str::from_utf8(&[j]).unwrap().parse::<u8>().unwrap())
);
} else if !i.is_ascii_digit() || !j.is_ascii_digit() {
// Check that values outside the valid range are parsed as the default value
assert_eq!(
Exponent::from([i, j]),
exp_def,
"i: {i}, j: {j}, string({})",
std::str::from_utf8(&[i, j]).unwrap()
);
}
}
}
}
#[test]
fn test_banknote_orientation() {
assert_eq!(
BanknoteOrientation::from(0x00),
BanknoteOrientation::RightEdgeFaceUp
);
assert_eq!(
BanknoteOrientation::from(0x01),
BanknoteOrientation::RightEdgeFaceDown
);
assert_eq!(
BanknoteOrientation::from(0x02),
BanknoteOrientation::LeftEdgeFaceUp
);
assert_eq!(
BanknoteOrientation::from(0x03),
BanknoteOrientation::LeftEdgeFaceDown
);
// Check that values outside the base range are parsed as their masked values
for i in 0x04..=u8::MAX {
assert_eq!(
BanknoteOrientation::from(i),
BanknoteOrientation::from(i & BanknoteOrientation::MASK)
);
}
}
#[test]
fn test_banknote_classification() {
assert_eq!(
BanknoteClassification::from(0x00),
BanknoteClassification::DisabledOrNotSupported
);
assert_eq!(
BanknoteClassification::from(0x01),
BanknoteClassification::Unidentified
);
assert_eq!(
BanknoteClassification::from(0x02),
BanknoteClassification::SuspectedCounterfeit
);
assert_eq!(
BanknoteClassification::from(0x03),
BanknoteClassification::SuspectedZero
);
assert_eq!(
BanknoteClassification::from(0x04),
BanknoteClassification::Genuine
);
for i in 0x05..=u8::MAX {
assert_eq!(
BanknoteClassification::from(i),
BanknoteClassification::default()
);
}
}
#[test]
fn test_ascii_tuples() {
let ascii_table = [
b' ', b'!', b'"', b'#', b'$', b'%', b'&', b'\'', b'(', b')', b'*', b'+', b',', b'-',
b'.', b'/', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', b';',
b'<', b'=', b'>', b'?', b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I',
b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
b'X', b'Y', b'Z', b'[', b'\\', b']', b'^', b'_', b'`', b'a', b'b', b'c', b'd', b'e',
b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's',
b't', b'u', b'v', b'w', b'x', b'y', b'z', b'{', b'|', b'}', b'~',
];
for c in 0..=u8::MAX {
if (32..=126).contains(&c) {
let ascii_index = (c - 32) as usize;
let ascii_val = ascii_table[ascii_index];
// Check that printable ASCII characters are parsed as non-empty strings
assert_eq!(NoteType::from(c).to_string().as_bytes()[0], ascii_val);
assert_eq!(NoteSeries::from(c).to_string().as_bytes()[0], ascii_val);
assert_eq!(
NoteCompatibility::from(c).to_string().as_bytes()[0],
ascii_val
);
assert_eq!(NoteVersion::from(c).to_string().as_bytes()[0], ascii_val);
} else if c < 128 {
// Check that non-printable ASCII characters are parsed as their unicode values
assert!(!NoteType::from(c).to_string().is_empty());
assert!(!NoteSeries::from(c).to_string().is_empty());
assert!(!NoteCompatibility::from(c).to_string().is_empty());
assert!(!NoteVersion::from(c).to_string().is_empty());
} else {
// Check that all other values are parsed as an empty string
assert!(NoteType::from(c).to_string().is_empty());
assert!(NoteSeries::from(c).to_string().is_empty());
assert!(NoteCompatibility::from(c).to_string().is_empty());
assert!(NoteVersion::from(c).to_string().is_empty());
}
}
}
}