1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Error types.

use core::fmt::{self, Display, Formatter};

/// Number out of bounds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OutOfBoundsError;

impl Display for OutOfBoundsError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str("number out of bounds")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for OutOfBoundsError {}

/// Error parsing a number.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// No digits in the string.
    NoDigits,
    /// Invalid digit for a given radix.
    InvalidDigit,
    /// The radix is not supported.
    UnsupportedRadix,
}

impl Display for ParseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            ParseError::NoDigits => f.write_str("no digits"),
            ParseError::InvalidDigit => f.write_str("invalid digit"),
            ParseError::UnsupportedRadix => f.write_str("unsupported radix"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseError {}

/// Panics when division by 0 is happening
pub(crate) const fn panic_divide_by_0() -> ! {
    panic!("divisor must not be 0")
}

/// Panics when the range input for the random generator in empty
#[cfg(feature = "rand")]
pub(crate) const fn panic_empty_range() -> ! {
    panic!("empty range for random generation")
}

/// Panics when try to allocate memory with size exceeding usize range
pub(crate) const fn panic_allocate_too_much() -> ! {
    panic!("try to allocate too much memory")
}

/// Panics when allocation failed
pub(crate) const fn panic_out_of_memory() -> ! {
    panic!("out of memory")
}

/// Panics when the `UBig` result is negative
pub(crate) const fn panic_negative_ubig() -> ! {
    panic!("UBig result must not be negative")
}

/// Panics when trying to do operations on `Modulo` values from different rings.
pub(crate) const fn panic_different_rings() -> ! {
    panic!("Modulo values from different rings")
}

/// Panics when the radix is not supported
pub(crate) fn panic_invalid_radix(radix: u32) -> ! {
    panic!("invalid radix: {}, only radix 2-36 are supported", radix);
}

/// Panics when the base is 0 or 1 in logarithm
pub(crate) fn panic_invalid_log_oprand() -> ! {
    panic!("logarithm is not defined for 0, base 0 and base 1!");
}