Files
rustytorch/demos/pinn-benchmark-shared/src/error.rs
T
2026-03-04 00:08:42 +00:00

76 lines
2.0 KiB
Rust

//! Error types for PINN benchmark
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// Result type for PINN benchmark operations
pub type Result<T> = std::result::Result<T, PINNBenchmarkError>;
/// Error types for PINN benchmark demo
#[derive(Debug, Clone, Error, Serialize, Deserialize)]
pub enum PINNBenchmarkError {
/// Configuration validation error
#[error("Configuration error: {0}")]
Configuration(String),
/// Training error
#[error("Training error: {0}")]
Training(String),
/// Inference error
#[error("Inference error: {0}")]
Inference(String),
/// Device error
#[error("Device error: {0}")]
Device(String),
/// Computation error
#[error("Computation error: {0}")]
Computation(String),
/// Invalid state error
#[error("Invalid state: {0}")]
InvalidState(String),
/// Serialization error
#[error("Serialization error: {0}")]
Serialization(String),
/// Internal error
#[error("Internal error: {0}")]
Internal(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = PINNBenchmarkError::Configuration("Invalid learning rate".to_string());
let display = format!("{err}");
assert!(display.contains("Configuration error"));
assert!(display.contains("Invalid learning rate"));
}
#[test]
fn test_error_serde() {
let err = PINNBenchmarkError::Training("Loss diverged".to_string());
let json = serde_json::to_string(&err).expect("Failed to serialize");
let deserialized: PINNBenchmarkError =
serde_json::from_str(&json).expect("Failed to deserialize");
assert_eq!(format!("{err}"), format!("{deserialized}"));
}
#[test]
fn test_result_type() {
let ok_result: Result<i32> = Ok(42);
assert!(ok_result.is_ok());
let err_result: Result<i32> = Err(PINNBenchmarkError::Internal("Test".to_string()));
assert!(err_result.is_err());
}
}