1use std::fmt;
2
3#[non_exhaustive]
5#[derive(Debug, Clone, Copy)]
6pub enum ErrorKind {
7 SledError,
9 SerializationError,
11 IOError,
13 IntegrityError,
15 NotFound,
17 UnregisteredEntity,
19}
20
21#[derive(Debug)]
23pub struct Error {
24 error_kind: ErrorKind,
25 message: String,
26}
27
28impl Error {
29 pub fn new(error_kind: ErrorKind, message: String) -> Error {
31 Error {
32 error_kind,
33 message,
34 }
35 }
36 pub fn kind(&self) -> ErrorKind {
37 self.error_kind
38 }
39}
40
41impl fmt::Display for Error {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 write!(
44 f,
45 "Reindeer Error of type {:?} : {}",
46 self.error_kind, &self.message
47 )
48 }
49}
50
51impl std::error::Error for Error {}
52
53pub type Result<T> = std::result::Result<T, Error>;
55
56impl From<std::io::Error> for Error {
57 fn from(source: std::io::Error) -> Self {
58 Error::new(ErrorKind::IOError, source.to_string())
59 }
60}
61
62impl From<sled::Error> for Error {
63 fn from(source: sled::Error) -> Self {
64 Error::new(ErrorKind::SledError, source.to_string())
65 }
66}
67
68impl From<bincode::Error> for Error {
69 fn from(source: bincode::Error) -> Self {
70 Error::new(ErrorKind::SerializationError, source.to_string())
71 }
72}
73
74impl From<serde_json::Error> for Error {
75 fn from(source: serde_json::Error) -> Self {
76 Error::new(ErrorKind::SerializationError, source.to_string())
77 }
78}