1use crate::prelude::DbKey;
2use kaspa_hashes::Hash;
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum StoreError {
7 #[error("key {0} not found in store")]
8 KeyNotFound(DbKey),
9
10 #[error("key {0} already exists in store")]
11 KeyAlreadyExists(String),
12
13 #[error("hash {0} already exists in store")]
16 HashAlreadyExists(Hash),
17
18 #[error("data inconsistency: {0}")]
19 DataInconsistency(String),
20
21 #[error("rocksdb error {0}")]
22 DbError(#[from] rocksdb::Error),
23
24 #[error("bincode error {0}")]
25 DeserializationError(#[from] Box<bincode::ErrorKind>),
26}
27
28pub type StoreResult<T> = std::result::Result<T, StoreError>;
29
30pub trait StoreResultExtensions<T> {
31 fn unwrap_option(self) -> Option<T>;
33}
34
35impl<T> StoreResultExtensions<T> for StoreResult<T> {
36 fn unwrap_option(self) -> Option<T> {
37 match self {
38 Ok(value) => Some(value),
39 Err(StoreError::KeyNotFound(_)) => None,
40 Err(err) => panic!("Unexpected store error: {err:?}"),
41 }
42 }
43}
44
45pub trait StoreResultEmptyTuple {
46 fn unwrap_or_exists(self);
48}
49
50impl StoreResultEmptyTuple for StoreResult<()> {
51 fn unwrap_or_exists(self) {
52 match self {
53 Ok(_) => (),
54 Err(StoreError::KeyAlreadyExists(_)) | Err(StoreError::HashAlreadyExists(_)) => (),
55 Err(err) => panic!("Unexpected store error: {err:?}"),
56 }
57 }
58}