2021-02-07 21:39:21 +00:00
|
|
|
use super::{Command, Execution, ExecutionResult};
|
2020-10-31 12:40:44 +00:00
|
|
|
use crate::context::Context;
|
2021-05-21 14:05:25 +00:00
|
|
|
use crate::db::errors::DataError;
|
|
|
|
use crate::db::Variables;
|
2021-05-25 23:54:06 +00:00
|
|
|
use crate::error::BotError;
|
2020-10-31 12:40:44 +00:00
|
|
|
use async_trait::async_trait;
|
2021-05-25 23:54:06 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
|
|
|
pub struct GetAllVariablesCommand;
|
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
impl From<GetAllVariablesCommand> for Box<dyn Command> {
|
|
|
|
fn from(cmd: GetAllVariablesCommand) -> Self {
|
|
|
|
Box::new(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for GetAllVariablesCommand {
|
|
|
|
type Error = BotError;
|
|
|
|
|
|
|
|
fn try_from(_: &str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(GetAllVariablesCommand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for GetAllVariablesCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"get all variables"
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2021-05-15 23:45:26 +00:00
|
|
|
let variables = ctx
|
|
|
|
.db
|
|
|
|
.get_user_variables(&ctx.username, ctx.room_id().as_str())
|
|
|
|
.await?;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
2021-01-30 22:12:44 +00:00
|
|
|
let mut variable_list: Vec<String> = variables
|
2021-01-30 14:17:34 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|(name, value)| format!(" - {} = {}", name, value))
|
2021-01-30 22:12:44 +00:00
|
|
|
.collect();
|
2020-10-31 12:40:44 +00:00
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
variable_list.sort();
|
|
|
|
|
|
|
|
let value = variable_list.join("\n");
|
2020-10-31 12:40:44 +00:00
|
|
|
let html = format!(
|
2021-01-31 14:06:25 +00:00
|
|
|
"<strong>Variables:</strong><br/>{}",
|
2020-10-31 12:40:44 +00:00
|
|
|
value.replace("\n", "<br/>")
|
|
|
|
);
|
2021-02-07 21:39:21 +00:00
|
|
|
|
|
|
|
Execution::success(html)
|
2020-10-31 12:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GetVariableCommand(pub String);
|
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
impl From<GetVariableCommand> for Box<dyn Command> {
|
|
|
|
fn from(cmd: GetVariableCommand) -> Self {
|
|
|
|
Box::new(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for GetVariableCommand {
|
|
|
|
type Error = BotError;
|
|
|
|
|
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(GetVariableCommand(input.to_owned()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for GetVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"retrieve variable value"
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2020-10-31 12:40:44 +00:00
|
|
|
let name = &self.0;
|
2021-05-15 23:45:26 +00:00
|
|
|
let result = ctx
|
|
|
|
.db
|
|
|
|
.get_user_variable(&ctx.username, ctx.room_id().as_str(), name)
|
|
|
|
.await;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
|
|
|
let value = match result {
|
|
|
|
Ok(num) => format!("{} = {}", name, num),
|
|
|
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
|
2021-01-30 14:17:34 +00:00
|
|
|
Err(e) => return Err(e.into()),
|
2020-10-31 12:40:44 +00:00
|
|
|
};
|
|
|
|
|
2021-01-31 14:06:25 +00:00
|
|
|
let html = format!("<strong>Variable:</strong> {}", value);
|
2021-02-07 21:39:21 +00:00
|
|
|
Execution::success(html)
|
2020-10-31 12:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SetVariableCommand(pub String, pub i32);
|
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
impl From<SetVariableCommand> for Box<dyn Command> {
|
|
|
|
fn from(cmd: SetVariableCommand) -> Self {
|
|
|
|
Box::new(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for SetVariableCommand {
|
|
|
|
type Error = BotError;
|
|
|
|
|
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
|
|
|
let (variable_name, value) = crate::parser::variables::parse_set_variable(input)?;
|
|
|
|
Ok(SetVariableCommand(variable_name, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for SetVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"set variable value"
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2020-10-31 12:40:44 +00:00
|
|
|
let name = &self.0;
|
|
|
|
let value = self.1;
|
|
|
|
|
2021-05-15 23:45:26 +00:00
|
|
|
ctx.db
|
|
|
|
.set_user_variable(&ctx.username, ctx.room_id().as_str(), name, value)
|
|
|
|
.await?;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
let content = format!("{} = {}", name, value);
|
2021-01-31 14:06:25 +00:00
|
|
|
let html = format!("<strong>Set Variable:</strong> {}", content);
|
2021-02-07 21:39:21 +00:00
|
|
|
Execution::success(html)
|
2020-10-31 12:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DeleteVariableCommand(pub String);
|
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
impl From<DeleteVariableCommand> for Box<dyn Command> {
|
|
|
|
fn from(cmd: DeleteVariableCommand) -> Self {
|
|
|
|
Box::new(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for DeleteVariableCommand {
|
|
|
|
type Error = BotError;
|
|
|
|
|
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(DeleteVariableCommand(input.to_owned()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for DeleteVariableCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"delete variable"
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2020-10-31 12:40:44 +00:00
|
|
|
let name = &self.0;
|
2021-05-15 23:45:26 +00:00
|
|
|
let result = ctx
|
|
|
|
.db
|
|
|
|
.delete_user_variable(&ctx.username, ctx.room_id().as_str(), name)
|
|
|
|
.await;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
|
|
|
let value = match result {
|
|
|
|
Ok(()) => format!("{} now unset", name),
|
|
|
|
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
|
2021-01-30 14:17:34 +00:00
|
|
|
Err(e) => return Err(e.into()),
|
2020-10-31 12:40:44 +00:00
|
|
|
};
|
|
|
|
|
2021-01-31 14:06:25 +00:00
|
|
|
let html = format!("<strong>Remove Variable:</strong> {}", value);
|
2021-02-07 21:39:21 +00:00
|
|
|
Execution::success(html)
|
2020-10-31 12:40:44 +00:00
|
|
|
}
|
|
|
|
}
|