based/src/result.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2024-12-17 23:28:43 +01:00
pub trait LogAndIgnore {
2024-12-18 14:33:53 +01:00
fn log_err_and_ignore(self, msg: &str);
fn log_warn_and_ignore(self, msg: &str);
2024-12-17 23:28:43 +01:00
}
impl<T, E: std::fmt::Debug> LogAndIgnore for Result<T, E> {
/// Handles the result by ignoring and logging it if it contains an error.
///
/// If the result is `Ok`, does nothing.
2024-12-18 14:33:53 +01:00
/// If the result is `Err(e)`, logs the message provided (`msg`) along with the error as error.
fn log_err_and_ignore(self, msg: &str) {
2024-12-17 23:28:43 +01:00
match self {
Ok(_) => {}
Err(e) => {
log::error!("{msg} : {:?}", e);
}
}
}
2024-12-18 14:33:53 +01:00
/// Handles the result by ignoring and logging it if it contains an error.
///
/// If the result is `Ok`, does nothing.
/// If the result is `Err(e)`, logs the message provided (`msg`) along with the error as warning.
fn log_warn_and_ignore(self, msg: &str) {
match self {
Ok(_) => {}
Err(e) => {
log::warn!("{msg} : {:?}", e);
}
}
}
}
2024-12-30 21:49:20 +01:00
pub trait LogNoneAndPass {
fn log_warn_none_and_pass(self, msg: impl Fn() -> String) -> Self;
fn log_err_none_and_pass(self, msg: impl Fn() -> String) -> Self;
}
impl<T> LogNoneAndPass for Option<T> {
fn log_warn_none_and_pass(self, msg: impl Fn() -> String) -> Option<T> {
if matches!(self, None) {
log::warn!("{}", msg());
}
return self;
}
fn log_err_none_and_pass(self, msg: impl Fn() -> String) -> Option<T> {
if matches!(self, None) {
log::error!("{}", msg());
}
return self;
}
}