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
use sled::Db;

use crate::{
    table::{Table, TableLayout},
    Error,
};

/// Database type.
#[derive(Debug, Clone)]
pub struct Base(Db);

impl Base {
    pub fn inner(&self) -> &Db {
        &self.0
    }

    pub fn table<T>(&self) -> Table<T>
    where
        T: TableLayout,
    {
        Table::new(self)
    }
}

/// Opens a database or create a new if the path does not point to a valid one.
pub fn open<P>(path: P) -> Result<Base, Error>
where
    P: AsRef<std::path::Path>,
{
    let db = sled::open(path).map_err(Error::SledErr)?;
    Ok(Base(db))
}