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

100 lines
2.7 KiB
Rust

//! Error types for the hemodynamics shared library
//!
//! This module defines all error types used throughout the shared crate,
//! providing consistent error handling for geometry, physics, and IPC operations.
use thiserror::Error;
/// Result type alias using [`HemodynamicsError`]
pub type Result<T> = std::result::Result<T, HemodynamicsError>;
/// Comprehensive error type for hemodynamics operations
#[derive(Debug, Error, Clone, PartialEq)]
pub enum HemodynamicsError {
/// Invalid geometry parameter
#[error("Invalid geometry parameter: {0}")]
InvalidGeometry(String),
/// Invalid physics parameter
#[error("Invalid physics parameter: {0}")]
InvalidPhysics(String),
/// Invalid field data
#[error("Invalid field data: {0}")]
InvalidField(String),
/// Invalid configuration
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
/// Serialization error
#[error("Serialization error: {0}")]
Serialization(String),
/// IPC communication error
#[error("IPC error: {0}")]
IpcError(String),
/// Simulation not initialized
#[error("Simulation not initialized")]
NotInitialized,
/// Operation in progress
#[error("Operation in progress: {0}")]
OperationInProgress(String),
}
impl HemodynamicsError {
/// Creates a new invalid geometry error with the given message
#[must_use]
pub fn invalid_geometry(msg: impl Into<String>) -> Self {
Self::InvalidGeometry(msg.into())
}
/// Creates a new invalid physics error with the given message
#[must_use]
pub fn invalid_physics(msg: impl Into<String>) -> Self {
Self::InvalidPhysics(msg.into())
}
/// Creates a new invalid field error with the given message
#[must_use]
pub fn invalid_field(msg: impl Into<String>) -> Self {
Self::InvalidField(msg.into())
}
/// Creates a new invalid config error with the given message
#[must_use]
pub fn invalid_config(msg: impl Into<String>) -> Self {
Self::InvalidConfig(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = HemodynamicsError::invalid_geometry("radius must be positive");
assert_eq!(
err.to_string(),
"Invalid geometry parameter: radius must be positive"
);
}
#[test]
fn test_error_equality() {
let err1 = HemodynamicsError::invalid_geometry("test");
let err2 = HemodynamicsError::invalid_geometry("test");
assert_eq!(err1, err2);
}
#[test]
fn test_error_clone() {
let err = HemodynamicsError::InvalidPhysics("viscosity".into());
let cloned = err.clone();
assert_eq!(err, cloned);
}
}