Make context owned, in hopes of async

This commit is contained in:
projectmoon 2020-10-17 15:18:51 +00:00
parent d1c4a01871
commit 1c3a0e754d
3 changed files with 20 additions and 22 deletions

View File

@ -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<RolledDicePool, BotError>;

View File

@ -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<CommandResult> {
let res = Command::parse(ctx.message_body).map(|cmd| {
pub fn execute_command(ctx: &Context) -> Option<CommandResult> {
let res = Command::parse(&ctx.message_body).map(|cmd| {
let execution = cmd.execute(ctx);
(execution.plain().into(), execution.html().into())
});

View File

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