Files
rustytorch/demos/rtx-segmentation-demo/src/error.rs
T
2026-03-04 00:08:42 +00:00

116 lines
3.3 KiB
Rust

// Error types for segmentation operations
use thiserror::Error;
/// Errors that can occur during segmentation
#[derive(Debug, Error, Clone, PartialEq)]
pub enum SegmentationError {
/// Invalid input dimensions
#[error("Invalid input dimensions: {0}")]
InvalidDimensions(String),
/// Invalid model configuration
#[error("Invalid model configuration: {0}")]
InvalidConfig(String),
/// Model not found
#[error("Model not found: {0}")]
ModelNotFound(String),
/// Inference failed
#[error("Inference failed: {0}")]
InferenceFailed(String),
/// Invalid image data
#[error("Invalid image data: {0}")]
InvalidImageData(String),
/// Colormap error
#[error("Colormap error: {0}")]
ColormapError(String),
}
pub type Result<T> = std::result::Result<T, SegmentationError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_invalid_dimensions_error() {
let err = SegmentationError::InvalidDimensions("width is zero".to_string());
assert_eq!(err.to_string(), "Invalid input dimensions: width is zero");
}
#[test]
fn test_invalid_config_error() {
let err = SegmentationError::InvalidConfig("num_classes is zero".to_string());
assert_eq!(
err.to_string(),
"Invalid model configuration: num_classes is zero"
);
}
#[test]
fn test_model_not_found_error() {
let err = SegmentationError::ModelNotFound("UnknownModel".to_string());
assert_eq!(err.to_string(), "Model not found: UnknownModel");
}
#[test]
fn test_inference_failed_error() {
let err = SegmentationError::InferenceFailed("out of memory".to_string());
assert_eq!(err.to_string(), "Inference failed: out of memory");
}
#[test]
fn test_invalid_image_data_error() {
let err = SegmentationError::InvalidImageData("wrong size".to_string());
assert_eq!(err.to_string(), "Invalid image data: wrong size");
}
#[test]
fn test_colormap_error() {
let err = SegmentationError::ColormapError("invalid class index".to_string());
assert_eq!(err.to_string(), "Colormap error: invalid class index");
}
#[test]
fn test_error_clone() {
let err1 = SegmentationError::InvalidDimensions("test".to_string());
let err2 = err1.clone();
assert_eq!(err1, err2);
}
#[test]
fn test_error_debug() {
let err = SegmentationError::InferenceFailed("test".to_string());
let debug_str = format!("{:?}", err);
assert!(debug_str.contains("InferenceFailed"));
assert!(debug_str.contains("test"));
}
#[test]
fn test_result_type_ok() {
let result: Result<i32> = Ok(42);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 42);
}
#[test]
fn test_result_type_err() {
let result: Result<i32> = Err(SegmentationError::ModelNotFound("test".to_string()));
assert!(result.is_err());
}
#[test]
fn test_error_equality() {
let err1 = SegmentationError::InvalidDimensions("test".to_string());
let err2 = SegmentationError::InvalidDimensions("test".to_string());
let err3 = SegmentationError::InvalidDimensions("other".to_string());
assert_eq!(err1, err2);
assert_ne!(err1, err3);
}
}