use std::time::Duration;
use super::duration_string::DurationString;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Freshness {
duration: Duration,
is_strict: bool,
}
impl Freshness {
#[must_use]
pub fn duration(&self) -> &Duration {
&self.duration
}
#[must_use]
pub fn is_strict(&self) -> bool {
self.is_strict
}
#[must_use]
pub fn set_strict(mut self) -> Self {
self.is_strict = true;
self
}
}
impl DurationString for Freshness {
fn duration(&self) -> &Duration {
&self.duration
}
}
impl From<Duration> for Freshness {
fn from(value: Duration) -> Self {
Self {
duration: value,
is_strict: false,
}
}
}
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: Duration::from_secs(1),
is_strict: false,
};
#[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"
);
}
#[test]
fn strict_default_test() {
assert!(!Freshness::default().is_strict());
}
}