blob: ac678574686bd7cc997c737703ab979f949f6247 (
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
30
31
32
33
|
use anyhow::Result;
use redb::{Database, ReadableTable, TableDefinition};
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Deserialize)]
pub struct Config {
path: PathBuf,
}
pub fn open_db(config: Config) -> Result<Database> {
let db = Database::create(config.path)?;
Ok(db)
}
static T_CREDS: TableDefinition<&str, &str> = TableDefinition::new("creds");
pub trait DatabaseExt {
fn check_or_insert_creds(&self, username: &str, password: &str) -> anyhow::Result<bool>;
}
impl DatabaseExt for Database {
fn check_or_insert_creds(&self, username: &str, password: &str) -> anyhow::Result<bool> {
let txn = self.begin_write()?;
let mut table = txn.open_table(T_CREDS)?;
if let Some(pw) = table.get(username)? {
return Ok(pw.value() == password);
}
table.insert(username, password)?;
Ok(true)
}
}
|