use std::fmt::Display;
#[cfg(feature = "color")]
use colored::*;
pub trait ResultExt<T, E>
where
E: Display,
{
fn elog(&self);
fn ilog(&self);
fn wlog(&self);
fn dlog(&self);
fn elog_own(self) -> Self;
fn ilog_own(self) -> Self;
fn wlog_own(self) -> Self;
fn dlog_own(self) -> Self;
}
#[cfg(feature = "tracing")]
impl<T, E> ResultExt<T, E> for Result<T, E>
where
E: std::fmt::Display,
{
fn elog(&self) {
if let Err(err) = self {
tracing::error!("{err}");
}
}
fn ilog(&self) {
if let Err(err) = self {
tracing::info!("{err}");
}
}
fn wlog(&self) {
if let Err(err) = self {
tracing::warn!("{err}");
}
}
fn dlog(&self) {
if let Err(err) = self {
tracing::debug!("{err}");
}
}
fn elog_own(self) -> Self {
self.elog();
self
}
fn ilog_own(self) -> Self {
self.ilog();
self
}
fn wlog_own(self) -> Self {
self.wlog();
self
}
fn dlog_own(self) -> Self {
self.dlog();
self
}
}
#[cfg(not(feature = "tracing"))]
impl<T, E> ResultExt<T, E> for Result<T, E>
where
E: std::fmt::Display,
{
fn elog(&self) {
if let Err(err) = self {
#[cfg(feature = "color")]
eprintln!("{}", format!("Error: {err}").red());
#[cfg(not(feature = "color"))]
eprintln!("Error: {err}");
}
}
fn ilog(&self) {
if let Err(err) = self {
#[cfg(feature = "color")]
println!("{}", format!("Info: {err}").green());
#[cfg(not(feature = "color"))]
println!("Info: {err}");
}
}
fn wlog(&self) {
if let Err(err) = self {
#[cfg(feature = "color")]
println!("{}", format!("Warning: {err}").yellow());
#[cfg(not(feature = "color"))]
println!("Warning: {err}");
}
}
fn dlog(&self) {
if let Err(err) = self {
#[cfg(feature = "color")]
println!("{}", format!("{err}").blue());
#[cfg(not(feature = "color"))]
println!("{err}");
}
}
fn elog_own(self) -> Self {
self.elog();
self
}
fn ilog_own(self) -> Self {
self.ilog();
self
}
fn wlog_own(self) -> Self {
self.wlog();
self
}
fn dlog_own(self) -> Self {
self.dlog();
self
}
}