From 97d91704a18f9e8a6df47024f21c3a28bdf932e4 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 17 Oct 2020 15:18:51 +0000 Subject: [PATCH] Make context owned, in hopes of async --- src/cofd/dice.rs | 2 +- src/commands.rs | 13 ++++++++----- src/context.rs | 27 +++++++++++---------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index ee9ff0d..960dbb3 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -255,7 +255,7 @@ impl DicePoolRoll { } /// Attach a Context to a dice pool. Needed for database access. -pub struct DicePoolWithContext<'a>(pub &'a DicePool, pub &'a Context<'a>); +pub struct DicePoolWithContext<'a>(pub &'a DicePool, pub &'a Context); impl Roll for DicePoolWithContext<'_> { type Output = Result; diff --git a/src/commands.rs b/src/commands.rs index cbb6528..1c808eb 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -115,7 +115,7 @@ 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) { + let value = match ctx.db.get_user_variable(&ctx.room_id, &ctx.username, name) { Ok(num) => format!("{} = {}", name, num), Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name), Err(e) => format!("error getting {}: {}", name, e), @@ -139,7 +139,7 @@ impl Command for SetVariableCommand { let value = self.1; 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), @@ -161,7 +161,10 @@ impl Command for DeleteVariableCommand { fn execute(&self, ctx: &Context) -> Execution { let name = &self.0; - let value = match ctx.db.delete_user_variable(ctx.room_id, ctx.username, name) { + 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), @@ -195,8 +198,8 @@ pub struct CommandResult { /// 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 fn execute_command<'a>(ctx: &'a Context) -> Option { - let res = Command::parse(ctx.message_body).map(|cmd| { +pub fn execute_command(ctx: &Context) -> Option { + let res = Command::parse(&ctx.message_body).map(|cmd| { let execution = cmd.execute(ctx); (execution.plain().into(), execution.html().into()) }); diff --git a/src/context.rs b/src/context.rs index f7be4a3..d40623c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -3,25 +3,20 @@ use crate::db::Database; /// A context carried through the system providing access to things /// like the database. #[derive(Clone)] -pub struct Context<'a> { - pub db: &'a Database, - pub room_id: &'a str, - pub username: &'a str, - pub message_body: &'a str, +pub struct Context { + pub db: Database, + pub room_id: String, + pub username: String, + pub message_body: String, } -impl<'a> Context<'a> { - pub fn new( - db: &'a Database, - room_id: &'a str, - username: &'a str, - message_body: &'a str, - ) -> Context<'a> { +impl Context { + pub fn new(db: &Database, room_id: &str, username: &str, message_body: &str) -> Context { Context { - db: db, - room_id: room_id, - username: username, - message_body: message_body, + db: db.clone(), + room_id: room_id.to_owned(), + username: username.to_owned(), + message_body: message_body.to_owned(), } } }