stupid_simple_kv/
kv_error.rs1use std::{
2 error::Error,
3 sync::{RwLockReadGuard, RwLockWriteGuard, TryLockError},
4};
5
6use crate::KvBackend;
7
8#[derive(Debug)]
9pub enum KvError {
10 KeyDecodeError(String),
11 InvalidSelector,
12 ValEncodeError(bincode::error::EncodeError),
13 ValDecodeError(bincode::error::DecodeError),
14 ValDowncastError(String),
15 Other(String),
16 #[cfg(feature = "sqlite")]
17 SqliteError(rusqlite::Error),
18}
19
20pub type KvResult<T> = Result<T, KvError>;
21
22impl std::fmt::Display for KvError {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 match self {
25 KvError::KeyDecodeError(str) => write!(f, "Error decoding key: {str}"),
26 KvError::InvalidSelector => write!(
27 f,
28 "Invalid selector provided - Provide any one or two of start, end, prefix, not all"
29 ),
30 KvError::ValEncodeError(encode_error) => {
31 write!(f, "Error encoding value with bincode: {encode_error}")
32 }
33 KvError::ValDecodeError(decode_error) => {
34 write!(f, "Error decoding value with bincode: {decode_error}")
35 }
36 KvError::Other(str) => write!(f, "Error during kv op: {str}"),
37 KvError::SqliteError(error) => write!(f, "rusqlite error: {error}"),
38 KvError::ValDowncastError(s) => write!(f, "Error converting to KvValue: {s}"),
39 }
40 }
41}
42
43impl From<std::cell::BorrowError> for KvError {
44 fn from(value: std::cell::BorrowError) -> Self {
45 Self::Other(value.to_string())
46 }
47}
48
49impl From<std::sync::PoisonError<&mut Box<dyn KvBackend>>> for KvError {
50 fn from(value: std::sync::PoisonError<&mut Box<dyn KvBackend>>) -> Self {
51 Self::Other(value.to_string())
52 }
53}
54
55impl From<TryLockError<RwLockWriteGuard<'_, Box<dyn KvBackend>>>> for KvError {
56 fn from(value: TryLockError<RwLockWriteGuard<Box<dyn KvBackend>>>) -> Self {
57 Self::Other(value.to_string())
58 }
59}
60
61impl From<TryLockError<RwLockReadGuard<'_, Box<dyn KvBackend>>>> for KvError {
62 fn from(value: TryLockError<RwLockReadGuard<Box<dyn KvBackend>>>) -> Self {
63 Self::Other(value.to_string())
64 }
65}
66
67impl Error for KvError {}