Skip to main content

use_catalan/
error.rs

1use core::fmt;
2use std::error::Error;
3
4/// Errors returned by checked Catalan-family helpers.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum CatalanError {
7    /// The Fuss-Catalan order must be at least one.
8    ZeroOrder,
9    /// The Catalan number no longer fits in `u128`.
10    CatalanOverflow(u64),
11    /// The Fuss-Catalan number no longer fits in `u128`.
12    FussCatalanOverflow {
13        /// The requested Fuss-Catalan order.
14        order: u64,
15        /// The requested sequence index.
16        n: u64,
17    },
18}
19
20impl fmt::Display for CatalanError {
21    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::ZeroOrder => formatter.write_str("Fuss-Catalan order must be at least 1"),
24            Self::CatalanOverflow(n) => {
25                write!(formatter, "Catalan number overflowed u128 for n={n}")
26            },
27            Self::FussCatalanOverflow { order, n } => {
28                write!(
29                    formatter,
30                    "Fuss-Catalan number overflowed u128 for order={order}, n={n}"
31                )
32            },
33        }
34    }
35}
36
37impl Error for CatalanError {}