use thiserror::Error;
pub type RadixResult<T> = Result<T, RadixError>;
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum RadixError {
#[error("Input collection cannot be empty")]
EmptyInput,
#[error("Type mismatch: expected {expected}, but got {got}")]
TypeMismatch {
expected: String,
got: String,
},
#[error("Invalid value encountered: {message}")]
InvalidValue {
message: String,
},
#[error("Memory allocation failed: {message}")]
MemoryAllocation {
message: String,
},
#[error("Integer overflow during transformation")]
Overflow,
#[error("Unsupported operation: {message}")]
UnsupportedOperation {
message: String,
},
}
impl RadixError {
pub fn type_mismatch(expected: impl Into<String>, got: impl Into<String>) -> Self {
RadixError::TypeMismatch {
expected: expected.into(),
got: got.into(),
}
}
pub fn invalid_value(message: impl Into<String>) -> Self {
RadixError::InvalidValue {
message: message.into(),
}
}
pub fn memory_allocation(message: impl Into<String>) -> Self {
RadixError::MemoryAllocation {
message: message.into(),
}
}
pub fn unsupported_operation(message: impl Into<String>) -> Self {
RadixError::UnsupportedOperation {
message: message.into(),
}
}
pub fn code(&self) -> &'static str {
match self {
RadixError::EmptyInput => "EMPTY_INPUT",
RadixError::TypeMismatch { .. } => "TYPE_MISMATCH",
RadixError::InvalidValue { .. } => "INVALID_VALUE",
RadixError::MemoryAllocation { .. } => "MEMORY_ALLOCATION",
RadixError::Overflow => "OVERFLOW",
RadixError::UnsupportedOperation { .. } => "UNSUPPORTED_OPERATION",
}
}
}