2021-02-07 21:39:21 +00:00
|
|
|
use super::{Command, Execution, ExecutionResult};
|
2020-11-04 20:34:57 +00:00
|
|
|
use crate::basic::dice::ElementExpression;
|
2021-05-25 23:54:06 +00:00
|
|
|
use crate::basic::parser::parse_element_expression;
|
2020-11-04 20:34:57 +00:00
|
|
|
use crate::basic::roll::Roll;
|
2020-10-31 12:40:44 +00:00
|
|
|
use crate::context::Context;
|
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 nom::Err as NomErr;
|
|
|
|
use std::convert::TryFrom;
|
2020-10-31 12:40:44 +00:00
|
|
|
|
|
|
|
pub struct RollCommand(pub ElementExpression);
|
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
impl From<RollCommand> for Box<dyn Command> {
|
|
|
|
fn from(cmd: RollCommand) -> Self {
|
|
|
|
Box::new(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for RollCommand {
|
|
|
|
type Error = BotError;
|
|
|
|
|
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
|
|
|
let result = parse_element_expression(input);
|
|
|
|
match result {
|
|
|
|
Ok((rest, expression)) if rest.len() == 0 => Ok(RollCommand(expression)),
|
|
|
|
//"Legacy code boundary": translates Nom errors into BotErrors.
|
|
|
|
Ok(_) => Err(BotError::NomParserIncomplete),
|
|
|
|
Err(NomErr::Error(e)) => Err(BotError::NomParserError(e.1)),
|
|
|
|
Err(NomErr::Failure(e)) => Err(BotError::NomParserError(e.1)),
|
|
|
|
Err(NomErr::Incomplete(_)) => Err(BotError::NomParserIncomplete),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for RollCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"roll regular dice"
|
|
|
|
}
|
|
|
|
|
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 roll = self.0.roll();
|
|
|
|
let html = format!(
|
2021-05-13 21:29:44 +00:00
|
|
|
"<strong>Dice:</strong> {}</p><p><strong>Result</strong>: {}",
|
2020-10-31 12:40:44 +00:00
|
|
|
self.0, roll
|
|
|
|
);
|
2021-01-30 14:17:34 +00:00
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
Execution::success(html)
|
2020-10-31 12:40:44 +00:00
|
|
|
}
|
|
|
|
}
|