use crate::cofd::dice::DicePool;
use crate::dice::ElementExpression;
use crate::parser::trim;
use crate::roll::Roll;
pub mod parser;
pub struct Execution {
plain: String,
html: String,
}
impl Execution {
pub fn plain(&self) -> &str {
&self.plain
}
pub fn html(&self) -> &str {
&self.html
}
}
pub trait Command {
fn execute(&self) -> Execution;
fn name(&self) -> &'static str;
}
pub struct RollCommand(ElementExpression);
impl Command for RollCommand {
fn name(&self) -> &'static str {
"roll regular dice"
}
fn execute(&self) -> Execution {
let roll = self.0.roll();
let plain = format!("Dice: {}\nResult: {}", self.0, roll);
let html = format!(
"
Dice: {}
Result: {}
",
self.0, roll
);
Execution { plain, html }
}
}
pub struct PoolRollCommand(DicePool);
impl Command for PoolRollCommand {
fn name(&self) -> &'static str {
"roll dice pool"
}
fn execute(&self) -> Execution {
let roll = self.0.roll();
let plain = format!("Pool: {}\nResult: {}", self.0, roll);
let html = format!(
"
Pool: {}
Result: {}
",
self.0, roll
);
Execution { plain, html }
}
}
/// Parse a command string into a dynamic command execution trait object.
/// Returns an error if a command was recognized but not parsed correctly. Returns None if no
/// command was recognized.
pub fn parse_command(s: &str) -> Result