use thiserror::Error;
pub type TptpResult<T> = std::result::Result<T, TptpError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TptpErrorCode {
ShapeError,
TypeError,
CompilationError,
VendorUnavailable,
DeviceError,
Unsupported,
InvalidArgument,
InternalError,
Timeout,
}
#[derive(Debug, Error)]
pub enum TptpError {
#[error("kernel compilation failed: {message}")]
CompilationError {
message: String,
kernel_name: Option<String>,
},
#[error("invalid input shape: {message}")]
ShapeError {
message: String,
expected: Option<String>,
got: Option<String>,
},
#[error("type mismatch: {message}")]
TypeError {
message: String,
expected: Option<String>,
got: Option<String>,
},
#[error("vendor library not available: {library}")]
VendorUnavailable {
library: String,
reason: Option<String>,
},
#[error("device error: {message}")]
DeviceError { message: String, code: Option<u32> },
#[error("unsupported operation: {message}")]
Unsupported { message: String },
#[error("invalid argument: {field}: {message}")]
InvalidArgument { field: String, message: String },
#[error("internal error: {message}")]
InternalError { message: String },
#[error("operation timed out after {duration_ms}ms")]
Timeout { duration_ms: u64 },
#[error("TPTIR C API error: {message}")]
FfiError { message: String, status_code: i32 },
}
impl TptpError {
pub fn code(&self) -> TptpErrorCode {
match self {
TptpError::CompilationError { .. } => TptpErrorCode::CompilationError,
TptpError::ShapeError { .. } => TptpErrorCode::ShapeError,
TptpError::TypeError { .. } => TptpErrorCode::TypeError,
TptpError::VendorUnavailable { .. } => TptpErrorCode::VendorUnavailable,
TptpError::DeviceError { .. } => TptpErrorCode::DeviceError,
TptpError::Unsupported { .. } => TptpErrorCode::Unsupported,
TptpError::InvalidArgument { .. } => TptpErrorCode::InvalidArgument,
TptpError::InternalError { .. } => TptpErrorCode::InternalError,
TptpError::Timeout { .. } => TptpErrorCode::Timeout,
TptpError::FfiError { .. } => TptpErrorCode::CompilationError,
}
}
pub fn is_recoverable(&self) -> bool {
matches!(
self.code(),
TptpErrorCode::Timeout | TptpErrorCode::DeviceError
)
}
pub fn compilation(message: impl Into<String>) -> Self {
TptpError::CompilationError {
message: message.into(),
kernel_name: None,
}
}
pub fn shape_error(message: impl Into<String>) -> Self {
TptpError::ShapeError {
message: message.into(),
expected: None,
got: None,
}
}
pub fn type_error(message: impl Into<String>) -> Self {
TptpError::TypeError {
message: message.into(),
expected: None,
got: None,
}
}
pub fn vendor_unavailable(library: impl Into<String>) -> Self {
TptpError::VendorUnavailable {
library: library.into(),
reason: None,
}
}
pub fn unsupported(message: impl Into<String>) -> Self {
TptpError::Unsupported {
message: message.into(),
}
}
pub fn internal(message: impl Into<String>) -> Self {
TptpError::InternalError {
message: message.into(),
}
}
pub fn device_error(message: impl Into<String>) -> Self {
TptpError::DeviceError {
message: message.into(),
code: None,
}
}
pub fn invalid_argument(field: impl Into<String>, message: impl Into<String>) -> Self {
TptpError::InvalidArgument {
field: field.into(),
message: message.into(),
}
}
pub fn timeout(duration_ms: u64) -> Self {
TptpError::Timeout { duration_ms }
}
}
pub trait TptpResultExt<T> {
fn with_context(self, context: &str) -> TptpResult<T>;
fn with_kernel(self, name: &str) -> TptpResult<T>;
}
impl<T> TptpResultExt<T> for TptpResult<T> {
fn with_context(self, context: &str) -> TptpResult<T> {
self.map_err(|e| TptpError::InternalError {
message: format!("{}: {}", context, e),
})
}
fn with_kernel(self, name: &str) -> TptpResult<T> {
self.map_err(|e| match e {
TptpError::CompilationError { message, .. } => TptpError::CompilationError {
message,
kernel_name: Some(name.to_string()),
},
other => other,
})
}
}