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
use crate::Error;
use enum_dispatch::enum_dispatch;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[enum_dispatch]
pub trait KVStorage {
fn available(&self) -> Result<(), Error>;
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<GetResponse<T>, Error>;
fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), Error>;
#[cfg(any(test, feature = "testing"))]
fn reset_and_clear(&mut self) -> Result<(), Error>;
}
impl<'a, S> KVStorage for &'a mut S
where
S: KVStorage,
{
fn available(&self) -> Result<(), Error> {
S::available(self)
}
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<GetResponse<T>, Error> {
S::get(self, key)
}
fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), Error> {
S::set(self, key, value)
}
#[cfg(any(test, feature = "testing"))]
fn reset_and_clear(&mut self) -> Result<(), Error> {
S::reset_and_clear(self)
}
}
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(tag = "data")]
pub struct GetResponse<T> {
pub last_update: u64,
pub value: T,
}
impl<T> GetResponse<T> {
pub fn new(value: T, last_update: u64) -> Self {
Self { last_update, value }
}
}