pub enum DecimalError {
InvalidValue {
value: f64,
reason: String,
},
ArithmeticError {
operation: String,
reason: String,
},
ConversionError {
from_type: String,
to_type: String,
reason: String,
},
OutOfBounds {
value: f64,
min: f64,
max: f64,
},
InvalidPrecision {
precision: i32,
reason: String,
},
ExpirationDate(ExpirationDateError),
Overflow {
operation: &'static str,
lhs: Decimal,
rhs: Decimal,
},
}Expand description
§Decimal Error Management
Represents errors that can occur during decimal operations in financial calculations. This enum provides a structured way to handle various error conditions that may arise when working with decimal values, including validation, arithmetic operations, conversions, and precision issues.
§Use Cases
- Financial calculations requiring strict decimal precision
- Currency and monetary value operations
- Option pricing models where precision is critical
- Risk management calculations
§Error Propagation
These errors are typically wrapped in DecimalResult and propagated through
the application’s calculation pipeline.
§Variants
InvalidValue- Handles errors when a value cannot be represented as a valid decimalArithmeticError- Handles errors during mathematical operationsConversionError- Handles errors when converting between different decimal representationsOutOfBounds- Handles errors when a value exceeds defined limitsInvalidPrecision- Handles errors related to decimal precision settings
§Example Usage
use optionstratlib_core::error::DecimalError;
fn validate_decimal(value: f64) -> Result<(), DecimalError> {
if value.is_nan() {
return Err(DecimalError::InvalidValue {
value,
reason: "Value cannot be NaN".to_string(),
});
}
if value < 0.0 || value > 100.0 {
return Err(DecimalError::OutOfBounds {
value,
min: 0.0,
max: 100.0,
});
}
Ok(())
}Variants§
InvalidValue
Error when attempting to create a decimal from an invalid value
Occurs when a value cannot be properly represented as a decimal, such as when it’s NaN, infinity, or otherwise unsuitable for financial calculations.
Fields
ArithmeticError
Error when performing decimal arithmetic operations
Occurs during mathematical operations such as addition, subtraction, multiplication, or division when the operation cannot be completed correctly (e.g., division by zero, overflow).
Fields
ConversionError
Error when converting between decimal types
Occurs when a decimal value cannot be correctly converted from one representation to another, such as between different precision levels or between different decimal formats.
Fields
OutOfBounds
Error when a decimal value exceeds its bounds
Occurs when a decimal value falls outside of acceptable minimum or maximum values for a specific calculation context.
Fields
InvalidPrecision
Error when decimal precision is invalid
Occurs when an operation specifies or results in an invalid precision level that cannot be properly handled.
Fields
ExpirationDate(ExpirationDateError)
Expiration-date conversion error surfaced during decimal operations.
Overflow
Error when a Decimal arithmetic operation overflows the representable range.
Emitted by the crate-private d_add / d_sub / d_mul / d_div helpers
in crate::model::decimal when the underlying
Decimal::checked_* call returns None. The operation tag is a
short, static identifier describing the call-site (for example
"pricing::black_scholes::discount_strike") so failures can be
traced back to their kernel without walking the call stack.
The operands are captured verbatim for post-mortem debugging and
are included in the formatted error output (see the #[error]
template below). This DecimalError variant is propagated across
domain modules through the existing #[from] cascade, so the
operand values will appear in any log or wrapper-error string
produced downstream. Callers that need to redact them for
production logging should match on the Overflow variant
explicitly and reformat rather than rely on the default Display.
Implementations§
Source§impl DecimalError
Helper methods for creating common decimal errors
impl DecimalError
Helper methods for creating common decimal errors
This implementation provides convenient factory methods to create
standardized instances of DecimalError for common error scenarios
in decimal operations. These methods help maintain consistency in
error creation across the codebase and simplify the construction of
descriptive error instances.
§Example
use optionstratlib_core::error::DecimalError;
// Creating an invalid value error
let err = DecimalError::invalid_value(12.34, "Value exceeds maximum allowed");
// Creating an arithmetic error
let div_err = DecimalError::arithmetic_error("division", "Division by zero");Sourcepub fn invalid_value(value: f64, reason: &str) -> Self
pub fn invalid_value(value: f64, reason: &str) -> Self
Creates a new InvalidValue error
Used when a decimal value fails validation due to being outside accepted ranges or otherwise inappropriate for the context.
§Parameters
value- The problematic floating-point valuereason- Explanation of why the value is invalid
§Returns
A new DecimalError::InvalidValue instance
Sourcepub fn arithmetic_error(operation: &str, reason: &str) -> Self
pub fn arithmetic_error(operation: &str, reason: &str) -> Self
Creates a new ArithmeticError error
Used when a mathematical operation on decimal values fails, such as division by zero, overflow, or underflow.
§Parameters
operation- The name of the operation that failed (e.g., “addition”, “division”)reason- Explanation of why the operation failed
§Returns
A new DecimalError::ArithmeticError instance
Sourcepub fn conversion_error(from_type: &str, to_type: &str, reason: &str) -> Self
pub fn conversion_error(from_type: &str, to_type: &str, reason: &str) -> Self
Creates a new ConversionError error
Used when conversion between decimal types or from/to other number types fails due to compatibility or range issues.
§Parameters
from_type- The source type being converted fromto_type- The destination type being converted toreason- Explanation of why the conversion failed
§Returns
A new DecimalError::ConversionError instance
Sourcepub fn out_of_bounds(value: f64, min: f64, max: f64) -> Self
pub fn out_of_bounds(value: f64, min: f64, max: f64) -> Self
Creates a new OutOfBounds error
Used when a decimal value falls outside of specified minimum and maximum bounds.
§Parameters
value- The out-of-bounds floating-point valuemin- The lower bound (inclusive) of the valid rangemax- The upper bound (inclusive) of the valid range
§Returns
A new DecimalError::OutOfBounds instance
Sourcepub fn invalid_precision(precision: i32, reason: &str) -> Self
pub fn invalid_precision(precision: i32, reason: &str) -> Self
Creates a new InvalidPrecision error
Used when a specified decimal precision is invalid, such as being negative, too large, or otherwise inappropriate for the context.
§Parameters
precision- The problematic precision valuereason- Explanation of why the precision is invalid
§Returns
A new DecimalError::InvalidPrecision instance
Sourcepub fn overflow(operation: &'static str, lhs: Decimal, rhs: Decimal) -> Self
pub fn overflow(operation: &'static str, lhs: Decimal, rhs: Decimal) -> Self
Creates a new Overflow error for a failed checked arithmetic operation.
Used by the crate-private d_add / d_sub / d_mul / d_div helpers
to surface an overflow of the underlying Decimal::checked_* call
with full operand context.
§Parameters
operation- Short static tag identifying the call-site (for example"pricing::black_scholes::discount_strike").lhs- Left-hand operand of the failed arithmetic.rhs- Right-hand operand of the failed arithmetic.
§Returns
A new DecimalError::Overflow instance.
Trait Implementations§
Source§impl Debug for DecimalError
impl Debug for DecimalError
Source§impl Display for DecimalError
impl Display for DecimalError
Source§impl Error for DecimalError
impl Error for DecimalError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()