64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
//! Error types for portfolio optimization
|
|
|
|
use thiserror::Error;
|
|
|
|
/// Portfolio optimization error type
|
|
#[derive(Debug, Error)]
|
|
pub enum PortfolioError {
|
|
/// Invalid configuration
|
|
#[error("Invalid configuration: {0}")]
|
|
InvalidConfig(String),
|
|
|
|
/// Optimization failed
|
|
#[error("Optimization failed: {0}")]
|
|
OptimizationFailed(String),
|
|
|
|
/// Covariance computation error
|
|
#[error("Covariance computation error: {0}")]
|
|
CovarianceError(String),
|
|
|
|
/// Constraint violation
|
|
#[error("Constraint violation: {0}")]
|
|
ConstraintViolation(String),
|
|
|
|
/// Insufficient data
|
|
#[error("Insufficient data: {0}")]
|
|
InsufficientData(String),
|
|
|
|
/// Numerical error
|
|
#[error("Numerical error: {0}")]
|
|
NumericalError(String),
|
|
}
|
|
|
|
/// Result type for portfolio operations
|
|
pub type Result<T> = std::result::Result<T, PortfolioError>;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_error_display() {
|
|
let err = PortfolioError::InvalidConfig("Missing assets".to_string());
|
|
assert_eq!(err.to_string(), "Invalid configuration: Missing assets");
|
|
}
|
|
|
|
#[test]
|
|
fn test_optimization_failed_error() {
|
|
let err = PortfolioError::OptimizationFailed("No solution found".to_string());
|
|
assert_eq!(err.to_string(), "Optimization failed: No solution found");
|
|
}
|
|
|
|
#[test]
|
|
fn test_result_type() {
|
|
let ok_result: Result<i32> = Ok(42);
|
|
assert!(ok_result.is_ok());
|
|
assert_eq!(ok_result.unwrap(), 42);
|
|
|
|
let err_result: Result<i32> = Err(PortfolioError::NumericalError(
|
|
"Division by zero".to_string(),
|
|
));
|
|
assert!(err_result.is_err());
|
|
}
|
|
}
|