usage_tracker/
usages.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4/// Keeps track of the usages of an object.
5#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
6pub struct Usages {
7    /// All recorded usages of something.
8    usages: Vec<DateTime<Utc>>,
9}
10
11impl Usages {
12    /// Removes all recorded usages.
13    pub fn clear(&mut self) {
14        self.usages.clear();
15    }
16
17    /// Provides read access to all stored data.
18    pub fn list(&self) -> &Vec<DateTime<Utc>> {
19        &self.usages
20    }
21
22    /// Creates a new, empty Usages object.
23    pub fn new() -> Self {
24        Self { usages: Vec::new() }
25    }
26
27    /// Removes all recorded usages from before the value of the `before` parameter.
28    pub fn prune(&mut self, before: DateTime<Utc>) {
29        self.usages.retain(|u| u >= &before);
30    }
31
32    /// Records a new usage of an object.
33    pub fn record_usage(&mut self) {
34        self.usages.push(Utc::now());
35    }
36}