sw_errors/info/
info.rs

1use colorized::*;
2
3pub struct InfoTrigger;
4
5pub struct InfoRaw {
6    pub message: String,
7    pub owner: String,
8    pub scope: String,
9    pub timestamp: Option<bool>,
10}
11
12impl InfoTrigger {
13    /// Throws an informational message.
14    pub fn throw(&self, info_raw: InfoRaw) {
15        match info_raw.timestamp {
16            Some(true) => {
17                print_message_with_timestamp(info_raw);
18            }
19
20            Some(false) => {
21                print_message(info_raw);
22            }
23
24            None => {
25                print_message(info_raw);
26            }
27        }
28    }
29}
30
31fn print_message(info_raw: InfoRaw) {
32    let scope = format!("[{}]", info_raw.scope).color(Colors::BrightYellowFg).color(Colors::GreenBg);
33    let message = info_raw.message.color(Colors::GreenFg);
34
35    println!("{} {}", scope, message);
36}
37
38
39fn print_message_with_timestamp(info_raw: InfoRaw) {
40    let scope = format!("[{}]", info_raw.scope).color(Colors::YellowFg).color(Colors::GreenBg);
41    let message = info_raw.message.color(Colors::GreenFg);
42    let timestamp = chrono::Local::now()
43        .format("%Y-%m-%d - %H:%M:%S")
44        .to_string()
45        .color(Colors::GreenFg);
46
47    println!("{} {}: {}", scope, message, timestamp);
48}
49
50// [testModule] Logger Message: 2025-03-03:10:00:00
51
52#[cfg(test)]
53mod tests {
54
55    use super::*;
56
57    #[test]
58    fn test_info_trigger_with_timestamp() {
59        let info_trigger = InfoTrigger;
60        let info_raw = InfoRaw {
61            scope: String::from("TestScope"),
62            message: String::from("This is a test message"),
63            owner: String::from("TestOwner"),
64            timestamp: Some(true),
65        };
66
67        info_trigger.throw(info_raw);
68    }
69
70    #[test]
71    fn test_info_trigger_without_timestamp() {
72        let info_trigger = InfoTrigger;
73        let info_raw = InfoRaw {
74            scope: String::from("TestScope"),
75            message: String::from("This is a test message"),
76            owner: String::from("TestOwner"),
77            timestamp: Some(false),
78        };
79
80        info_trigger.throw(info_raw);
81    }
82}