feattle_core/
last_reload.rs

1use chrono::{DateTime, Utc};
2use serde::Serialize;
3
4/// Store details of the last time the data was synchronized by calling
5/// [`crate::Feattles::reload()`].
6#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
7pub enum LastReload {
8    /// The data was never updated and all feattles carry their default values.
9    Never,
10    /// The reload finished with success, but no data was found. All feattle carry their default
11    /// values.
12    NoData { reload_date: DateTime<Utc> },
13    /// The reload finished with success.
14    Data {
15        reload_date: DateTime<Utc>,
16        version: i32,
17        version_date: DateTime<Utc>,
18    },
19}
20
21impl LastReload {
22    /// Indicate when, if ever, a reload finished with success.
23    pub fn reload_date(self) -> Option<DateTime<Utc>> {
24        match self {
25            LastReload::Never => None,
26            LastReload::NoData { reload_date, .. } | LastReload::Data { reload_date, .. } => {
27                Some(reload_date)
28            }
29        }
30    }
31
32    /// Indicate which is, if any, the current data version. Note that the value `0` is used for
33    /// [`LastReload::NoData`].
34    pub fn version(self) -> Option<i32> {
35        match self {
36            LastReload::Never => None,
37            LastReload::NoData { .. } => Some(0),
38            LastReload::Data { version, .. } => Some(version),
39        }
40    }
41
42    /// Indicate when, if known, this data version was created.
43    pub fn version_date(self) -> Option<DateTime<Utc>> {
44        match self {
45            LastReload::Never | LastReload::NoData { .. } => None,
46            LastReload::Data { version_date, .. } => Some(version_date),
47        }
48    }
49}