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
use serde::{Deserialize, Serialize};
pub(crate) mod cache;
pub mod database;
mod error;
mod filesystem;
use crate::error::DBError;
use database::{cached::CachedDB, indexed::IndexedDB, simple::SimpleDB};
pub trait GenericDatabase {
fn location(&self) -> &str;
fn exists(&self, key: &str) -> bool;
fn save<T>(&mut self, key: &str, data: &T) -> Result<(), DBError>
where
for<'de> T: Deserialize<'de> + Serialize + Clone;
fn load<T>(&mut self, identifier: &str) -> Result<T, DBError>
where
for<'de> T: Deserialize<'de> + Serialize + Clone;
fn delete(&mut self, identifier: &str);
}
fn init(dir: &str) {
std::fs::create_dir_all(dir).ok();
}
pub fn new(location: &str) -> SimpleDB {
init(location);
SimpleDB::new(location)
}
pub fn new_cached(location: &str, cache: Option<usize>, resync_every: u16) -> CachedDB {
init(location);
CachedDB::new(location, cache, resync_every)
}
pub fn new_indexed<I>(location: &str, cache: Option<usize>, resync_every: u16) -> IndexedDB<I>
where
for<'de> I: Deserialize<'de> + Serialize + Clone,
{
init(location);
IndexedDB::new(location, cache, resync_every)
}