aboutsummaryrefslogtreecommitdiff
path: root/server/src/database.rs
blob: 39b306ce0a03a436e288cea652d1639441a4e7b8 (plain)
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
use std::path::PathBuf;

use anyhow::Context;
use log::info;
use serde::{Deserialize, Serialize};
use typed_sled::Tree;

pub struct Database {
    pub db: sled::Db,
    pub users: Tree<String, User>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub name: String,
    pub display_name: String,
    pub password: Vec<u8>,
}

impl Database {
    pub fn open(path: &PathBuf) -> Result<Self, anyhow::Error> {
        info!("opening database… (takes O(n) time sadly)");
        let db = sled::open(path).context("opening database")?;
        Ok(Self {
            users: typed_sled::Tree::open(&db, "users"),
            db,
        })
    }
}