//! Error types for inference profiler. use serde::{Deserialize, Serialize}; use thiserror::Error; /// Errors that can occur during inference profiling. #[derive(Debug, Clone, PartialEq, Error, Serialize, Deserialize)] pub enum ProfilerError { /// Configuration validation failed #[error("configuration error: {0}")] ConfigError(String), /// Model initialization failed #[error("model initialization failed: {0}")] ModelInitError(String), /// Device is not available #[error("device unavailable: {0}")] DeviceUnavailable(String), /// Profiling measurement failed #[error("measurement failed: {0}")] MeasurementError(String), /// Invalid input data #[error("invalid input: {0}")] InvalidInput(String), /// Serialization/deserialization error #[error("serialization error: {0}")] SerializationError(String), /// Internal error #[error("internal error: {0}")] InternalError(String), } #[cfg(test)] mod tests { use super::*; #[test] fn test_profiler_error_display() { let err = ProfilerError::ConfigError("invalid batch size".to_string()); assert_eq!(err.to_string(), "configuration error: invalid batch size"); } #[test] fn test_profiler_error_variants() { let errors = vec![ ProfilerError::ConfigError("test".to_string()), ProfilerError::ModelInitError("test".to_string()), ProfilerError::DeviceUnavailable("test".to_string()), ProfilerError::MeasurementError("test".to_string()), ProfilerError::InvalidInput("test".to_string()), ProfilerError::SerializationError("test".to_string()), ProfilerError::InternalError("test".to_string()), ]; for err in errors { assert!(!err.to_string().is_empty()); } } #[test] fn test_profiler_error_serialization() { let err = ProfilerError::DeviceUnavailable("CUDA not found".to_string()); let json = serde_json::to_string(&err).expect("serialization failed"); assert!(json.contains("DeviceUnavailable")); } #[test] fn test_profiler_error_deserialization() { let json = r#"{"MeasurementError":"out of memory"}"#; let err: ProfilerError = serde_json::from_str(json).expect("deserialization failed"); match err { ProfilerError::MeasurementError(msg) => assert_eq!(msg, "out of memory"), _ => panic!("wrong error variant"), } } #[test] fn test_profiler_error_roundtrip() { let original = ProfilerError::InvalidInput("negative value".to_string()); let json = serde_json::to_string(&original).expect("serialization failed"); let decoded: ProfilerError = serde_json::from_str(&json).expect("deserialization failed"); assert_eq!(original, decoded); } #[test] fn test_profiler_error_equality() { let err1 = ProfilerError::ConfigError("test".to_string()); let err2 = ProfilerError::ConfigError("test".to_string()); let err3 = ProfilerError::ConfigError("different".to_string()); assert_eq!(err1, err2); assert_ne!(err1, err3); } #[test] fn test_profiler_error_clone() { let err1 = ProfilerError::InternalError("test".to_string()); let err2 = err1.clone(); assert_eq!(err1, err2); } }