1use thiserror::Error;
7
8use crate::Error;
9
10#[non_exhaustive]
16#[derive(Debug, Error)]
17pub enum StoreError {
18 #[error("Key not found in store '{store}': {key}")]
20 KeyNotFound { store: String, key: String },
21
22 #[error("Serialization failed in store '{store}': {reason}")]
24 SerializationFailed { store: String, reason: String },
25
26 #[error("Deserialization failed in store '{store}': {reason}")]
28 DeserializationFailed { store: String, reason: String },
29
30 #[error("Type mismatch in store '{store}': expected {expected}, found {actual}")]
32 TypeMismatch {
33 store: String,
34 expected: String,
35 actual: String,
36 },
37
38 #[error("Invalid operation '{operation}' for store '{store}': {reason}")]
40 InvalidOperation {
41 store: String,
42 operation: String,
43 reason: String,
44 },
45
46 #[error("Operation requires transaction context for store '{store}'")]
48 RequiresTransaction { store: String },
49
50 #[error("Data corruption detected in store '{store}': {reason}")]
52 DataCorruption { store: String, reason: String },
53
54 #[error("Invalid configuration for store '{store}': {reason}")]
56 InvalidConfiguration { store: String, reason: String },
57
58 #[error("Store implementation error in '{store}': {reason}")]
60 ImplementationError { store: String, reason: String },
61}
62
63impl StoreError {
64 pub fn is_not_found(&self) -> bool {
66 matches!(self, StoreError::KeyNotFound { .. })
67 }
68
69 pub fn is_serialization_error(&self) -> bool {
71 matches!(
72 self,
73 StoreError::SerializationFailed { .. } | StoreError::DeserializationFailed { .. }
74 )
75 }
76
77 pub fn is_type_error(&self) -> bool {
79 matches!(self, StoreError::TypeMismatch { .. })
80 }
81
82 pub fn is_integrity_error(&self) -> bool {
84 matches!(self, StoreError::DataCorruption { .. })
85 }
86
87 pub fn is_operation_error(&self) -> bool {
89 matches!(
90 self,
91 StoreError::InvalidOperation { .. } | StoreError::RequiresTransaction { .. }
92 )
93 }
94
95 pub fn is_implementation_error(&self) -> bool {
97 matches!(self, StoreError::ImplementationError { .. })
98 }
99
100 pub fn store_name(&self) -> &str {
102 match self {
103 StoreError::KeyNotFound { store, .. }
104 | StoreError::SerializationFailed { store, .. }
105 | StoreError::DeserializationFailed { store, .. }
106 | StoreError::TypeMismatch { store, .. }
107 | StoreError::InvalidOperation { store, .. }
108 | StoreError::RequiresTransaction { store, .. }
109 | StoreError::DataCorruption { store, .. }
110 | StoreError::InvalidConfiguration { store, .. }
111 | StoreError::ImplementationError { store, .. } => store,
112 }
113 }
114
115 pub fn operation(&self) -> Option<&str> {
117 match self {
118 StoreError::InvalidOperation { operation, .. } => Some(operation),
119 _ => None,
120 }
121 }
122
123 pub fn key(&self) -> Option<&str> {
125 match self {
126 StoreError::KeyNotFound { key, .. } => Some(key),
127 _ => None,
128 }
129 }
130}
131
132impl From<StoreError> for Error {
134 fn from(err: StoreError) -> Self {
135 Error::Store(err)
136 }
137}