From 304c91c69d337a7bcbf868992e8e75fb503ec458 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 7 Feb 2021 21:39:21 +0000 Subject: [PATCH] Rename CommandResult to ExecutionResult --- src/bot.rs | 8 ++++---- src/commands.rs | 12 ++++++------ src/commands/basic_rolling.rs | 6 +++--- src/commands/cofd.rs | 6 +++--- src/commands/cthulhu.rs | 10 +++++----- src/commands/management.rs | 6 +++--- src/commands/misc.rs | 6 +++--- src/commands/variables.rs | 19 ++++++++++--------- 8 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 48506c5..a08be6f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,4 +1,4 @@ -use crate::commands::{execute_command, CommandResult, ExecutionError, ResponseExtractor}; +use crate::commands::{execute_command, ExecutionError, ExecutionResult, ResponseExtractor}; use crate::config::*; use crate::context::{Context, RoomContext}; use crate::db::Database; @@ -56,7 +56,7 @@ fn create_client(config: &Config) -> Result { /// out the full result of that command. async fn handle_single_result( client: &Client, - cmd_result: &CommandResult, + cmd_result: &ExecutionResult, respond_to: &str, room_id: &RoomId, ) { @@ -68,7 +68,7 @@ async fn handle_single_result( /// out how many commands succeeded and failed (if any failed). async fn handle_multiple_results( client: &Client, - results: &[(&str, CommandResult)], + results: &[(&str, ExecutionResult)], respond_to: &str, room_id: &RoomId, ) { @@ -186,7 +186,7 @@ impl DiceBot { .collect(); //Up to 50 commands allowed, otherwise we send back an error. - let results: Vec<(&str, CommandResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE { + let results: Vec<(&str, ExecutionResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE { stream::iter(commands) .then(|command| async move { let ctx = Context { diff --git a/src/commands.rs b/src/commands.rs index 955ea83..b0a67de 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -31,7 +31,7 @@ pub struct Execution { } impl Execution { - pub fn new(html: String) -> CommandResult { + pub fn success(html: String) -> ExecutionResult { Ok(Execution { html }) } @@ -63,7 +63,7 @@ impl ExecutionError { /// Wraps either a successful command execution response, or an error /// that occurred. -pub type CommandResult = Result; +pub type ExecutionResult = Result; /// Extract response messages out of a type, whether it is success or /// failure. @@ -73,7 +73,7 @@ pub trait ResponseExtractor { fn message_html(&self, username: &str) -> String; } -impl ResponseExtractor for CommandResult { +impl ResponseExtractor for ExecutionResult { /// Error message in bolded HTML. fn message_html(&self, username: &str) -> String { //TODO use user display name too @@ -91,7 +91,7 @@ impl ResponseExtractor for CommandResult { /// The trait that any command that can be executed must implement. #[async_trait] pub trait Command: Send + Sync { - async fn execute(&self, ctx: &Context<'_>) -> CommandResult; + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult; fn name(&self) -> &'static str; } @@ -99,7 +99,7 @@ pub trait Command: Send + Sync { /// go back to Matrix, if the command was executed (successfully or /// not). If a command is determined to be ignored, this function will /// return None, signifying that we should not send a response. -pub async fn execute_command(ctx: &Context<'_>) -> CommandResult { +pub async fn execute_command(ctx: &Context<'_>) -> ExecutionResult { let cmd = parser::parse_command(&ctx.message_body)?; cmd.execute(ctx).await } @@ -119,7 +119,7 @@ mod tests { #[test] fn command_result_extractor_creates_bubble() { - let result = Execution::new("test".to_string()); + let result = Execution::success("test".to_string()); let message = result.message_html("@myuser:example.com"); assert!(message.contains( "@myuser:example.com" diff --git a/src/commands/basic_rolling.rs b/src/commands/basic_rolling.rs index 26489b6..4375e55 100644 --- a/src/commands/basic_rolling.rs +++ b/src/commands/basic_rolling.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::basic::dice::ElementExpression; use crate::basic::roll::Roll; use crate::context::Context; @@ -12,13 +12,13 @@ impl Command for RollCommand { "roll regular dice" } - async fn execute(&self, _ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult { let roll = self.0.roll(); let html = format!( "

Dice: {}

Result: {}

", self.0, roll ); - Execution::new(html) + Execution::success(html) } } diff --git a/src/commands/cofd.rs b/src/commands/cofd.rs index 7ac3a2a..781f364 100644 --- a/src/commands/cofd.rs +++ b/src/commands/cofd.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::cofd::dice::{roll_pool, DicePool, DicePoolWithContext}; use crate::context::Context; use async_trait::async_trait; @@ -11,7 +11,7 @@ impl Command for PoolRollCommand { "roll dice pool" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let pool_with_ctx = DicePoolWithContext(&self.0, ctx); let rolled_pool = roll_pool(&pool_with_ctx).await?; @@ -20,6 +20,6 @@ impl Command for PoolRollCommand { rolled_pool, rolled_pool.roll ); - Execution::new(html) + Execution::success(html) } } diff --git a/src/commands/cthulhu.rs b/src/commands/cthulhu.rs index c6d63a1..0f8c917 100644 --- a/src/commands/cthulhu.rs +++ b/src/commands/cthulhu.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::context::Context; use crate::cthulhu::dice::{regular_roll, AdvancementRoll, DiceRoll, DiceRollWithContext}; use async_trait::async_trait; @@ -11,7 +11,7 @@ impl Command for CthRoll { "roll percentile pool" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let roll_with_ctx = DiceRollWithContext(&self.0, ctx); let executed_roll = regular_roll(&roll_with_ctx).await?; @@ -20,7 +20,7 @@ impl Command for CthRoll { executed_roll, executed_roll.roll ); - Execution::new(html) + Execution::success(html) } } @@ -32,7 +32,7 @@ impl Command for CthAdvanceRoll { "roll percentile pool" } - async fn execute(&self, _ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult { //TODO this will be converted to a result when supporting variables. let roll = self.0.roll(); let html = format!( @@ -40,6 +40,6 @@ impl Command for CthAdvanceRoll { self.0, roll ); - Execution::new(html) + Execution::success(html) } } diff --git a/src/commands/management.rs b/src/commands/management.rs index 8f0bbc1..803c366 100644 --- a/src/commands/management.rs +++ b/src/commands/management.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::context::Context; use crate::logic::record_room_information; use async_trait::async_trait; @@ -12,7 +12,7 @@ impl Command for ResyncCommand { "resync room information" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let our_username: Option = ctx.matrix_client.user_id().await; let our_username: &str = our_username.as_ref().map_or("", UserId::as_str); @@ -26,6 +26,6 @@ impl Command for ResyncCommand { .await?; let message = "Room information resynced.".to_string(); - Execution::new(message) + Execution::success(message) } } diff --git a/src/commands/misc.rs b/src/commands/misc.rs index aede558..8666edb 100644 --- a/src/commands/misc.rs +++ b/src/commands/misc.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::context::Context; use crate::help::HelpTopic; use async_trait::async_trait; @@ -11,13 +11,13 @@ impl Command for HelpCommand { "help information" } - async fn execute(&self, _ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult { let help = match &self.0 { Some(topic) => topic.message(), _ => "There is no help for this topic", }; let html = format!("Help: {}", help.replace("\n", "
")); - Execution::new(html) + Execution::success(html) } } diff --git a/src/commands/variables.rs b/src/commands/variables.rs index 91d12d5..a00111b 100644 --- a/src/commands/variables.rs +++ b/src/commands/variables.rs @@ -1,4 +1,4 @@ -use super::{Command, CommandResult, Execution}; +use super::{Command, Execution, ExecutionResult}; use crate::context::Context; use crate::db::errors::DataError; use crate::db::variables::UserAndRoom; @@ -12,7 +12,7 @@ impl Command for GetAllVariablesCommand { "get all variables" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let key = UserAndRoom(&ctx.username, &ctx.room.id.as_str()); let variables = ctx.db.variables.get_user_variables(&key)?; @@ -28,7 +28,8 @@ impl Command for GetAllVariablesCommand { "Variables:
{}", value.replace("\n", "
") ); - Execution::new(html) + + Execution::success(html) } } @@ -40,7 +41,7 @@ impl Command for GetVariableCommand { "retrieve variable value" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let name = &self.0; let key = UserAndRoom(&ctx.username, &ctx.room.id.as_str()); let result = ctx.db.variables.get_user_variable(&key, name); @@ -52,7 +53,7 @@ impl Command for GetVariableCommand { }; let html = format!("Variable: {}", value); - Execution::new(html) + Execution::success(html) } } @@ -64,7 +65,7 @@ impl Command for SetVariableCommand { "set variable value" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let name = &self.0; let value = self.1; let key = UserAndRoom(&ctx.username, ctx.room.id.as_str()); @@ -73,7 +74,7 @@ impl Command for SetVariableCommand { let content = format!("{} = {}", name, value); let html = format!("Set Variable: {}", content); - Execution::new(html) + Execution::success(html) } } @@ -85,7 +86,7 @@ impl Command for DeleteVariableCommand { "delete variable" } - async fn execute(&self, ctx: &Context<'_>) -> CommandResult { + async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult { let name = &self.0; let key = UserAndRoom(&ctx.username, ctx.room.id.as_str()); let result = ctx.db.variables.delete_user_variable(&key, name); @@ -97,6 +98,6 @@ impl Command for DeleteVariableCommand { }; let html = format!("Remove Variable: {}", value); - Execution::new(html) + Execution::success(html) } }