reliakit_primitives/
duration.rs1use crate::{PrimitiveError, PrimitiveResult};
2#[cfg(feature = "alloc")]
3use alloc::string::String;
4use core::{fmt, str::FromStr, time::Duration};
5
6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct HumanDuration(Duration);
13
14impl HumanDuration {
15 pub fn parse(s: &str) -> PrimitiveResult<Self> {
25 if s.is_empty() {
26 return Err(PrimitiveError::Empty);
27 }
28
29 const RANK_H: u8 = 4;
32 const RANK_M: u8 = 3;
33 const RANK_S: u8 = 2;
34 const RANK_MS: u8 = 1;
35
36 let mut total_nanos: u128 = 0;
37 let mut last_rank: u8 = u8::MAX;
38 let mut pos = 0;
39 let bytes = s.as_bytes();
40
41 while pos < bytes.len() {
42 let num_start = pos;
44 while pos < bytes.len() && bytes[pos].is_ascii_digit() {
45 pos += 1;
46 }
47 if pos == num_start {
48 return Err(PrimitiveError::Invalid {
49 message: "expected a number before unit",
50 });
51 }
52 let num_str = &s[num_start..pos];
53 let num = parse_u64(num_str).ok_or(PrimitiveError::Invalid {
54 message: "duration number is too large",
55 })?;
56
57 let unit_start = pos;
59 while pos < bytes.len() && bytes[pos].is_ascii_alphabetic() {
60 pos += 1;
61 }
62 let unit = &s[unit_start..pos];
63
64 let (nanos_per_unit, rank): (u128, u8) = match unit {
65 "h" => (3_600 * 1_000_000_000, RANK_H),
66 "m" => (60 * 1_000_000_000, RANK_M),
67 "s" => (1_000_000_000, RANK_S),
68 "ms" => (1_000_000, RANK_MS),
69 _ => {
70 return Err(PrimitiveError::Invalid {
71 message: "unknown time unit; use h, m, s, or ms",
72 })
73 }
74 };
75
76 if rank >= last_rank {
77 return Err(PrimitiveError::Invalid {
78 message: "units must be in descending order (h, m, s, ms) with no duplicates",
79 });
80 }
81 last_rank = rank;
82
83 let component =
84 (num as u128)
85 .checked_mul(nanos_per_unit)
86 .ok_or(PrimitiveError::Invalid {
87 message: "duration overflow",
88 })?;
89
90 total_nanos = total_nanos
91 .checked_add(component)
92 .ok_or(PrimitiveError::Invalid {
93 message: "duration overflow",
94 })?;
95 }
96
97 let secs =
98 u64::try_from(total_nanos / 1_000_000_000).map_err(|_| PrimitiveError::Invalid {
99 message: "duration overflow: total duration exceeds maximum representable value",
100 })?;
101 let nanos = (total_nanos % 1_000_000_000) as u32;
102 Ok(Self(Duration::new(secs, nanos)))
103 }
104
105 pub fn as_duration(self) -> Duration {
107 self.0
108 }
109
110 pub fn as_secs(self) -> u64 {
112 self.0.as_secs()
113 }
114
115 pub fn as_millis(self) -> u128 {
117 self.0.as_millis()
118 }
119}
120
121fn parse_u64(s: &str) -> Option<u64> {
122 if s.is_empty() {
123 return None;
124 }
125 let mut result: u64 = 0;
126 for c in s.chars() {
127 let digit = c.to_digit(10)? as u64;
128 result = result.checked_mul(10)?.checked_add(digit)?;
129 }
130 Some(result)
131}
132
133impl fmt::Display for HumanDuration {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 let total_secs = self.0.as_secs();
136 let millis = self.0.subsec_millis();
137 let h = total_secs / 3600;
138 let m = (total_secs % 3600) / 60;
139 let s = total_secs % 60;
140
141 let mut wrote = false;
142 if h > 0 {
143 write!(f, "{h}h")?;
144 wrote = true;
145 }
146 if m > 0 {
147 write!(f, "{m}m")?;
148 wrote = true;
149 }
150 if s > 0 || millis > 0 {
151 if s > 0 {
152 write!(f, "{s}s")?;
153 }
154 if millis > 0 {
155 write!(f, "{millis}ms")?;
156 }
157 wrote = true;
158 }
159 if !wrote {
160 write!(f, "0s")?;
161 }
162 Ok(())
163 }
164}
165
166impl FromStr for HumanDuration {
167 type Err = PrimitiveError;
168
169 fn from_str(s: &str) -> Result<Self, Self::Err> {
170 Self::parse(s)
171 }
172}
173
174impl PartialEq<str> for HumanDuration {
175 fn eq(&self, other: &str) -> bool {
176 Self::parse(other).is_ok_and(|other| self == &other)
177 }
178}
179
180impl PartialEq<&str> for HumanDuration {
181 fn eq(&self, other: &&str) -> bool {
182 self.eq(*other)
183 }
184}
185
186#[cfg(feature = "alloc")]
187impl PartialEq<String> for HumanDuration {
188 fn eq(&self, other: &String) -> bool {
189 self.eq(other.as_str())
190 }
191}
192
193#[cfg(feature = "alloc")]
194impl PartialEq<&String> for HumanDuration {
195 fn eq(&self, other: &&String) -> bool {
196 self.eq(other.as_str())
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::HumanDuration;
203 use crate::PrimitiveError;
204 use alloc::string::ToString;
205
206 #[test]
207 fn parses_seconds() {
208 assert_eq!(HumanDuration::parse("45s").unwrap().as_secs(), 45);
209 }
210
211 #[test]
212 fn parses_minutes() {
213 assert_eq!(HumanDuration::parse("2m").unwrap().as_secs(), 120);
214 }
215
216 #[test]
217 fn parses_hours() {
218 assert_eq!(HumanDuration::parse("1h").unwrap().as_secs(), 3600);
219 }
220
221 #[test]
222 fn parses_milliseconds() {
223 assert_eq!(HumanDuration::parse("500ms").unwrap().as_millis(), 500);
224 }
225
226 #[test]
227 fn parses_combination() {
228 let d = HumanDuration::parse("1h30m45s").unwrap();
229 assert_eq!(d.as_secs(), 3600 + 1800 + 45);
230 }
231
232 #[test]
233 fn parses_minutes_and_seconds() {
234 assert_eq!(HumanDuration::parse("2m30s").unwrap().as_secs(), 150);
235 }
236
237 #[test]
238 fn rejects_empty() {
239 assert_eq!(HumanDuration::parse("").unwrap_err(), PrimitiveError::Empty);
240 }
241
242 #[test]
243 fn rejects_unknown_unit() {
244 assert!(HumanDuration::parse("5d").is_err());
245 }
246
247 #[test]
248 fn rejects_no_number() {
249 assert!(HumanDuration::parse("s").is_err());
250 }
251
252 #[test]
253 fn rejects_out_of_order_units() {
254 assert!(HumanDuration::parse("1s1h").is_err());
255 }
256
257 #[test]
258 fn rejects_duplicate_units() {
259 assert!(HumanDuration::parse("1h1h").is_err());
260 }
261
262 #[test]
263 fn rejects_ms_before_s() {
264 assert!(HumanDuration::parse("500ms30s").is_err());
265 }
266
267 #[test]
268 fn as_duration() {
269 let d = HumanDuration::parse("1s").unwrap();
270 assert_eq!(d.as_duration().as_secs(), 1);
271 }
272
273 #[test]
274 fn display_seconds() {
275 assert_eq!(HumanDuration::parse("45s").unwrap().to_string(), "45s");
276 }
277
278 #[test]
279 fn display_combined() {
280 assert_eq!(HumanDuration::parse("1h30m").unwrap().to_string(), "1h30m");
281 }
282
283 #[test]
284 fn display_zero() {
285 assert_eq!(HumanDuration::parse("0s").unwrap().to_string(), "0s");
286 }
287
288 #[test]
289 fn display_mixed_seconds_and_millis() {
290 assert_eq!(
291 HumanDuration::parse("1s500ms").unwrap().to_string(),
292 "1s500ms"
293 );
294 }
295
296 #[test]
297 fn display_millis_only() {
298 assert_eq!(HumanDuration::parse("500ms").unwrap().to_string(), "500ms");
299 }
300
301 #[test]
302 fn rejects_duration_that_overflows_u64_seconds() {
303 assert!(HumanDuration::parse("18446744073709551615h").is_err());
305 }
306
307 #[test]
308 fn from_str_and_string_comparisons() {
309 let duration = "1m30s".parse::<HumanDuration>().unwrap();
310 let owned = "90s".to_string();
311 assert_eq!(duration, "1m30s");
312 assert_eq!(duration, owned);
313 assert!("1s1m".parse::<HumanDuration>().is_err());
314 }
315}