pub trait ResultExt<T, E> {
fn ok_or_log_error(self) -> Option<T>;
fn ok_or_log_error_once(self) -> Option<T>;
fn warn_on_err_once(self, msg: impl std::fmt::Display) -> Option<T>;
fn unwrap_debug_or_log_error(self) -> Option<T>;
}
impl<T, E> ResultExt<T, E> for Result<T, E>
where
E: std::fmt::Display,
{
#[track_caller]
fn ok_or_log_error(self) -> Option<T> {
match self {
Ok(t) => Some(t),
Err(err) => {
let loc = std::panic::Location::caller();
let (file, line) = (loc.file(), loc.line());
log::error!("{file}:{line} {err}");
None
}
}
}
#[track_caller]
fn ok_or_log_error_once(self) -> Option<T> {
match self {
Ok(t) => Some(t),
Err(err) => {
let loc = std::panic::Location::caller();
let (file, line) = (loc.file(), loc.line());
crate::error_once!("{file}:{line} {err}");
None
}
}
}
#[track_caller]
fn warn_on_err_once(self, msg: impl std::fmt::Display) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
let loc = std::panic::Location::caller();
let (file, line) = (loc.file(), loc.line());
crate::warn_once!("{file}:{line} {msg}: {err}");
None
}
}
}
#[track_caller]
fn unwrap_debug_or_log_error(self) -> Option<T> {
if cfg!(debug_assertions) {
match self {
Ok(value) => Some(value),
Err(err) => {
let loc = std::panic::Location::caller();
let (file, line) = (loc.file(), loc.line());
panic!("{file}:{line} {err}");
}
}
} else {
self.ok_or_log_error()
}
}
}