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
use serde::ser::Serialize;
use serde::de::DeserializeOwned;
use bincode;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadError {
    IoError,
    NoSuchKey,
    InvalidFormat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StoreError {
    IoError,
}

pub trait Storage {
    fn load_raw<K>(&self, key: K) -> Result<Vec<u8>, LoadError>
    where
        K: AsRef<str>;

    fn store_raw<K, V>(&mut self, key: K, value: V) -> Result<(), StoreError>
    where
        K: AsRef<str>,
        V: AsRef<[u8]>;

    fn remove_raw<K>(&mut self, key: K) -> Result<Vec<u8>, LoadError>
    where
        K: AsRef<str>;

    fn exists<K>(&self, key: K) -> bool
    where
        K: AsRef<str>;

    fn clear(&mut self);

    fn load<K, T>(&self, key: K) -> Result<T, LoadError>
    where
        K: AsRef<str>,
        T: DeserializeOwned,
    {
        bincode::deserialize(self.load_raw(key)?.as_slice()).map_err(|_| LoadError::InvalidFormat)
    }

    fn store<K, T>(&mut self, key: K, value: &T) -> Result<(), StoreError>
    where
        K: AsRef<str>,
        T: Serialize,
    {
        let bytes = bincode::serialize(value).expect("Failed to serialize data");
        self.store_raw(key, bytes)
    }

    fn remove<K, T>(&mut self, key: K) -> Result<T, LoadError>
    where
        K: AsRef<str>,
        T: DeserializeOwned,
    {
        let bytes = self.remove_raw(key)?;
        bincode::deserialize(&bytes).map_err(|_| LoadError::InvalidFormat)
    }
}