use thiserror::Error;
#[derive(Error, Debug)]
pub enum CrushError {
#[error("Plugin error: {0}")]
Plugin(#[from] PluginError),
#[error("Timeout error: {0}")]
Timeout(#[from] TimeoutError),
#[error("Validation error: {0}")]
Validation(#[from] ValidationError),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Operation cancelled")]
Cancelled,
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
#[error("Version mismatch: file was produced by engine {file_version}, current engine is {current_version}")]
VersionMismatch {
file_version: String,
current_version: String,
},
#[error("Invalid format: {0}")]
InvalidFormat(String),
#[error(
"Checksum mismatch at block {block_index}: expected {expected:#010x}, got {actual:#010x}"
)]
ChecksumMismatch {
block_index: u64,
expected: u32,
actual: u32,
},
#[error(
"Expansion limit exceeded at block {block_index}: decompressed size would exceed limit"
)]
ExpansionLimitExceeded {
block_index: u64,
},
#[error("Block index corrupted or truncated: {0}")]
IndexCorrupted(String),
}
impl CrushError {
#[must_use]
pub fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled)
}
}
#[derive(Error, Debug)]
pub enum PluginError {
#[error("Plugin not found: {0}")]
NotFound(String),
#[error("Duplicate plugin magic number: {0:?}")]
DuplicateMagic([u8; 4]),
#[error("Invalid plugin metadata: {0}")]
InvalidMetadata(String),
#[error("Plugin operation failed: {0}")]
OperationFailed(String),
#[error("Plugin operation was cancelled")]
Cancelled,
}
#[derive(Error, Debug)]
pub enum TimeoutError {
#[error("Operation timed out after {0:?}")]
Timeout(std::time::Duration),
#[error("Plugin panicked during execution")]
PluginPanic,
}
#[derive(Error, Debug)]
pub enum ValidationError {
#[error("Invalid magic number: expected Crush format, got {0:?}")]
InvalidMagic([u8; 4]),
#[error("CRC32 mismatch: expected {expected:08x}, got {actual:08x}")]
CrcMismatch { expected: u32, actual: u32 },
#[error("Invalid header format: {0}")]
InvalidHeader(String),
#[error("Corrupted data: {0}")]
CorruptedData(String),
#[error("Invalid scoring weights: {0}")]
InvalidWeights(String),
#[error("Invalid metadata format: {0}")]
InvalidMetadata(String),
}
pub type Result<T> = std::result::Result<T, CrushError>;