Parse a human-friendly duration string (`10s`, `5m`, `2h`). No fractional units. Used by `--ttl`.
(s: &str)
| 67 | /// Parse a human-friendly duration string (`10s`, `5m`, `2h`). No |
| 68 | /// fractional units. Used by `--ttl`. |
| 69 | pub fn parse_duration(s: &str) -> Result<std::time::Duration, String> { |
| 70 | let (num_str, unit) = s.split_at( |
| 71 | s.find(|c: char| !c.is_ascii_digit()) |
| 72 | .ok_or_else(|| format!("duration needs a unit suffix (s/m/h): {s}"))?, |
| 73 | ); |
| 74 | let n: u64 = num_str |
| 75 | .parse() |
| 76 | .map_err(|_| format!("bad duration number: {s}"))?; |
| 77 | let secs = match unit { |
| 78 | "s" => n, |
| 79 | "m" => n * 60, |
| 80 | "h" => n * 3600, |
| 81 | other => return Err(format!("unknown duration unit {other:?} (expected s/m/h)")), |
| 82 | }; |
| 83 | Ok(std::time::Duration::from_secs(secs)) |
| 84 | } |
| 85 | |
| 86 | #[cfg(test)] |
| 87 | mod tests { |
no test coverage detected