forked from projectmoon/tenebrous-dicebot
Make context owned, in hopes of async
This commit is contained in:
parent
9268314421
commit
97d91704a1
|
@ -255,7 +255,7 @@ impl DicePoolRoll {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attach a Context to a dice pool. Needed for database access.
|
/// 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<'_> {
|
impl Roll for DicePoolWithContext<'_> {
|
||||||
type Output = Result<RolledDicePool, BotError>;
|
type Output = Result<RolledDicePool, BotError>;
|
||||||
|
|
|
@ -115,7 +115,7 @@ impl Command for GetVariableCommand {
|
||||||
|
|
||||||
fn execute(&self, ctx: &Context) -> Execution {
|
fn execute(&self, ctx: &Context) -> Execution {
|
||||||
let name = &self.0;
|
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),
|
Ok(num) => format!("{} = {}", name, num),
|
||||||
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
||||||
Err(e) => format!("error getting {}: {}", name, e),
|
Err(e) => format!("error getting {}: {}", name, e),
|
||||||
|
@ -139,7 +139,7 @@ impl Command for SetVariableCommand {
|
||||||
let value = self.1;
|
let value = self.1;
|
||||||
let result = ctx
|
let result = ctx
|
||||||
.db
|
.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 {
|
let content = match result {
|
||||||
Ok(_) => format!("{} = {}", name, value),
|
Ok(_) => format!("{} = {}", name, value),
|
||||||
|
@ -161,7 +161,10 @@ impl Command for DeleteVariableCommand {
|
||||||
|
|
||||||
fn execute(&self, ctx: &Context) -> Execution {
|
fn execute(&self, ctx: &Context) -> Execution {
|
||||||
let name = &self.0;
|
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),
|
Ok(()) => format!("{} now unset", name),
|
||||||
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
||||||
Err(e) => format!("error deleting {}: {}", name, e),
|
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
|
/// 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 fn execute_command<'a>(ctx: &'a Context) -> Option<CommandResult> {
|
pub fn execute_command(ctx: &Context) -> Option<CommandResult> {
|
||||||
let res = Command::parse(ctx.message_body).map(|cmd| {
|
let res = Command::parse(&ctx.message_body).map(|cmd| {
|
||||||
let execution = cmd.execute(ctx);
|
let execution = cmd.execute(ctx);
|
||||||
(execution.plain().into(), execution.html().into())
|
(execution.plain().into(), execution.html().into())
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,25 +3,20 @@ use crate::db::Database;
|
||||||
/// A context carried through the system providing access to things
|
/// A context carried through the system providing access to things
|
||||||
/// like the database.
|
/// like the database.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Context<'a> {
|
pub struct Context {
|
||||||
pub db: &'a Database,
|
pub db: Database,
|
||||||
pub room_id: &'a str,
|
pub room_id: String,
|
||||||
pub username: &'a str,
|
pub username: String,
|
||||||
pub message_body: &'a str,
|
pub message_body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Context<'a> {
|
impl Context {
|
||||||
pub fn new(
|
pub fn new(db: &Database, room_id: &str, username: &str, message_body: &str) -> Context {
|
||||||
db: &'a Database,
|
|
||||||
room_id: &'a str,
|
|
||||||
username: &'a str,
|
|
||||||
message_body: &'a str,
|
|
||||||
) -> Context<'a> {
|
|
||||||
Context {
|
Context {
|
||||||
db: db,
|
db: db.clone(),
|
||||||
room_id: room_id,
|
room_id: room_id.to_owned(),
|
||||||
username: username,
|
username: username.to_owned(),
|
||||||
message_body: message_body,
|
message_body: message_body.to_owned(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue