46 lines
1 KiB
Rust
46 lines
1 KiB
Rust
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<Duration> for PersistentDuration {
|
|
fn from(duration: Duration) -> Self {
|
|
Self(duration)
|
|
}
|
|
}
|
|
|
|
impl Into<Duration> for PersistentDuration {
|
|
fn into(self) -> Duration {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl FromStr for PersistentDuration {
|
|
type Err = anyhow::Error;
|
|
|
|
fn from_str(s: &str) -> anyhow::Result<Self> {
|
|
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())
|
|
}
|
|
}
|