use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StoreError {
KeyNotFound(String),
TypeMismatch(String),
LockError(String),
AppendTypeMismatch(String),
Generic(String),
}
impl StoreError {
pub fn key_not_found<S: Into<String>>(key: S) -> Self {
StoreError::KeyNotFound(format!("Key '{}' not found in store", key.into()))
}
pub fn type_mismatch<S: Into<String>>(msg: S) -> Self {
StoreError::TypeMismatch(msg.into())
}
pub fn lock_error<S: Into<String>>(msg: S) -> Self {
StoreError::LockError(msg.into())
}
pub fn append_type_mismatch<S: Into<String>>(key: S) -> Self {
StoreError::AppendTypeMismatch(format!(
"Cannot append to key '{}': existing value is not a Vec<TState>",
key.into()
))
}
pub fn generic<S: Into<String>>(msg: S) -> Self {
StoreError::Generic(msg.into())
}
}
impl fmt::Display for StoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StoreError::KeyNotFound(msg) => write!(f, "Key not found: {msg}"),
StoreError::TypeMismatch(msg) => write!(f, "Type mismatch: {msg}"),
StoreError::LockError(msg) => write!(f, "Lock error: {msg}"),
StoreError::AppendTypeMismatch(msg) => write!(f, "Append type mismatch: {msg}"),
StoreError::Generic(msg) => write!(f, "Store error: {msg}"),
}
}
}
impl std::error::Error for StoreError {}