1use std::sync::OnceLock;
2use chrono::NaiveTime;
3use regex::Regex;
4use crate::error::MpsError;
5
6fn re_word() -> &'static Regex {
7 static RE: OnceLock<Regex> = OnceLock::new();
8 RE.get_or_init(|| Regex::new(r"(?i)^(noon|midnight)$").unwrap())
9}
10
11fn re_12h() -> &'static Regex {
12 static RE: OnceLock<Regex> = OnceLock::new();
13 RE.get_or_init(|| Regex::new(r"(?i)^(\d{1,2})(?::(\d{2}))?\s*(am|pm)$").unwrap())
14}
15
16fn re_24h() -> &'static Regex {
17 static RE: OnceLock<Regex> = OnceLock::new();
18 RE.get_or_init(|| Regex::new(r"^(\d{1,2}):(\d{2})$").unwrap())
19}
20
21pub fn parse_time(input: &str) -> Result<NaiveTime, MpsError> {
32 let s = input.trim();
33
34 if let Some(cap) = re_word().captures(s) {
36 return match cap[1].to_lowercase().as_str() {
37 "noon" => Ok(NaiveTime::from_hms_opt(12, 0, 0).unwrap()),
38 "midnight" => Ok(NaiveTime::from_hms_opt(0, 0, 0).unwrap()),
39 _ => unreachable!(),
40 };
41 }
42
43 if let Some(cap) = re_12h().captures(s) {
45 let hour: u32 = cap[1].parse().unwrap();
46 let minute: u32 = cap.get(2).map(|m| m.as_str().parse().unwrap()).unwrap_or(0);
47 let ampm = cap[3].to_lowercase();
48 let h24 = match (ampm.as_str(), hour) {
49 ("am", 12) => 0,
50 ("am", h) => h,
51 ("pm", 12) => 12,
52 ("pm", h) => h + 12,
53 _ => unreachable!(),
54 };
55 return NaiveTime::from_hms_opt(h24, minute, 0)
56 .ok_or_else(|| MpsError::TimeParse(input.to_string()));
57 }
58
59 if let Some(cap) = re_24h().captures(s) {
61 let hour: u32 = cap[1].parse().unwrap();
62 let minute: u32 = cap[2].parse().unwrap();
63 return NaiveTime::from_hms_opt(hour, minute, 0)
64 .ok_or_else(|| MpsError::TimeParse(input.to_string()));
65 }
66
67 Err(MpsError::TimeParse(input.to_string()))
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 fn hm(h: u32, m: u32) -> NaiveTime { NaiveTime::from_hms_opt(h, m, 0).unwrap() }
75
76 #[test] fn test_noon() { assert_eq!(parse_time("noon").unwrap(), hm(12, 0)); }
77 #[test] fn test_noon_upper() { assert_eq!(parse_time("NOON").unwrap(), hm(12, 0)); }
78 #[test] fn test_midnight() { assert_eq!(parse_time("midnight").unwrap(), hm( 0, 0)); }
79 #[test] fn test_9am() { assert_eq!(parse_time("9am").unwrap(), hm( 9, 0)); }
80 #[test] fn test_9_30am() { assert_eq!(parse_time("9:30am").unwrap(), hm( 9,30)); }
81 #[test] fn test_3pm() { assert_eq!(parse_time("3pm").unwrap(), hm(15, 0)); }
82 #[test] fn test_3_45pm() { assert_eq!(parse_time("3:45pm").unwrap(), hm(15,45)); }
83 #[test] fn test_12am_midnight() { assert_eq!(parse_time("12am").unwrap(), hm( 0, 0)); }
84 #[test] fn test_12pm_noon() { assert_eq!(parse_time("12pm").unwrap(), hm(12, 0)); }
85 #[test] fn test_5pm() { assert_eq!(parse_time("5pm").unwrap(), hm(17, 0)); }
86 #[test] fn test_24h_colon() { assert_eq!(parse_time("17:00").unwrap(), hm(17, 0)); }
87 #[test] fn test_24h_930() { assert_eq!(parse_time("9:30").unwrap(), hm( 9,30)); }
88 #[test] fn test_24h_0000() { assert_eq!(parse_time("00:00").unwrap(), hm( 0, 0)); }
89 #[test] fn test_with_spaces() { assert_eq!(parse_time(" 5pm ").unwrap(), hm(17, 0)); }
90 #[test] fn test_am_uppercase() { assert_eq!(parse_time("9AM").unwrap(), hm( 9, 0)); }
91 #[test] fn test_pm_uppercase() { assert_eq!(parse_time("3PM").unwrap(), hm(15, 0)); }
92
93 #[test] fn test_reject_empty() { assert!(parse_time("").is_err()); }
94 #[test] fn test_reject_garbage() { assert!(parse_time("not-a-time").is_err()); }
95 #[test] fn test_reject_bare_num() { assert!(parse_time("930").is_err()); }
96 #[test] fn test_reject_bad_hour() { assert!(parse_time("25:00").is_err()); }
97 #[test] fn test_reject_bad_min() { assert!(parse_time("9:99pm").is_err()); }
98}