use std::time::Duration;
use super::duration_string::DurationString;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Freshness(Duration);
impl DurationString for Freshness {
fn duration(&self) -> &Duration {
&self.0
}
}
impl From<Duration> for Freshness {
fn from(value: Duration) -> Self {
Self(value)
}
}
impl Default for Freshness {
fn default() -> Self {
FRESHNESS_DEFAULT
}
}
impl std::fmt::Display for Freshness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&DurationString::to_string(self))
}
}
const FRESHNESS_DEFAULT: Freshness = Freshness(Duration::from_secs(1));
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::Freshness;
#[test]
fn display_test() {
assert_eq!(
&Freshness::from(Duration::from_millis(1_001)).to_string(),
"1s1ms"
);
}
}