Skip to main content

use_rational/
error.rs

1use core::fmt;
2use std::error::Error;
3
4/// Errors returned by validated rational-number helpers.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum RationalError {
7    /// A rational denominator must not be zero.
8    ZeroDenominator,
9    /// Division by a zero-valued rational is undefined.
10    DivisionByZero,
11    /// Sign normalization could not be represented in the current integer type.
12    NormalizationOverflow,
13    /// Exact arithmetic overflowed the current integer representation.
14    ArithmeticOverflow {
15        /// The checked operation that overflowed.
16        operation: &'static str,
17    },
18}
19
20impl fmt::Display for RationalError {
21    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::ZeroDenominator => write!(formatter, "denominator must not be zero"),
24            Self::DivisionByZero => write!(formatter, "division by zero-valued rational"),
25            Self::NormalizationOverflow => {
26                write!(
27                    formatter,
28                    "rational normalization overflowed the current representation"
29                )
30            },
31            Self::ArithmeticOverflow { operation } => {
32                write!(
33                    formatter,
34                    "rational {operation} overflowed the current representation"
35                )
36            },
37        }
38    }
39}
40
41impl Error for RationalError {}