diehard 1.0.0

A dice roll simulator for tabletop RPGs (and other dice games)
Documentation
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Michael Dippery <michael@monkey-robot.com>

//! Drives the command-line program.

use crate::expr::Expression;
use anyhow::Result;
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use log::info;

/// Program configuration.
#[derive(Debug, Parser)]
#[command(version)]
#[command(about = "A dice roll simulator for tabletop RPGs")]
pub struct Config {
    #[command(flatten)]
    pub verbosity: Verbosity,

    /// A set of dice to roll, such as "2d10 + 4"
    dice: Vec<String>,
}

/// Runs the command-line program.
#[derive(Debug)]
pub struct Runner {
    config: Config,
}

impl Runner {
    /// Create a new program runner using the given `config`.
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Runs the command-line program.
    pub fn run(&self) -> Result<()> {
        let expr = self.config.dice.join(" ");
        let expr = Expression::parse(expr)?;
        let result = expr.roll();
        info!("evaluating {expr}");
        println!("{result}");
        Ok(())
    }
}