1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::error::{DecimalError, GreeksError, OptionsError, PositionError};
use positive::PositiveError;
use thiserror::Error;
/// Error type for option pricing operations.
///
/// This enum represents the various errors that can occur during option pricing,
/// providing domain-specific error handling for different pricing scenarios.
#[derive(Error, Debug)]
pub enum PricingError {
/// Error from a specific pricing method (e.g., Black-Scholes, Binomial).
#[error("Pricing method '{method}' failed: {reason}")]
MethodError {
/// Name of the pricing method that failed
method: String,
/// Detailed reason for the failure
reason: String,
},
/// Error during Monte Carlo simulation.
#[error("Pricing simulation failed: {reason}")]
SimulationError {
/// Detailed reason for the simulation failure
reason: String,
},
/// Error due to invalid pricing engine configuration.
#[error("Invalid pricing engine: {reason}")]
InvalidEngine {
/// Detailed reason for the invalid engine
reason: String,
},
/// Error from Greeks calculations.
#[error(transparent)]
Greeks(#[from] GreeksError),
/// Error from Options operations.
#[error(transparent)]
Options(#[from] OptionsError),
/// Error from Position operations.
#[error(transparent)]
Position(#[from] PositionError),
/// Error from Decimal operations.
#[error(transparent)]
Decimal(#[from] DecimalError),
/// Generic pricing error.
#[error("Pricing error: {reason}")]
OtherError {
/// Detailed reason for the error
reason: String,
},
/// Error from Positive operations.
#[error(transparent)]
Positive(#[from] PositiveError),
/// Error for unsupported option types.
#[error("Unsupported option type '{option_type}' for pricing method '{method}'")]
UnsupportedOptionType {
/// The option type that is not supported
option_type: String,
/// The pricing method that does not support this option type
method: String,
},
}
impl PricingError {
/// Creates a new `MethodError` variant.
///
/// # Arguments
/// * `method` - Name of the pricing method that failed
/// * `reason` - Detailed reason for the failure
pub fn method_error(method: &str, reason: &str) -> Self {
PricingError::MethodError {
method: method.to_string(),
reason: reason.to_string(),
}
}
/// Creates a new `SimulationError` variant.
///
/// # Arguments
/// * `reason` - Detailed reason for the simulation failure
pub fn simulation_error(reason: &str) -> Self {
PricingError::SimulationError {
reason: reason.to_string(),
}
}
/// Creates a new `InvalidEngine` variant.
///
/// # Arguments
/// * `reason` - Detailed reason for the invalid engine
pub fn invalid_engine(reason: &str) -> Self {
PricingError::InvalidEngine {
reason: reason.to_string(),
}
}
/// Creates a new `OtherError` variant.
///
/// # Arguments
/// * `reason` - Detailed reason for the error
pub fn other(reason: &str) -> Self {
PricingError::OtherError {
reason: reason.to_string(),
}
}
/// Creates a new `UnsupportedOptionType` variant.
///
/// # Arguments
/// * `option_type` - The option type that is not supported
/// * `method` - The pricing method that does not support this option type
pub fn unsupported_option_type(option_type: &str, method: &str) -> Self {
PricingError::UnsupportedOptionType {
option_type: option_type.to_string(),
method: method.to_string(),
}
}
}
impl From<Box<dyn std::error::Error>> for PricingError {
fn from(err: Box<dyn std::error::Error>) -> Self {
PricingError::OtherError {
reason: err.to_string(),
}
}
}
impl From<String> for PricingError {
fn from(s: String) -> Self {
PricingError::OtherError { reason: s }
}
}
impl From<expiration_date::error::ExpirationDateError> for PricingError {
fn from(err: expiration_date::error::ExpirationDateError) -> Self {
PricingError::OtherError {
reason: err.to_string(),
}
}
}
impl From<&str> for PricingError {
fn from(s: &str) -> Self {
PricingError::OtherError {
reason: s.to_string(),
}
}
}
/// Type alias for Results that may return a `PricingError`.
///
/// This is a convenience type for functions that return pricing results.
pub type PricingResult<T> = Result<T, PricingError>;