2020-04-21 03:15:13 +00:00
|
|
|
use crate::dice::ElementExpression;
|
2020-04-21 06:09:43 +00:00
|
|
|
use crate::roll::Roll;
|
|
|
|
|
2020-04-21 03:15:13 +00:00
|
|
|
pub mod parser;
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub struct Execution {
|
|
|
|
plain: String,
|
|
|
|
html: String,
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Execution {
|
|
|
|
pub fn plain(&self) -> &str {
|
|
|
|
&self.plain
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub fn html(&self) -> &str {
|
|
|
|
&self.html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RollCommand(ElementExpression);
|
|
|
|
|
|
|
|
pub trait Command {
|
|
|
|
fn execute(&self) -> Execution;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command for RollCommand {
|
|
|
|
fn execute(&self) -> Execution {
|
|
|
|
let roll = self.0.roll();
|
|
|
|
let plain = format!("Dice: {}\nResult: {}", self.0, roll);
|
2020-04-21 06:09:43 +00:00
|
|
|
let html = format!(
|
|
|
|
"<strong>Dice:</strong> {}<br><strong>Result</strong>: {}",
|
|
|
|
self.0, roll
|
|
|
|
);
|
|
|
|
Execution { plain, html }
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 06:22:31 +00:00
|
|
|
pub fn parse_command(s: &str) -> Result<Option<Box<dyn Command>>, String> {
|
2020-04-21 06:07:03 +00:00
|
|
|
// Ignore trailing input, if any.
|
|
|
|
match parser::parse_command(s) {
|
2020-04-21 06:22:31 +00:00
|
|
|
Ok((_, result)) => Ok(result),
|
|
|
|
Err(err) => Err(err.to_string()),
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
}
|