fastcode 0.1.0

Fast to writing your code with extension function and popular crates.
Documentation
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;
}

// A. Tracing feature enabled implementation
#[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
    }
}

// B. Tracing feature disabled implementation (uses color or plain println!)
#[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
    }
}