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
use std::{error::Error as StdError, fmt::Debug, io, result::Result as StdResult};

use thiserror::Error;

pub(crate) type Result<T> = StdResult<T, Error>;

/// Error returned by the storage component.
#[derive(Debug, Error)]
pub enum Error {
    /// Failed to create the given directory.
    #[error("failed to create {dir}: {source}")]
    CreateDir {
        /// The path of directory which was attempted to be created.
        dir: String,
        /// Underlying IO error.
        source: io::Error,
    },

    /// Failed to serialize data.
    #[error("serialization: {0}")]
    Serialization(#[source] bincode::ErrorKind),

    /// Failed to deserialize data.
    #[error("deserialization: {0}")]
    Deserialization(#[source] bincode::ErrorKind),

    /// Internal storage component error.
    #[error("internal: {0}")]
    Internal(Box<dyn StdError + Send + Sync>),
}

impl Error {
    pub(crate) fn from_serialization(error: bincode::ErrorKind) -> Self {
        Error::Serialization(error)
    }

    pub(crate) fn from_deserialization(error: bincode::ErrorKind) -> Self {
        Error::Deserialization(error)
    }
}

impl From<lmdb::Error> for Error {
    fn from(error: lmdb::Error) -> Self {
        Error::Internal(Box::new(error))
    }
}