1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! A model for the cache data.
//!
//! For more details, see the [API document](https://tetr.io/about/api/#cachedata).
use crate::model::prelude::*;
/// Data about how a request was cached.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct CacheData {
/// Whether the cache was hit.
/// Either `"hit"`, `"miss"`, or `"awaited"`.
/// `"awaited"` means resource was already being requested by another client.
pub status: Status,
/// When this resource was cached.
pub cached_at: u64,
/// When this resource's cache expires.
pub cached_until: u64,
}
impl CacheData {
/// Returns a UNIX timestamp when this resource was cached.
///
/// # Examples
///
/// ```ignore
/// // An example.
/// assert_eq!(cache_data.cached_at(), 1596317823);
/// ```
pub fn cached_at(&self) -> i64 {
self.cached_at as i64 / 1000
}
/// Returns a UNIX timestamp when this resource's cache expires.
///
/// # Examples
///
/// ```ignore
/// // An example.
/// assert_eq!(cache_data.cached_until(), 1661710844);
/// ```
pub fn cached_until(&self) -> i64 {
self.cached_until as i64 / 1000
}
}
impl AsRef<CacheData> for CacheData {
fn as_ref(&self) -> &Self {
self
}
}
/// A status of the cache.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Status {
Hit,
Miss,
/// Resource was already being requested by another client.
Awaited,
}
impl AsRef<Status> for Status {
fn as_ref(&self) -> &Self {
self
}
}
impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Status::Hit => write!(f, "hit"),
Status::Miss => write!(f, "miss"),
Status::Awaited => write!(f, "awaited"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_cached_at_and_cached_until() {
let cache_data = CacheData {
status: Status::Hit,
cached_at: 1661710769000,
cached_until: 1661710844000,
};
assert_eq!(cache_data.cached_at(), 1661710769);
assert_eq!(cache_data.cached_until(), 1661710844);
}
#[test]
fn cache_data_as_ref() {
let cache_data = CacheData {
status: Status::Hit,
cached_at: 1661710769000,
cached_until: 1661710844000,
};
assert_eq!(cache_data.as_ref().status, Status::Hit);
assert_eq!(cache_data.as_ref().cached_at(), 1661710769);
assert_eq!(cache_data.as_ref().cached_until(), 1661710844);
}
}