use crate::error::DbError;
pub(crate) type DbResult<R> = Result<R, DbError>;
pub(crate) trait DbResultHelper<T> {
fn context(self, context: &str) -> DbResult<T>;
#[allow(dead_code)]
fn with_context<O>(self, op: O) -> DbResult<T>
where
O: FnOnce() -> String;
}
impl<T, E> DbResultHelper<T> for Result<T, E>
where
E: std::error::Error,
{
fn context(self, context: &str) -> DbResult<T> {
self.map_err(|e| DbError::ServerError(format!("{context}: {e}")))
}
fn with_context<O>(self, op: O) -> DbResult<T>
where
O: FnOnce() -> String,
{
self.map_err(|e| DbError::ServerError(format!("{}: {e}", op())))
}
}
impl<T> DbResultHelper<T> for Option<T> {
fn context(self, context: &str) -> DbResult<T> {
self.ok_or_else(|| DbError::ServerError(context.to_owned()))
}
fn with_context<O>(self, op: O) -> DbResult<T>
where
O: FnOnce() -> String,
{
self.ok_or_else(|| DbError::ServerError(op()))
}
}