1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use serde::{Deserialize, Serialize};
use std::convert::From;
use std::fmt;
use zeroize::Zeroize;

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Zeroize, Clone)]
#[zeroize(drop)]
pub enum StoreError {
  InvalidBlock(String),
  InvalidStoreUrl(String),
  IO(String),
  Mutex(String),
  Conflict(String),
  StoreNotFound(String),
}

impl fmt::Display for StoreError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      StoreError::InvalidBlock(block_id) => write!(f, "Invalid block: {}", block_id)?,
      StoreError::InvalidStoreUrl(url) => write!(f, "Invalid store url: {}", url)?,
      StoreError::IO(error) => write!(f, "IO: {}", error)?,
      StoreError::Mutex(error) => write!(f, "Internal locking problem: {}", error)?,
      StoreError::Conflict(error) => write!(f, "Conflict: {}", error)?,
      StoreError::StoreNotFound(name) => write!(f, "Store with name {} not found", name)?,
    }
    Ok(())
  }
}

pub type StoreResult<T> = Result<T, StoreError>;

error_convert_from!(std::io::Error, StoreError, IO(display));
error_convert_from!(url::ParseError, StoreError, InvalidStoreUrl(display));
#[cfg(feature = "sled")]
error_convert_from!(sled::Error, StoreError, IO(display));
#[cfg(feature = "sled")]
error_convert_from!(rmp_serde::encode::Error, StoreError, IO(display));
#[cfg(feature = "sled")]
error_convert_from!(rmp_serde::decode::Error, StoreError, IO(display));
#[cfg(feature = "dropbox")]
error_convert_from!(std::sync::mpsc::RecvError, StoreError, IO(display));
#[cfg(feature = "dropbox")]
error_convert_from!(dropbox_sdk::Error, StoreError, IO(display));
#[cfg(feature = "dropbox")]
error_convert_from!(dropbox_sdk::files::ListFolderError, StoreError, IO(display));
#[cfg(feature = "dropbox")]
error_convert_from!(dropbox_sdk::files::ListFolderContinueError, StoreError, IO(display));
#[cfg(feature = "dropbox")]
error_convert_from!(dropbox_sdk::files::UploadError, StoreError, IO(display));

impl<T> From<std::sync::PoisonError<T>> for StoreError {
  fn from(error: std::sync::PoisonError<T>) -> Self {
    StoreError::Mutex(format!("{}", error))
  }
}

#[cfg(feature = "sled")]
impl From<sled::transaction::TransactionError<StoreError>> for StoreError {
  fn from(err: sled::transaction::TransactionError<StoreError>) -> Self {
    match err {
      sled::transaction::TransactionError::Abort(err) => err,
      sled::transaction::TransactionError::Storage(err) => err.into(),
    }
  }
}