sw-errors 0.0.8

A library for error parsing and pretty-printing, used across the swtools toolset.
Documentation
use colorized::*;

pub struct InfoTrigger;

pub struct InfoRaw {
    pub message: String,
    pub owner: String,
    pub scope: String,
    pub timestamp: Option<bool>,
}

impl InfoTrigger {
    /// Throws an informational message.
    pub fn throw(&self, info_raw: InfoRaw) {
        match info_raw.timestamp {
            Some(true) => {
                print_message_with_timestamp(info_raw);
            }

            Some(false) => {
                print_message(info_raw);
            }

            None => {
                print_message(info_raw);
            }
        }
    }
}

fn print_message(info_raw: InfoRaw) {
    let scope = format!("[{}]", info_raw.scope).color(Colors::BrightYellowFg).color(Colors::GreenBg);
    let message = info_raw.message.color(Colors::GreenFg);

    println!("{} {}", scope, message);
}


fn print_message_with_timestamp(info_raw: InfoRaw) {
    let scope = format!("[{}]", info_raw.scope).color(Colors::YellowFg).color(Colors::GreenBg);
    let message = info_raw.message.color(Colors::GreenFg);
    let timestamp = chrono::Local::now()
        .format("%Y-%m-%d - %H:%M:%S")
        .to_string()
        .color(Colors::GreenFg);

    println!("{} {}: {}", scope, message, timestamp);
}

// [testModule] Logger Message: 2025-03-03:10:00:00

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_info_trigger_with_timestamp() {
        let info_trigger = InfoTrigger;
        let info_raw = InfoRaw {
            scope: String::from("TestScope"),
            message: String::from("This is a test message"),
            owner: String::from("TestOwner"),
            timestamp: Some(true),
        };

        info_trigger.throw(info_raw);
    }

    #[test]
    fn test_info_trigger_without_timestamp() {
        let info_trigger = InfoTrigger;
        let info_raw = InfoRaw {
            scope: String::from("TestScope"),
            message: String::from("This is a test message"),
            owner: String::from("TestOwner"),
            timestamp: Some(false),
        };

        info_trigger.throw(info_raw);
    }
}