use geoffrey_models::models::response::api_error::GeoffreyAPIError; pub type Result = std::result::Result; #[derive(Debug)] pub enum GeoffreyDBError { SledError(sled::Error), SerdeJsonError(serde_json::Error), NotUnique, NotFound, RegexError(regex::Error), } impl std::error::Error for GeoffreyDBError {} impl std::fmt::Display for GeoffreyDBError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { GeoffreyDBError::SledError(e) => write!(f, "Sled Error: {}", e), GeoffreyDBError::SerdeJsonError(e) => write!(f, "Serde JSON Error: {}", e), GeoffreyDBError::NotUnique => write!(f, "Entry is not unique."), GeoffreyDBError::NotFound => write!(f, "Entry was not found."), GeoffreyDBError::RegexError(e) => write!(f, "Regex Error: {}", e), } } } impl From for GeoffreyDBError { fn from(e: sled::Error) -> Self { GeoffreyDBError::SledError(e) } } impl From for GeoffreyDBError { fn from(e: serde_json::Error) -> Self { GeoffreyDBError::SerdeJsonError(e) } } impl From for GeoffreyDBError { fn from(e: regex::Error) -> Self { Self::RegexError(e) } } impl From for GeoffreyAPIError { fn from(e: GeoffreyDBError) -> Self { match e { GeoffreyDBError::SledError(_) => GeoffreyAPIError::DatabaseError(e.to_string()), GeoffreyDBError::SerdeJsonError(_) => GeoffreyAPIError::DatabaseError(e.to_string()), GeoffreyDBError::NotUnique => GeoffreyAPIError::EntryNotUnique, GeoffreyDBError::NotFound => GeoffreyAPIError::EntryNotFound, GeoffreyDBError::RegexError(_) => GeoffreyAPIError::InvalidRegex(e.to_string()), } } }