sui_types/storage/
error.rs1use typed_store_error::TypedStoreError;
5
6pub type Result<T, E = Error> = ::std::result::Result<T, E>;
7
8#[derive(Debug)]
9pub struct Error {
10    inner: Box<Inner>,
11}
12
13type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
14
15#[derive(Debug)]
16struct Inner {
17    kind: Kind,
18    source: Option<BoxError>,
19}
20
21#[derive(Copy, Clone, Debug, PartialEq, Eq)]
22pub enum Kind {
23    Serialization,
24    Missing,
25    Custom,
26}
27
28impl Error {
29    fn new<E: Into<BoxError>>(kind: Kind, source: Option<E>) -> Self {
30        Self {
31            inner: Box::new(Inner {
32                kind,
33                source: source.map(Into::into),
34            }),
35        }
36    }
37
38    pub fn serialization<E: Into<BoxError>>(e: E) -> Self {
39        Self::new(Kind::Serialization, Some(e))
40    }
41
42    pub fn missing<E: Into<BoxError>>(e: E) -> Self {
43        Self::new(Kind::Missing, Some(e))
44    }
45
46    pub fn custom<E: Into<BoxError>>(e: E) -> Self {
47        Self::new(Kind::Custom, Some(e))
48    }
49
50    pub fn kind(&self) -> Kind {
51        self.inner.kind
52    }
53}
54
55impl std::error::Error for Error {
56    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57        self.inner.source.as_ref().map(|e| &**e as _)
58    }
59}
60
61impl std::fmt::Display for Error {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "{:?}", self)
65    }
66}
67
68impl From<TypedStoreError> for Error {
69    fn from(e: TypedStoreError) -> Self {
70        Self::custom(e)
71    }
72}