/// Convert an h:m:s string to a Duration pub mod hms_duration { use std::num::ParseIntError; use std::time::Duration; use serde::de::Error; use serde::Deserialize; use serde::Deserializer; use serde::Serializer; pub fn serialize(duration: &Duration, serializer: S) -> Result where S: Serializer, { let secs = duration.as_secs(); let hours = secs / 3600; let minutes = (secs % 3600) / 60; let seconds = secs % 60; let s = if hours > 0 { format!("{hours:0>2}:{minutes:0>2}:{seconds:0>2}") } else { format!("{minutes:0>2}:{seconds:0>2}") }; serializer.serialize_str(&s) } pub fn deserialize<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; from_string(s).map_err(Error::custom) } pub fn from_string(s: String) -> Result { let mut total_seconds = 0; let mut mult = 1; for x in s.rsplit(":") { let t: u64 = x.parse()?; total_seconds += t * mult; mult *= 60; } Ok(Duration::from_secs(total_seconds)) } }