Skip to main content

fpe/ff1/
error.rs

1use core::fmt;
2
3/// Error indicating that a radix was not in the supported range of values for FF1.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct InvalidRadix(pub(super) u32);
6
7impl fmt::Display for InvalidRadix {
8    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9        write!(f, "The radix {} is not in the range 2..=(1 << 16)", self.0)
10    }
11}
12
13#[cfg(feature = "std")]
14impl std::error::Error for InvalidRadix {}
15
16/// Errors that can occur while using FF1 for encryption or decryption.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum NumeralStringError {
19    /// The numeral string was not compatible with the configured radix.
20    InvalidForRadix(u32),
21    /// The numeral string was longer than the maximum allowed length for FF1.
22    TooLong {
23        /// The length of the numeral string.
24        ns_len: usize,
25        /// The maximum length allowed (in numerals) for a numeral string of its radix.
26        max_len: usize,
27    },
28    /// The numeral string was shorter than the minimum allowed length for FF1.
29    TooShort {
30        /// The length of the numeral string.
31        ns_len: usize,
32        /// The minimum length allowed (in numerals) for a numeral string of its radix.
33        min_len: usize,
34    },
35}
36
37impl fmt::Display for NumeralStringError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            NumeralStringError::InvalidForRadix(radix) => {
41                write!(f, "The given numeral string is invalid for radix {}", radix)
42            }
43            NumeralStringError::TooLong { ns_len, max_len } => write!(
44                f,
45                "The given numeral string is too long for FF1 ({} > {})",
46                ns_len, max_len,
47            ),
48            NumeralStringError::TooShort { ns_len, min_len } => write!(
49                f,
50                "The given numeral string is too short for FF1 ({} < {})",
51                ns_len, min_len,
52            ),
53        }
54    }
55}
56
57#[cfg(feature = "std")]
58impl std::error::Error for NumeralStringError {}