belajar_rust_crate_lib 0.1.0

Belajar Rust Crate (Library)
Documentation
#[allow(unused_imports)]
use chrono::{Local, Timelike};

#[cfg(feature = "hello")]
pub fn say_hello(name: &str) -> String {
    format!("Hello, {}", name)
}

#[cfg(feature = "time")]
pub fn get_timestamp() -> String {
    let now = Local::now();
    let formatted_time = now.format("%H:%M");
    format!("{}", formatted_time)
}

#[cfg(feature = "greet")]
pub fn say_greeting() -> String {
    let hour = Local::now().hour();
    let greet = match hour {
        11..=14 => "Afternoon",
        15..=20 => "Evening",
        21..=24 => "Night",
        _ => "Morning",
    };
    format!("Good {}", greet)
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn test_hello_lib() {
//         let hello = say_hello("dedy");
//         assert_eq!(hello, "Hello, dedy");
//     }

//     #[test]
//     fn test_get_timestamp() {
//         let sekarang = Local::now().format("%H:%M");
//         assert_eq!(format!("{}", sekarang), get_timestamp());
//     }

//     #[test]
//     fn test_greet_lib() {
//         let greet = say_greeting();
//         assert_ne!(greet, "");
//     }
// }

// test #[cfg(test)] will not work when using #[cfg(feature = "xxx")]
// soo how to test: cargo test --features hello
//