Skip to main content

use_combinatorics/
error.rs

1use core::fmt;
2use std::error::Error;
3
4/// Errors returned by checked combinatorics helpers.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum CombinatoricsError {
7    /// `k` must not exceed `n` in selection-style helpers.
8    KExceedsN {
9        /// The size of the source set.
10        n: u64,
11        /// The requested selection size.
12        k: u64,
13    },
14    /// The factorial result overflowed `u128`.
15    FactorialOverflow(u64),
16    /// The permutation result overflowed `u128`.
17    PermutationOverflow {
18        /// The size of the source set.
19        n: u64,
20        /// The requested ordered selection size.
21        k: u64,
22    },
23    /// The combination result overflowed `u128`.
24    CombinationOverflow {
25        /// The size of the source set.
26        n: u64,
27        /// The requested unordered selection size.
28        k: u64,
29    },
30}
31
32impl fmt::Display for CombinatoricsError {
33    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::KExceedsN { n, k } => {
36                write!(
37                    formatter,
38                    "k must be less than or equal to n, got k={k}, n={n}"
39                )
40            },
41            Self::FactorialOverflow(n) => {
42                write!(formatter, "factorial overflowed u128 for n={n}")
43            },
44            Self::PermutationOverflow { n, k } => {
45                write!(formatter, "permutations overflowed u128 for n={n}, k={k}")
46            },
47            Self::CombinationOverflow { n, k } => {
48                write!(formatter, "combinations overflowed u128 for n={n}, k={k}")
49            },
50        }
51    }
52}
53
54impl Error for CombinatoricsError {}