Rename CommandResult to ExecutionResult

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::context::{Context, RoomContext};
use crate::db::Database;
@ -56,7 +56,7 @@ fn create_client(config: &Config) -> Result<Client, BotError> {
/// 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 {

View File

@ -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<Execution, ExecutionError>;
pub type ExecutionResult = Result<Execution, ExecutionError>;
/// 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(
"<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::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!(
"<p><strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}</p>",
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::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)
}
}

View File

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

View File

@ -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<UserId> = 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)
}
}

View File

@ -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!("<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::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 {
"<strong>Variables:</strong><br/>{}",
value.replace("\n", "<br/>")
);
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!("<strong>Variable:</strong> {}", 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!("<strong>Set Variable:</strong> {}", 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!("<strong>Remove Variable:</strong> {}", value);
Execution::new(html)
Execution::success(html)
}
}