use thiserror::Error;
#[derive(Error, Debug)]
pub enum OperationErrorKind {
#[error("Operation '{operation}' is not supported for strategy '{reason}'")]
NotSupported {
operation: String,
reason: String,
},
#[error("Invalid parameters for operation '{operation}': {reason}")]
InvalidParameters {
operation: String,
reason: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_operation_error_not_supported_display() {
let error = OperationErrorKind::NotSupported {
operation: "calculate_profit".to_string(),
reason: "IronCondor".to_string(),
};
assert_eq!(
error.to_string(),
"Operation 'calculate_profit' is not supported for strategy 'IronCondor'"
);
}
#[test]
fn test_operation_error_invalid_parameters_display() {
let error = OperationErrorKind::InvalidParameters {
operation: "validate_strikes".to_string(),
reason: "Strike prices must be positive".to_string(),
};
assert_eq!(
error.to_string(),
"Invalid parameters for operation 'validate_strikes': Strike prices must be positive"
);
}
#[test]
fn test_operation_error_debug() {
let error = OperationErrorKind::NotSupported {
operation: "calculate_profit".to_string(),
reason: "IronCondor".to_string(),
};
assert_eq!(
format!("{error:?}"),
"NotSupported { operation: \"calculate_profit\", reason: \"IronCondor\" }"
);
}
#[test]
fn test_operation_error_as_error() {
let error = OperationErrorKind::InvalidParameters {
operation: "validate_strikes".to_string(),
reason: "Strike prices must be positive".to_string(),
};
let error_ref: &dyn std::error::Error = &error;
assert_eq!(
error_ref.to_string(),
"Invalid parameters for operation 'validate_strikes': Strike prices must be positive"
);
}
#[test]
fn test_operation_error_kinds_distinct() {
let error1 = OperationErrorKind::NotSupported {
operation: "op".to_string(),
reason: "strat".to_string(),
};
let error2 = OperationErrorKind::InvalidParameters {
operation: "op".to_string(),
reason: "err".to_string(),
};
assert_ne!(format!("{error1:?}"), format!("{:?}", error2));
}
#[test]
fn test_operation_error_empty_strings() {
let error = OperationErrorKind::NotSupported {
operation: "".to_string(),
reason: "".to_string(),
};
assert_eq!(
error.to_string(),
"Operation '' is not supported for strategy ''"
);
}
}