use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Failed to create topology refiner")]
CreateTopologyRefinerFailed,
#[error("Failed to create stencil table")]
StencilTableCreation,
#[error("Failed to create patch table")]
PatchTableCreation,
#[error("Stencil evaluation failed")]
EvalStencilsFailed,
#[error("Invalid topology descriptor: {0}")]
InvalidTopology(String),
#[error("Invalid patch configuration: {0}")]
InvalidPatch(String),
#[error("Index {index} out of bounds (max: {max})")]
IndexOutOfBounds { index: usize, max: usize },
#[error("Invalid buffer size: expected {expected}, got {actual}")]
InvalidBufferSize { expected: usize, actual: usize },
#[error("OpenSubdiv FFI error: {0}")]
Ffi(String),
#[error("Unexpected null pointer")]
NullPointer,
#[error("Feature not available: {0}")]
FeatureNotAvailable(String),
#[cfg(any(feature = "cuda", feature = "opencl", feature = "metal"))]
#[error("GPU backend error: {0}")]
GpuBackend(String),
#[cfg(feature = "monstertruck")]
#[error("Monstertruck error: {0}")]
Monstertruck(#[from] crate::monstertruck::MonstertruckError),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Format error: {0}")]
Format(#[from] std::fmt::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn from_null_ptr<T>(ptr: *const T, context: &str) -> Self {
if ptr.is_null() {
Error::NullPointer
} else {
Error::Ffi(format!("Unexpected error in {}", context))
}
}
pub fn check_null_ptr<T>(ptr: *const T, context: &str) -> Result<()> {
if ptr.is_null() {
Err(Error::Ffi(format!("Null pointer in {}", context)))
} else {
Ok(())
}
}
}