1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug)]
5pub enum PinBlockError {
6 InvalidPin,
7 RandomSeedTooShort { minimum: usize, actual: usize },
8 InvalidPinFieldLength { expected: usize, actual: usize },
9 InvalidControlField { actual: u8 },
10 InvalidDecodedPinLength { actual: usize },
11 InvalidPinDigit,
12 InvalidFiller,
13 InvalidPan,
14 InvalidPinBlockLength { expected: usize, actual: usize },
15 InvalidIntermediateBlockLength { expected: usize, actual: usize },
16 Hex(hex::FromHexError),
17}
18
19impl Display for PinBlockError {
20 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21 match self {
22 Self::InvalidPin => write!(
23 f,
24 "PIN BLOCK ISO 4 ERROR: PIN must be between 4 and 12 digits long"
25 ),
26
27 Self::RandomSeedTooShort { .. } => write!(
28 f,
29 "PIN BLOCK ISO 4 ERROR: Random seed must be at least 8 bytes long"
30 ),
31
32 Self::InvalidPinFieldLength { .. } => {
33 write!(f, "PIN BLOCK ISO 4 ERROR: PIN field must be 16 bytes long")
34 }
35
36 Self::InvalidControlField { actual } => write!(
37 f,
38 "PIN BLOCK ISO 4 ERROR: PIN block is not ISO format 4: control field `{actual}`"
39 ),
40
41 Self::InvalidDecodedPinLength { actual } => write!(
42 f,
43 "PIN BLOCK ISO 4 ERROR: PIN length must be between 4 and 12: `{actual}`"
44 ),
45
46 Self::InvalidPinDigit => write!(f, "PIN BLOCK ISO 4 ERROR: PIN contains invalid digit"),
47
48 Self::InvalidFiller => {
49 write!(f, "PIN BLOCK ISO 4 ERROR: PIN block filler is incorrect")
50 }
51
52 Self::InvalidPan => write!(
53 f,
54 "PIN BLOCK ISO 4 ERROR: PAN must be between 1 and 19 digits long."
55 ),
56
57 Self::InvalidPinBlockLength { .. } => {
58 write!(f, "PIN BLOCK ISO 4 ERROR: PIN block must be 16 bytes long")
59 }
60
61 Self::InvalidIntermediateBlockLength { .. } => write!(
62 f,
63 "PIN BLOCK ISO 4 ERROR: Intermediate block must be 16 bytes long"
64 ),
65
66 Self::Hex(error) => Display::fmt(error, f),
67 }
68 }
69}
70
71impl Error for PinBlockError {
72 fn source(&self) -> Option<&(dyn Error + 'static)> {
73 match self {
74 Self::Hex(error) => Some(error),
75 _ => None,
76 }
77 }
78}
79
80impl From<hex::FromHexError> for PinBlockError {
81 fn from(error: hex::FromHexError) -> Self {
82 Self::Hex(error)
83 }
84}
85
86#[derive(Debug)]
87pub enum PinBlockCryptoError<E> {
88 PinBlock(PinBlockError),
89 Crypto(E),
90}
91
92impl<E> Display for PinBlockCryptoError<E>
93where
94 E: Display,
95{
96 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
97 match self {
98 Self::PinBlock(error) => Display::fmt(error, f),
99 Self::Crypto(error) => Display::fmt(error, f),
100 }
101 }
102}
103
104impl<E> Error for PinBlockCryptoError<E>
105where
106 E: Error + 'static,
107{
108 fn source(&self) -> Option<&(dyn Error + 'static)> {
109 match self {
110 Self::PinBlock(error) => Some(error),
111 Self::Crypto(error) => Some(error),
112 }
113 }
114}
115
116impl<E> From<PinBlockError> for PinBlockCryptoError<E> {
117 fn from(error: PinBlockError) -> Self {
118 Self::PinBlock(error)
119 }
120}