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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::fmt;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    IoError(std::io::Error),
    Uuid(uuid::Error),
    ErasedSerde(erased_serde::Error),
    #[cfg(feature = "serde_importers")]
    RonDe(ron::de::Error),
    Boxed(Box<dyn std::error::Error + Send>),
    ExportUnsupported,
    Custom(String),
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            Error::IoError(ref e) => Some(e),
            Error::Uuid(ref e) => Some(e),
            Error::ErasedSerde(ref e) => Some(e),
            #[cfg(feature = "serde_importers")]
            Error::RonDe(ref e) => Some(e),
            Error::Boxed(ref e) => e.source(),
            Error::ExportUnsupported => None,
            Error::Custom(_) => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::Uuid(ref e) => e.fmt(f),
            Error::IoError(ref e) => e.fmt(f),
            Error::ErasedSerde(ref e) => e.fmt(f),
            #[cfg(feature = "serde_importers")]
            Error::RonDe(ref e) => e.fmt(f),
            Error::Boxed(ref e) => e.fmt(f),
            Error::ExportUnsupported => write!(f, "{:?}", self),
            Error::Custom(ref e) => write!(f, "{}", e),
        }
    }
}

impl From<uuid::Error> for Error {
    fn from(err: uuid::Error) -> Error {
        Error::Uuid(err)
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error::IoError(err)
    }
}
impl From<erased_serde::Error> for Error {
    fn from(err: erased_serde::Error) -> Error {
        Error::ErasedSerde(err)
    }
}

#[cfg(feature = "serde_importers")]
impl From<ron::de::Error> for Error {
    fn from(err: ron::de::Error) -> Error {
        Error::RonDe(err)
    }
}

impl From<Box<dyn std::error::Error + Send>> for Error {
    fn from(err: Box<dyn std::error::Error + Send>) -> Error {
        Error::Boxed(err)
    }
}

impl From<String> for Error {
    fn from(err: String) -> Error {
        Error::Custom(err)
    }
}

impl From<&str> for Error {
    fn from(err: &str) -> Error {
        Error::Custom(err.into())
    }
}