j_db/src/error.rs

51 lines
1.3 KiB
Rust

pub type Result<T> = std::result::Result<T, JDbError>;
#[derive(Debug)]
pub enum JDbError {
SledError(sled::Error),
SerdeJsonError(serde_json::Error),
NotUnique,
NotFound,
RegexError(regex::Error),
JsonError(json::JsonError),
}
impl std::error::Error for JDbError {}
impl std::fmt::Display for JDbError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JDbError::SledError(e) => write!(f, "Sled Error: {}", e),
JDbError::SerdeJsonError(e) => write!(f, "Serde JSON Error: {}", e),
JDbError::NotUnique => write!(f, "Entry is not unique."),
JDbError::NotFound => write!(f, "Entry was not found."),
JDbError::RegexError(e) => write!(f, "Regex Error: {}", e),
JDbError::JsonError(e) => write!(f, "JSON Error: {}", e),
}
}
}
impl From<sled::Error> for JDbError {
fn from(e: sled::Error) -> Self {
JDbError::SledError(e)
}
}
impl From<serde_json::Error> for JDbError {
fn from(e: serde_json::Error) -> Self {
JDbError::SerdeJsonError(e)
}
}
impl From<regex::Error> for JDbError {
fn from(e: regex::Error) -> Self {
Self::RegexError(e)
}
}
impl From<json::Error> for JDbError {
fn from(e: json::Error) -> Self {
Self::JsonError(e)
}
}