#[cfg(feature = "parser")]
mod pest_parser;
#[cfg(feature = "parser")]
pub use pest_parser::*;
#[derive(Debug, Clone)]
pub struct Parser {
config: ParserConfig,
}
#[derive(Debug, Clone)]
pub struct ParserConfig {
pub default_sides: u32,
pub allow_unlimited_pools: bool,
pub max_depth: u32,
}
impl Default for ParserConfig {
fn default() -> Self {
Self {
default_sides: 20, allow_unlimited_pools: false,
max_depth: 100,
}
}
}
impl Parser {
pub fn new() -> Self {
Self {
config: ParserConfig::default(),
}
}
pub fn with_config(config: ParserConfig) -> Self {
Self { config }
}
#[cfg(feature = "parser")]
pub fn eval(&self, expr: &str) -> Result<crate::core::RollResult, crate::error::DiceError> {
use pest_parser::PestParser;
let parser = PestParser::new(self.config.clone());
parser.eval(expr)
}
#[cfg(feature = "parser")]
pub fn parse(&self, expr: &str) -> Result<Box<dyn crate::core::Dice>, crate::error::DiceError> {
use pest_parser::PestParser;
let parser = PestParser::new(self.config.clone());
parser.parse(expr)
}
}
impl Default for Parser {
fn default() -> Self {
Self::new()
}
}