use std::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum ErrorKind {
SledError,
SerializationError,
IOError,
IntegrityError,
NotFound,
UnregisteredEntity,
}
#[derive(Debug)]
pub struct Error {
error_kind: ErrorKind,
message: String,
}
impl Error {
pub fn new(error_kind: ErrorKind, message: String) -> Error {
Error {
error_kind,
message,
}
}
pub fn kind(&self) -> ErrorKind {
self.error_kind
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Reindeer Error of type {:?} : {}",
self.error_kind, &self.message
)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
impl From<std::io::Error> for Error {
fn from(source: std::io::Error) -> Self {
Error::new(ErrorKind::IOError, source.to_string())
}
}
impl From<sled::Error> for Error {
fn from(source: sled::Error) -> Self {
Error::new(ErrorKind::SledError, source.to_string())
}
}
impl From<bincode::Error> for Error {
fn from(source: bincode::Error) -> Self {
Error::new(ErrorKind::SerializationError, source.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(source: serde_json::Error) -> Self {
Error::new(ErrorKind::SerializationError, source.to_string())
}
}