From ebfd230f317c09f2eebf22d1e63b897dd8942902 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 18 Oct 2020 20:36:47 +0000 Subject: [PATCH] Use variables namespace for user variables. --- src/cofd/dice.rs | 5 ++--- src/db.rs | 9 +++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 008d45e..879df89 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -1,5 +1,4 @@ use crate::context::Context; -use crate::db::DataError::KeyDoesNotExist; use crate::error::BotError; use crate::roll::Rolled; use futures::stream::{self, StreamExt, TryStreamExt}; @@ -526,7 +525,7 @@ mod tests { #[tokio::test] async fn rejects_large_expression_test() { - let db = Database::new(&sled::open(tempdir().unwrap()).unwrap()); + let db = Database::new(&tempdir().unwrap()).unwrap(); let ctx = Context::new(&db, "roomid", "username", "message"); let mut amounts = vec![]; @@ -551,7 +550,7 @@ mod tests { #[tokio::test] async fn can_resolve_variables_test() { - let db = Database::new(&sled::open(tempdir().unwrap()).unwrap()); + let db = Database::new(&tempdir().unwrap()).unwrap(); let ctx = Context::new(&db, "roomid", "username", "message"); db.set_user_variable(&ctx.room_id, &ctx.username, "myvariable", 10) diff --git a/src/db.rs b/src/db.rs index f714675..4af7474 100644 --- a/src/db.rs +++ b/src/db.rs @@ -82,7 +82,7 @@ impl Database { let prefix_len: usize = prefix.len(); let variables: Result, DataError> = self - .db + .variables .scan_prefix(prefix) .map(|entry| match entry { Ok((key, raw_value)) => { @@ -108,7 +108,7 @@ impl Database { ) -> Result { let key = to_key(room_id, username, variable_name); - if let Some(raw_value) = self.db.get(&key)? { + if let Some(raw_value) = self.variables.get(&key)? { convert(&raw_value) } else { Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap())) @@ -124,7 +124,8 @@ impl Database { ) -> Result<(), DataError> { let key = to_key(room_id, username, variable_name); let db_value: I32 = I32::new(value); - self.db.insert(&key, IVec::from(db_value.as_bytes()))?; + self.variables + .insert(&key, IVec::from(db_value.as_bytes()))?; Ok(()) } @@ -135,7 +136,7 @@ impl Database { variable_name: &str, ) -> Result<(), DataError> { let key = to_key(room_id, username, variable_name); - if let Some(_) = self.db.remove(&key)? { + if let Some(_) = self.variables.remove(&key)? { Ok(()) } else { Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))