amplify_num/
error.rs

1// Rust language amplification library providing multiple generic trait
2// implementations, type wrappers, derive macros and other language enhancements
3//
4// Written in 2014 by
5//     Andrew Poelstra <apoelstra@wpsoftware.net>
6// Updated in 2020-2024 by
7//     Dr. Maxim Orlovsky <orlovsky@ubideco.org>
8//
9// To the extent possible under law, the author(s) have dedicated all
10// copyright and related and neighboring rights to this software to
11// the public domain worldwide. This software is distributed without
12// any warranty.
13//
14// You should have received a copy of the MIT License
15// along with this software.
16// If not, see <https://opensource.org/licenses/MIT>.
17
18#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
19/// Error indicating that a value does not fit integer dimension
20pub struct OverflowError<T = usize> {
21    /// Integer bit size
22    pub max: T,
23    /// Value that overflows
24    pub value: T,
25}
26
27impl core::fmt::Display for OverflowError {
28    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29        write!(
30            f,
31            "Unable to construct bit-sized integer from a value `{}` overflowing max value `{}`",
32            self.value, self.max
33        )
34    }
35}
36
37#[cfg(feature = "std")]
38impl std::error::Error for OverflowError {}
39
40#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
41pub enum DivError {
42    ZeroDiv,
43    Overflow,
44}
45
46impl core::fmt::Display for DivError {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        match *self {
49            DivError::ZeroDiv => write!(f, "division by zero"),
50            DivError::Overflow => write!(f, "division with overflow"),
51        }
52    }
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for DivError {}
57
58#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
59pub enum PositDecodeError {
60    Zero,
61    NaR,
62}
63
64impl core::fmt::Display for PositDecodeError {
65    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66        match *self {
67            PositDecodeError::Zero => write!(f, "exception value zero"),
68            PositDecodeError::NaR => write!(f, "exception value NaR"),
69        }
70    }
71}
72
73#[cfg(feature = "std")]
74impl std::error::Error for PositDecodeError {}
75
76#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
77/// Invalid slice length
78pub struct ParseLengthError {
79    /// The length of the slice de-facto
80    pub actual: usize,
81    /// The required length of the slice
82    pub expected: usize,
83}
84
85impl core::fmt::Display for ParseLengthError {
86    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
87        write!(f, "Invalid length: got {}, expected {}", self.actual, self.expected)
88    }
89}
90#[cfg(feature = "std")]
91impl std::error::Error for ParseLengthError {}