use thiserror::Error;
#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MathError {
#[error("Division by zero")]
DivisionByZero,
#[error("Arithmetic overflow")]
Overflow,
#[error("Arithmetic underflow")]
Underflow,
#[error("Value {value} is out of bounds [{min}, {max}]")]
OutOfBounds { value: i128, min: i128, max: i128 },
}
impl MathError {
pub fn out_of_bounds_u64(value: u64, min: u64, max: u64) -> Self {
Self::OutOfBounds {
value: value as i128,
min: min as i128,
max: max as i128,
}
}
pub fn out_of_bounds_i64(value: i64, min: i64, max: i64) -> Self {
Self::OutOfBounds {
value: value as i128,
min: min as i128,
max: max as i128,
}
}
pub fn out_of_bounds_i128(value: i128, min: i128, max: i128) -> Self {
Self::OutOfBounds { value, min, max }
}
pub fn out_of_bounds_u32(value: u32, min: u32, max: u32) -> Self {
Self::OutOfBounds {
value: value as i128,
min: min as i128,
max: max as i128,
}
}
pub fn out_of_bounds_i32(value: i32, min: i32, max: i32) -> Self {
Self::OutOfBounds {
value: value as i128,
min: min as i128,
max: max as i128,
}
}
}