use colorized::*;
pub struct InfoTrigger;
pub struct InfoRaw {
pub message: String,
pub owner: String,
pub scope: String,
pub timestamp: Option<bool>,
}
impl InfoTrigger {
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);
}
#[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);
}
}