use std::{fmt, str::FromStr, time::Duration}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord)] pub struct PersistentDuration(Duration); impl PersistentDuration { pub fn as_secs_f32(&self) -> f32 { self.0.as_secs_f32() } pub fn from_secs_f32(s: f32) -> Self { Self(Duration::from_secs_f32(s)) } pub fn from_secs(s: u64) -> Self { Self(Duration::from_secs(s)) } } impl From for PersistentDuration { fn from(duration: Duration) -> Self { Self(duration) } } impl Into for PersistentDuration { fn into(self) -> Duration { self.0 } } impl FromStr for PersistentDuration { type Err = anyhow::Error; fn from_str(s: &str) -> anyhow::Result { Ok(Self(Duration::from_secs_f32(s.parse()?))) } } impl fmt::Display for PersistentDuration { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0.as_secs_f32()) } }