diff --git a/src/commands.rs b/src/commands.rs index 48eaa0e..a8da19c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,12 +1,23 @@ use crate::cofd::dice::DicePool; use crate::context::Context; +use crate::db::DataError; use crate::dice::ElementExpression; -use crate::error::{BotError, CommandError}; +use crate::error::BotError; use crate::help::HelpTopic; use crate::roll::Roll; +use thiserror::Error; pub mod parser; +#[derive(Error, Debug)] +pub enum CommandError { + #[error("invalid command: {0}")] + InvalidCommand(String), + + #[error("ignored command")] + IgnoredCommand, +} + pub struct Execution { plain: String, html: String, @@ -104,8 +115,8 @@ impl Command for GetVariableCommand { fn execute(&self, ctx: &Context) -> Execution { let name = &self.0; let value = match ctx.db.get_user_variable(ctx.room_id, ctx.username, name) { - Ok(Some(num)) => format!("{} = {}", name, num), - Ok(None) => format!("{} is not set", name), + Ok(num) => format!("{} = {}", name, num), + Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name), Err(e) => format!("error getting {}: {}", name, e), }; @@ -125,16 +136,17 @@ impl Command for SetVariableCommand { fn execute(&self, ctx: &Context) -> Execution { let name = &self.0; let value = self.1; - let value = match ctx + let result = ctx .db - .set_user_variable(ctx.room_id, ctx.username, name, value) - { + .set_user_variable(ctx.room_id, ctx.username, name, value); + + let content = match result { Ok(_) => format!("{} = {}", name, value), Err(e) => format!("error setting {}: {}", name, e), }; - let plain = format!("Set Variable: {}", value); - let html = format!("

Set Variable: {}", value); + let plain = format!("Set Variable: {}", content); + let html = format!("

Set Variable: {}", content); Execution { plain, html } } } @@ -150,6 +162,7 @@ impl Command for DeleteVariableCommand { let name = &self.0; let value = match ctx.db.delete_user_variable(ctx.room_id, ctx.username, name) { Ok(()) => format!("{} now unset", name), + Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name), Err(e) => format!("error deleting {}: {}", name, e), }; diff --git a/src/config.rs b/src/config.rs index e3e58e5..1590813 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,17 @@ -use crate::error::ConfigError; use serde::{self, Deserialize, Serialize}; use std::env; use std::fs; use std::path::PathBuf; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum ConfigError { + #[error("i/o error: {0}")] + IoError(#[from] std::io::Error), + + #[error("toml parsing error: {0}")] + TomlParsingError(#[from] toml::de::Error), +} pub fn read_config>(config_path: P) -> Result { let config_path = config_path.into(); diff --git a/src/db.rs b/src/db.rs index 1cae17b..14b998f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -36,17 +36,17 @@ impl Database { room_id: &str, username: &str, variable_name: &str, - ) -> Result, DataError> { + ) -> 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.db.get(&key)? { let layout: LayoutVerified<&[u8], I32> = LayoutVerified::new_unaligned(&*raw_value).expect("bytes do not fit schema"); let value: I32 = *layout; - Ok(Some(value.get())) + Ok(value.get()) } else { - Ok(None) + Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap())) } } @@ -59,7 +59,7 @@ 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.db.insert(&key, IVec::from(db_value.as_bytes()))?; Ok(()) } @@ -70,13 +70,10 @@ 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.db.remove(&key)? { Ok(()) } else { - let mut key = room_id.to_owned(); - key.push_str(username); - key.push_str(variable_name); - Err(DataError::KeyDoesNotExist(key)) + Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap())) } } } diff --git a/src/error.rs b/src/error.rs index cf1beb8..fa34ac5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,24 +1,8 @@ +use crate::commands::CommandError; +use crate::config::ConfigError; use crate::db::DataError; use thiserror::Error; -#[derive(Error, Debug)] -pub enum ConfigError { - #[error("i/o error: {0}")] - IoError(#[from] std::io::Error), - - #[error("toml parsing error: {0}")] - TomlParsingError(#[from] toml::de::Error), -} - -#[derive(Error, Debug)] -pub enum CommandError { - #[error("invalid command: {0}")] - InvalidCommand(String), - - #[error("ignored command")] - IgnoredCommand, -} - #[derive(Error, Debug)] pub enum BotError { #[error("configuration error: {0}")]