use thiserror::Error;
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum StoreError {
#[error("Key not found in store '{store}': {key}")]
KeyNotFound { store: String, key: String },
#[error("Serialization failed in store '{store}': {reason}")]
SerializationFailed { store: String, reason: String },
#[error("Deserialization failed in store '{store}': {reason}")]
DeserializationFailed { store: String, reason: String },
#[error("Type mismatch in store '{store}': expected {expected}, found {actual}")]
TypeMismatch {
store: String,
expected: String,
actual: String,
},
#[error("Invalid operation '{operation}' for store '{store}': {reason}")]
InvalidOperation {
store: String,
operation: String,
reason: String,
},
#[error("Operation requires transaction context for store '{store}'")]
RequiresTransaction { store: String },
#[error("Data corruption detected in store '{store}': {reason}")]
DataCorruption { store: String, reason: String },
#[error("Store implementation error in '{store}': {reason}")]
ImplementationError { store: String, reason: String },
}
impl StoreError {
pub fn is_not_found(&self) -> bool {
matches!(self, StoreError::KeyNotFound { .. })
}
pub fn is_serialization_error(&self) -> bool {
matches!(
self,
StoreError::SerializationFailed { .. } | StoreError::DeserializationFailed { .. }
)
}
pub fn is_type_error(&self) -> bool {
matches!(self, StoreError::TypeMismatch { .. })
}
pub fn is_integrity_error(&self) -> bool {
matches!(self, StoreError::DataCorruption { .. })
}
pub fn is_operation_error(&self) -> bool {
matches!(
self,
StoreError::InvalidOperation { .. } | StoreError::RequiresTransaction { .. }
)
}
pub fn is_implementation_error(&self) -> bool {
matches!(self, StoreError::ImplementationError { .. })
}
pub fn store_name(&self) -> &str {
match self {
StoreError::KeyNotFound { store, .. }
| StoreError::SerializationFailed { store, .. }
| StoreError::DeserializationFailed { store, .. }
| StoreError::TypeMismatch { store, .. }
| StoreError::InvalidOperation { store, .. }
| StoreError::RequiresTransaction { store, .. }
| StoreError::DataCorruption { store, .. }
| StoreError::ImplementationError { store, .. } => store,
}
}
pub fn operation(&self) -> Option<&str> {
match self {
StoreError::InvalidOperation { operation, .. } => Some(operation),
_ => None,
}
}
pub fn key(&self) -> Option<&str> {
match self {
StoreError::KeyNotFound { key, .. } => Some(key),
_ => None,
}
}
}
impl From<StoreError> for crate::Error {
fn from(err: StoreError) -> Self {
crate::Error::Store(err)
}
}