Skip to main content

crypto_primes/
error.rs

1use core::fmt;
2
3use crate::Flavor;
4
5/// Errors returned by the crate's API.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Error {
8    /// The requested bit length of the candidate is larger than the maximum size of the target integer type.
9    BitLengthTooLarge {
10        /// The requested bit length.
11        bit_length: u32,
12        /// The maximum size of the integer type.
13        bits_precision: u32,
14    },
15    /// The requested bit length is too small to fit a prime of the chosen [`Flavor`](`crate::Flavor`).
16    BitLengthTooSmall {
17        /// The requested bit length.
18        bit_length: u32,
19        /// The requested flavor.
20        flavor: Flavor,
21    },
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
26        match self {
27            Error::BitLengthTooLarge {
28                bit_length,
29                bits_precision,
30            } => write!(
31                f,
32                concat![
33                    "The requested bit length of the candidate ({}) ",
34                    "is larger than the maximum size of the target integer type ({})."
35                ],
36                bit_length, bits_precision
37            ),
38            Error::BitLengthTooSmall { bit_length, flavor } => write!(
39                f,
40                concat![
41                    "The requested bit length of the candidate ({}) ",
42                    "is too small to fit a prime of the flavor {:?}",
43                ],
44                bit_length, flavor
45            ),
46        }
47    }
48}