ext_ops/
error.rs

1/*
2 * Copyright (c) 2023 Martin Mills <daggerbot@gmail.com>
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9use core::fmt::{Display, Formatter};
10
11/// Error raised when a checked arithmetic operation fails.
12#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
13pub enum ArithmeticError {
14    Undefined,
15    Underflow,
16    Overflow,
17}
18
19impl ArithmeticError {
20    fn brief(self) -> &'static str {
21        match self {
22            ArithmeticError::Undefined => Undefined::BRIEF,
23            ArithmeticError::Underflow => Underflow::BRIEF,
24            ArithmeticError::Overflow => Overflow::BRIEF,
25        }
26    }
27}
28
29impl Display for ArithmeticError {
30    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
31        f.write_str(self.brief())
32    }
33}
34
35#[cfg(feature = "std")]
36impl std::error::Error for ArithmeticError {
37    fn description(&self) -> &str { self.brief() }
38}
39
40/// Error raised when the result of a checked arithmetic operation is too high to be represented by
41/// the destination type.
42#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
43pub struct Overflow;
44
45impl Overflow {
46    const BRIEF: &'static str = "arithmetic overflow";
47}
48
49impl Display for Overflow {
50    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
51        f.write_str(Overflow::BRIEF)
52    }
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for Overflow {
57    fn description(&self) -> &str { Overflow::BRIEF }
58}
59
60/// Error raised when the result of a checked arithmetic operation is too high or too low to be
61/// represented by the destination type.
62#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
63pub enum RangeError {
64    Underflow,
65    Overflow,
66}
67
68impl RangeError {
69    fn brief(self) -> &'static str {
70        match self {
71            RangeError::Underflow => Underflow::BRIEF,
72            RangeError::Overflow => Overflow::BRIEF,
73        }
74    }
75}
76
77impl Display for RangeError {
78    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
79        f.write_str(self.brief())
80    }
81}
82
83#[cfg(feature = "std")]
84impl std::error::Error for RangeError {
85    fn description(&self) -> &str { self.brief() }
86}
87
88/// Error raised when the result of a checked arithmetic operation is undefined, indeterminate, or
89/// not a number.
90#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
91pub struct Undefined;
92
93impl Undefined {
94    const BRIEF: &'static str = "arithmetic result undefined";
95}
96
97impl Display for Undefined {
98    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
99        f.write_str(Undefined::BRIEF)
100    }
101}
102
103#[cfg(feature = "std")]
104impl std::error::Error for Undefined {
105    fn description(&self) -> &str { Undefined::BRIEF }
106}
107
108/// Error raised when the result of a checked arithmetic operation is too low to be represented by
109/// the destination type.
110#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
111pub struct Underflow;
112
113impl Underflow {
114    const BRIEF: &'static str = "arithmetic underflow";
115}
116
117impl Display for Underflow {
118    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
119        f.write_str(Underflow::BRIEF)
120    }
121}
122
123#[cfg(feature = "std")]
124impl std::error::Error for Underflow {
125    fn description(&self) -> &str { Underflow::BRIEF }
126}