| 25 | } |
| 26 | |
| 27 | pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error> |
| 28 | where |
| 29 | D: Deserializer<'de>, |
| 30 | { |
| 31 | let s = String::deserialize(deserializer)?; |
| 32 | if s.is_empty() { |
| 33 | return Err(serde::de::Error::custom("Duration string cannot be empty")); |
| 34 | } |
| 35 | if s == "never" { |
| 36 | return Ok(Duration::MAX); |
| 37 | } |
| 38 | let (value, unit) = s.split_at(s.len() - 1); |
| 39 | let value = value.parse::<u64>().map_err(serde::de::Error::custom)?; |
| 40 | |
| 41 | let seconds = match unit { |
| 42 | "s" => value, |
| 43 | "m" => value * 60, |
| 44 | "h" => value * 3600, |
| 45 | "d" => value * 24 * 3600, |
| 46 | _ => { |
| 47 | return Err(serde::de::Error::custom( |
| 48 | "Invalid time unit. Use s, m, h, or d", |
| 49 | )) |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | Ok(Duration::from_secs(seconds)) |
| 54 | } |