Rename CommandResult to ExecutionResult
continuous-integration/drone/push Build is passing Details

This commit is contained in:
projectmoon 2021-02-07 21:39:21 +00:00
parent 94be4d2578
commit 304c91c69d
8 changed files with 37 additions and 36 deletions

View File

@ -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::config::*;
use crate::context::{Context, RoomContext}; use crate::context::{Context, RoomContext};
use crate::db::Database; use crate::db::Database;
@ -56,7 +56,7 @@ fn create_client(config: &Config) -> Result<Client, BotError> {
/// out the full result of that command. /// out the full result of that command.
async fn handle_single_result( async fn handle_single_result(
client: &Client, client: &Client,
cmd_result: &CommandResult, cmd_result: &ExecutionResult,
respond_to: &str, respond_to: &str,
room_id: &RoomId, room_id: &RoomId,
) { ) {
@ -68,7 +68,7 @@ async fn handle_single_result(
/// out how many commands succeeded and failed (if any failed). /// out how many commands succeeded and failed (if any failed).
async fn handle_multiple_results( async fn handle_multiple_results(
client: &Client, client: &Client,
results: &[(&str, CommandResult)], results: &[(&str, ExecutionResult)],
respond_to: &str, respond_to: &str,
room_id: &RoomId, room_id: &RoomId,
) { ) {
@ -186,7 +186,7 @@ impl DiceBot {
.collect(); .collect();
//Up to 50 commands allowed, otherwise we send back an error. //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) stream::iter(commands)
.then(|command| async move { .then(|command| async move {
let ctx = Context { let ctx = Context {

View File

@ -31,7 +31,7 @@ pub struct Execution {
} }
impl Execution { impl Execution {
pub fn new(html: String) -> CommandResult { pub fn success(html: String) -> ExecutionResult {
Ok(Execution { html }) Ok(Execution { html })
} }
@ -63,7 +63,7 @@ impl ExecutionError {
/// Wraps either a successful command execution response, or an error /// Wraps either a successful command execution response, or an error
/// that occurred. /// that occurred.
pub type CommandResult = Result<Execution, ExecutionError>; pub type ExecutionResult = Result<Execution, ExecutionError>;
/// Extract response messages out of a type, whether it is success or /// Extract response messages out of a type, whether it is success or
/// failure. /// failure.
@ -73,7 +73,7 @@ pub trait ResponseExtractor {
fn message_html(&self, username: &str) -> String; fn message_html(&self, username: &str) -> String;
} }
impl ResponseExtractor for CommandResult { impl ResponseExtractor for ExecutionResult {
/// Error message in bolded HTML. /// Error message in bolded HTML.
fn message_html(&self, username: &str) -> String { fn message_html(&self, username: &str) -> String {
//TODO use user display name too //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. /// The trait that any command that can be executed must implement.
#[async_trait] #[async_trait]
pub trait Command: Send + Sync { pub trait Command: Send + Sync {
async fn execute(&self, ctx: &Context<'_>) -> CommandResult; async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult;
fn name(&self) -> &'static str; 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 /// go back to Matrix, if the command was executed (successfully or
/// not). If a command is determined to be ignored, this function will /// not). If a command is determined to be ignored, this function will
/// return None, signifying that we should not send a response. /// 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)?; let cmd = parser::parse_command(&ctx.message_body)?;
cmd.execute(ctx).await cmd.execute(ctx).await
} }
@ -119,7 +119,7 @@ mod tests {
#[test] #[test]
fn command_result_extractor_creates_bubble() { 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"); let message = result.message_html("@myuser:example.com");
assert!(message.contains( assert!(message.contains(
"<a href=\"https://matrix.to/#/@myuser:example.com\">@myuser:example.com</a>" "<a href=\"https://matrix.to/#/@myuser:example.com\">@myuser:example.com</a>"

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::basic::dice::ElementExpression; use crate::basic::dice::ElementExpression;
use crate::basic::roll::Roll; use crate::basic::roll::Roll;
use crate::context::Context; use crate::context::Context;
@ -12,13 +12,13 @@ impl Command for RollCommand {
"roll regular dice" "roll regular dice"
} }
async fn execute(&self, _ctx: &Context<'_>) -> CommandResult { async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult {
let roll = self.0.roll(); let roll = self.0.roll();
let html = format!( let html = format!(
"<p><strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}</p>", "<p><strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}</p>",
self.0, roll self.0, roll
); );
Execution::new(html) Execution::success(html)
} }
} }

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::cofd::dice::{roll_pool, DicePool, DicePoolWithContext}; use crate::cofd::dice::{roll_pool, DicePool, DicePoolWithContext};
use crate::context::Context; use crate::context::Context;
use async_trait::async_trait; use async_trait::async_trait;
@ -11,7 +11,7 @@ impl Command for PoolRollCommand {
"roll dice pool" "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 pool_with_ctx = DicePoolWithContext(&self.0, ctx);
let rolled_pool = roll_pool(&pool_with_ctx).await?; let rolled_pool = roll_pool(&pool_with_ctx).await?;
@ -20,6 +20,6 @@ impl Command for PoolRollCommand {
rolled_pool, rolled_pool.roll rolled_pool, rolled_pool.roll
); );
Execution::new(html) Execution::success(html)
} }
} }

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::context::Context; use crate::context::Context;
use crate::cthulhu::dice::{regular_roll, AdvancementRoll, DiceRoll, DiceRollWithContext}; use crate::cthulhu::dice::{regular_roll, AdvancementRoll, DiceRoll, DiceRollWithContext};
use async_trait::async_trait; use async_trait::async_trait;
@ -11,7 +11,7 @@ impl Command for CthRoll {
"roll percentile pool" "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 roll_with_ctx = DiceRollWithContext(&self.0, ctx);
let executed_roll = regular_roll(&roll_with_ctx).await?; let executed_roll = regular_roll(&roll_with_ctx).await?;
@ -20,7 +20,7 @@ impl Command for CthRoll {
executed_roll, executed_roll.roll executed_roll, executed_roll.roll
); );
Execution::new(html) Execution::success(html)
} }
} }
@ -32,7 +32,7 @@ impl Command for CthAdvanceRoll {
"roll percentile pool" "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. //TODO this will be converted to a result when supporting variables.
let roll = self.0.roll(); let roll = self.0.roll();
let html = format!( let html = format!(
@ -40,6 +40,6 @@ impl Command for CthAdvanceRoll {
self.0, roll self.0, roll
); );
Execution::new(html) Execution::success(html)
} }
} }

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::context::Context; use crate::context::Context;
use crate::logic::record_room_information; use crate::logic::record_room_information;
use async_trait::async_trait; use async_trait::async_trait;
@ -12,7 +12,7 @@ impl Command for ResyncCommand {
"resync room information" "resync room information"
} }
async fn execute(&self, ctx: &Context<'_>) -> CommandResult { async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let our_username: Option<UserId> = ctx.matrix_client.user_id().await; let our_username: Option<UserId> = ctx.matrix_client.user_id().await;
let our_username: &str = our_username.as_ref().map_or("", UserId::as_str); let our_username: &str = our_username.as_ref().map_or("", UserId::as_str);
@ -26,6 +26,6 @@ impl Command for ResyncCommand {
.await?; .await?;
let message = "Room information resynced.".to_string(); let message = "Room information resynced.".to_string();
Execution::new(message) Execution::success(message)
} }
} }

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::context::Context; use crate::context::Context;
use crate::help::HelpTopic; use crate::help::HelpTopic;
use async_trait::async_trait; use async_trait::async_trait;
@ -11,13 +11,13 @@ impl Command for HelpCommand {
"help information" "help information"
} }
async fn execute(&self, _ctx: &Context<'_>) -> CommandResult { async fn execute(&self, _ctx: &Context<'_>) -> ExecutionResult {
let help = match &self.0 { let help = match &self.0 {
Some(topic) => topic.message(), Some(topic) => topic.message(),
_ => "There is no help for this topic", _ => "There is no help for this topic",
}; };
let html = format!("<strong>Help:</strong> {}", help.replace("\n", "<br/>")); let html = format!("<strong>Help:</strong> {}", help.replace("\n", "<br/>"));
Execution::new(html) Execution::success(html)
} }
} }

View File

@ -1,4 +1,4 @@
use super::{Command, CommandResult, Execution}; use super::{Command, Execution, ExecutionResult};
use crate::context::Context; use crate::context::Context;
use crate::db::errors::DataError; use crate::db::errors::DataError;
use crate::db::variables::UserAndRoom; use crate::db::variables::UserAndRoom;
@ -12,7 +12,7 @@ impl Command for GetAllVariablesCommand {
"get all variables" "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 key = UserAndRoom(&ctx.username, &ctx.room.id.as_str());
let variables = ctx.db.variables.get_user_variables(&key)?; let variables = ctx.db.variables.get_user_variables(&key)?;
@ -28,7 +28,8 @@ impl Command for GetAllVariablesCommand {
"<strong>Variables:</strong><br/>{}", "<strong>Variables:</strong><br/>{}",
value.replace("\n", "<br/>") value.replace("\n", "<br/>")
); );
Execution::new(html)
Execution::success(html)
} }
} }
@ -40,7 +41,7 @@ impl Command for GetVariableCommand {
"retrieve variable value" "retrieve variable value"
} }
async fn execute(&self, ctx: &Context<'_>) -> CommandResult { async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let name = &self.0; let name = &self.0;
let key = UserAndRoom(&ctx.username, &ctx.room.id.as_str()); let key = UserAndRoom(&ctx.username, &ctx.room.id.as_str());
let result = ctx.db.variables.get_user_variable(&key, name); let result = ctx.db.variables.get_user_variable(&key, name);
@ -52,7 +53,7 @@ impl Command for GetVariableCommand {
}; };
let html = format!("<strong>Variable:</strong> {}", value); let html = format!("<strong>Variable:</strong> {}", value);
Execution::new(html) Execution::success(html)
} }
} }
@ -64,7 +65,7 @@ impl Command for SetVariableCommand {
"set variable value" "set variable value"
} }
async fn execute(&self, ctx: &Context<'_>) -> CommandResult { async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let name = &self.0; let name = &self.0;
let value = self.1; let value = self.1;
let key = UserAndRoom(&ctx.username, ctx.room.id.as_str()); let key = UserAndRoom(&ctx.username, ctx.room.id.as_str());
@ -73,7 +74,7 @@ impl Command for SetVariableCommand {
let content = format!("{} = {}", name, value); let content = format!("{} = {}", name, value);
let html = format!("<strong>Set Variable:</strong> {}", content); let html = format!("<strong>Set Variable:</strong> {}", content);
Execution::new(html) Execution::success(html)
} }
} }
@ -85,7 +86,7 @@ impl Command for DeleteVariableCommand {
"delete variable" "delete variable"
} }
async fn execute(&self, ctx: &Context<'_>) -> CommandResult { async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let name = &self.0; let name = &self.0;
let key = UserAndRoom(&ctx.username, ctx.room.id.as_str()); let key = UserAndRoom(&ctx.username, ctx.room.id.as_str());
let result = ctx.db.variables.delete_user_variable(&key, name); let result = ctx.db.variables.delete_user_variable(&key, name);
@ -97,6 +98,6 @@ impl Command for DeleteVariableCommand {
}; };
let html = format!("<strong>Remove Variable:</strong> {}", value); let html = format!("<strong>Remove Variable:</strong> {}", value);
Execution::new(html) Execution::success(html)
} }
} }