Open trees for rooms and variables, but not yet use them.

This commit is contained in:
projectmoon 2020-10-18 15:13:34 +00:00 committed by ProjectMoon
parent ce82e6ddad
commit 6b7acbe520
3 changed files with 15 additions and 5 deletions

View File

@ -5,7 +5,7 @@ use chronicle_dicebot::error::BotError;
#[tokio::main]
async fn main() -> Result<(), BotError> {
let db = Database::new(&sled::open("test-db")?);
let db = Database::new("test-db")?;
let input = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
let command = match commands::parse(&input) {
Ok(command) => command,

View File

@ -26,7 +26,7 @@ async fn run() -> Result<(), BotError> {
.expect("Need a config as an argument");
let cfg = Arc::new(read_config(config_path)?);
let db = Database::new(&sled::open(cfg.database_path())?);
let db = Database::new(&cfg.database_path())?;
let state = Arc::new(RwLock::new(DiceBotState::new(&cfg)));
match DiceBot::new(&cfg, &state, &db) {

View File

@ -1,5 +1,5 @@
use byteorder::LittleEndian;
use sled::{Db, IVec};
use sled::{Db, IVec, Tree};
use std::collections::HashMap;
use thiserror::Error;
use zerocopy::byteorder::I32;
@ -13,6 +13,8 @@ type LittleEndianI32Layout<'a> = LayoutVerified<&'a [u8], I32<LittleEndian>>;
#[derive(Clone)]
pub struct Database {
db: Db,
variables: Tree,
rooms: Tree,
}
//TODO better combining of key and value in certain errors (namely
@ -59,8 +61,16 @@ fn convert(raw_value: &[u8]) -> Result<i32, DataError> {
}
impl Database {
pub fn new(db: &Db) -> Database {
Database { db: db.clone() }
pub fn new<P: AsRef<std::path::Path>>(path: P) -> Result<Database, DataError> {
let db = sled::open(path)?;
let variables = db.open_tree("variables")?;
let rooms = db.open_tree("rooms")?;
Ok(Database {
db: db.clone(),
variables: variables,
rooms: rooms,
})
}
pub async fn get_user_variables(