Use variables namespace for user variables.

This commit is contained in:
projectmoon 2020-10-18 20:36:47 +00:00 committed by ProjectMoon
parent 6b7acbe520
commit ebfd230f31
2 changed files with 7 additions and 7 deletions

View File

@ -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)

View File

@ -82,7 +82,7 @@ impl Database {
let prefix_len: usize = prefix.len();
let variables: Result<Vec<_>, DataError> = self
.db
.variables
.scan_prefix(prefix)
.map(|entry| match entry {
Ok((key, raw_value)) => {
@ -108,7 +108,7 @@ impl Database {
) -> Result<i32, DataError> {
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<LittleEndian> = 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()))