| 1 | //! Error type which can be returned by some [`OptionOperations`]. |
| 2 | |
| 3 | #[cfg (feature = "std" )] |
| 4 | use std::{error, fmt}; |
| 5 | |
| 6 | // Required for doc |
| 7 | #[allow (unused)] |
| 8 | use crate::OptionOperations; |
| 9 | |
| 10 | /// Error type which can be returned by some [`OptionOperations`]. |
| 11 | #[derive (Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 12 | pub enum Error { |
| 13 | /// Division by zero attempted with an [`OptionOperations`]. |
| 14 | DivisionByZero, |
| 15 | /// An [`OptionOperations`] overflowed. |
| 16 | Overflow, |
| 17 | } |
| 18 | |
| 19 | impl Error { |
| 20 | /// Returns `true` if this [`Error`] results from a division by zero. |
| 21 | #[must_use ] |
| 22 | pub fn is_division_by_zero(&self) -> bool { |
| 23 | matches!(self, Error::DivisionByZero) |
| 24 | } |
| 25 | |
| 26 | /// Returns `true` if this [`Error`] results from an overflow. |
| 27 | #[must_use ] |
| 28 | pub fn is_overflow(&self) -> bool { |
| 29 | matches!(self, Error::Overflow) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[cfg (feature = "std" )] |
| 34 | impl fmt::Display for Error { |
| 35 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 36 | match self { |
| 37 | Error::DivisionByZero => f.write_str(data:"An Option Operation overflowed" ), |
| 38 | Error::Overflow => f.write_str(data:"Division by zero attempted with an Option Operation" ), |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[cfg (feature = "std" )] |
| 44 | impl error::Error for Error {} |
| 45 | |