belajar_rust_crate_lib/lib.rs
1#[allow(unused_imports)]
2use chrono::{Local, Timelike};
3
4#[cfg(feature = "hello")]
5pub fn say_hello(name: &str) -> String {
6 format!("Hello, {}", name)
7}
8
9#[cfg(feature = "time")]
10pub fn get_timestamp() -> String {
11 let now = Local::now();
12 let formatted_time = now.format("%H:%M");
13 format!("{}", formatted_time)
14}
15
16#[cfg(feature = "greet")]
17pub fn say_greeting() -> String {
18 let hour = Local::now().hour();
19 let greet = match hour {
20 11..=14 => "Afternoon",
21 15..=20 => "Evening",
22 21..=24 => "Night",
23 _ => "Morning",
24 };
25 format!("Good {}", greet)
26}
27
28// #[cfg(test)]
29// mod tests {
30// use super::*;
31
32// #[test]
33// fn test_hello_lib() {
34// let hello = say_hello("dedy");
35// assert_eq!(hello, "Hello, dedy");
36// }
37
38// #[test]
39// fn test_get_timestamp() {
40// let sekarang = Local::now().format("%H:%M");
41// assert_eq!(format!("{}", sekarang), get_timestamp());
42// }
43
44// #[test]
45// fn test_greet_lib() {
46// let greet = say_greeting();
47// assert_ne!(greet, "");
48// }
49// }
50
51// test #[cfg(test)] will not work when using #[cfg(feature = "xxx")]
52// soo how to test: cargo test --features hello
53//