engine/config_handler/src/persistent_duration.rs

47 lines
1 KiB
Rust
Raw Normal View History

2024-08-23 11:22:09 +00:00
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())
}
}