use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetentionPolicy {
pub max_age: Option<Duration>,
pub max_bytes: Option<u64>,
}
impl RetentionPolicy {
pub const fn keep_forever() -> Self {
Self {
max_age: None,
max_bytes: None,
}
}
pub const fn keep_for(d: Duration) -> Self {
Self {
max_age: Some(d),
max_bytes: None,
}
}
pub fn days(days: u64) -> Self {
Self::keep_for(Duration::from_secs(days * 86_400))
}
pub const fn with_max_bytes(mut self, n: u64) -> Self {
self.max_bytes = Some(n);
self
}
pub fn cutoff_micros(&self, now_micros: u64) -> Option<u64> {
self.max_age
.map(|d| now_micros.saturating_sub(d.as_micros() as u64))
}
}