fermat_core/error.rs
1//! Error types for fermat-core arithmetic operations.
2
3/// All errors that can arise from fixed-point arithmetic in fermat-core.
4///
5/// Every fallible operation returns `Result<_, ArithmeticError>` instead of
6/// panicking, satisfying the sBPF requirement for panic-free on-chain programs.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum ArithmeticError {
9 /// The result exceeded the representable range of `i128`.
10 Overflow,
11 /// The result fell below the representable range of `i128`.
12 Underflow,
13 /// A division or modulo operation was attempted with a zero denominator.
14 DivisionByZero,
15 /// A scale value exceeded `MAX_SCALE` (28) was provided or computed.
16 ScaleExceeded,
17 /// The input could not be parsed or is otherwise malformed (e.g. bad string).
18 InvalidInput,
19}
20
21impl core::fmt::Display for ArithmeticError {
22 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23 match self {
24 ArithmeticError::Overflow => f.write_str("arithmetic overflow"),
25 ArithmeticError::Underflow => f.write_str("arithmetic underflow"),
26 ArithmeticError::DivisionByZero => f.write_str("division by zero"),
27 ArithmeticError::ScaleExceeded => f.write_str("scale exceeds maximum (28)"),
28 ArithmeticError::InvalidInput => f.write_str("invalid input"),
29 }
30 }
31}