From 6eb81f43d521740a8f61e76e8625db1e358b8d35 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 5 Nov 2021 20:18:37 +0000 Subject: [PATCH] Change CofD modifiers to come after dice pool --- dicebot/src/cofd/parser.rs | 31 ++++++------------ dicebot/src/commands/parser.rs | 6 ++-- dicebot/src/parser/dice.rs | 58 ++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/dicebot/src/cofd/parser.rs b/dicebot/src/cofd/parser.rs index 56e10fd..037797e 100644 --- a/dicebot/src/cofd/parser.rs +++ b/dicebot/src/cofd/parser.rs @@ -45,13 +45,13 @@ pub fn parse_modifiers(input: &str) -> Result) -> Result { +fn convert_to_modifiers(parsed: &Vec) -> Result { use ParsedInfo::*; if parsed.len() == 0 { Ok(DicePoolModifiers::default()) @@ -79,19 +79,8 @@ fn convert_to_info(parsed: &Vec) -> Result Result { - //The "modifiers:" part is optional. Assume amounts if no modifier - //section found. - let split = input.split(":").collect::>(); - let (modifiers_str, amounts_str) = (match split[..] { - [amounts] => Ok(("", amounts)), - [modifiers, amounts] => Ok((modifiers, amounts)), - _ => Err(BotError::DiceParsingError( - DiceParsingError::UnconsumedInput, - )), - })?; - + let (amounts, modifiers_str) = parse_amounts(input)?; let modifiers = parse_modifiers(modifiers_str)?; - let amounts = parse_amounts(&amounts_str)?; Ok(DicePool::new(amounts, modifiers)) } @@ -175,7 +164,7 @@ mod tests { #[test] fn dice_pool_number_with_quality() { - let result = parse_dice_pool("n:8"); + let result = parse_dice_pool("8 n"); assert!(result.is_ok()); assert_eq!( result.unwrap(), @@ -186,7 +175,7 @@ mod tests { #[test] fn dice_pool_number_with_success_change() { let modifiers = DicePoolModifiers::custom_exceptional_on(3); - let result = parse_dice_pool("s3:8"); + let result = parse_dice_pool("8 s3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers)); } @@ -194,7 +183,7 @@ mod tests { #[test] fn dice_pool_with_quality_and_success_change() { let modifiers = DicePoolModifiers::custom(DicePoolQuality::Rote, 3); - let result = parse_dice_pool("rs3:8"); + let result = parse_dice_pool("8 rs3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), DicePool::easy_with_modifiers(8, modifiers)); } @@ -224,20 +213,20 @@ mod tests { let expected = DicePool::new(amounts, modifiers); - let result = parse_dice_pool("rs3:8+10-2+varname"); + let result = parse_dice_pool("8+10-2+varname rs3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); - let result = parse_dice_pool("rs3:8+10- 2 + varname"); + let result = parse_dice_pool("8+10- 2 + varname rs3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); - let result = parse_dice_pool("rs3 : 8+ 10 -2 + varname"); + let result = parse_dice_pool("8+ 10 -2 + varname rs3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); //This one has tabs in it. - let result = parse_dice_pool(" r s3 : 8 + 10 -2 + varname"); + let result = parse_dice_pool(" 8 + 10 -2 + varname r s3"); assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); } diff --git a/dicebot/src/commands/parser.rs b/dicebot/src/commands/parser.rs index 30ca7a4..c19e739 100644 --- a/dicebot/src/commands/parser.rs +++ b/dicebot/src/commands/parser.rs @@ -221,9 +221,9 @@ mod tests { #[test] fn pool_whitespace_test() { - parse_command("!pool ns3:8 ").expect("was error"); - parse_command(" !pool ns3:8").expect("was error"); - parse_command(" !pool ns3:8 ").expect("was error"); + parse_command("!pool 8 ns3 ").expect("was error"); + parse_command(" !pool 8 ns3").expect("was error"); + parse_command(" !pool 8 ns3 ").expect("was error"); } #[test] diff --git a/dicebot/src/parser/dice.rs b/dicebot/src/parser/dice.rs index 389515b..4991f81 100644 --- a/dicebot/src/parser/dice.rs +++ b/dicebot/src/parser/dice.rs @@ -151,8 +151,9 @@ where /// should not have an operator, but every one after that should. /// Accepts expressions like "8", "10 + variablename", "variablename - /// 3", etc. This function is currently common to systems that don't -/// deal with XdY rolls. Support for that will be added later. -pub fn parse_amounts(input: &str) -> ParseResult> { +/// deal with XdY rolls. Support for that will be added later. Returns +/// parsed amounts and unconsumed input (e.g. roll modifiers). +pub fn parse_amounts(input: &str) -> ParseResult<(Vec, &str)> { let input = input.trim(); let remaining_amounts = many(amount_parser()).map(|amounts: Vec>| amounts); @@ -169,12 +170,9 @@ pub fn parse_amounts(input: &str) -> ParseResult> { (amounts, results.1) })?; - if rest.len() == 0 { - // Any ParseResult errors will short-circuit the collect. - results.into_iter().collect() - } else { - Err(DiceParsingError::UnconsumedInput) - } + // Any ParseResult errors will short-circuit the collect. + let results: Vec = results.into_iter().collect::>()?; + Ok((results, rest)) } /// Parse an expression that expects a single number or variable. No @@ -263,20 +261,26 @@ mod parse_many_amounts_tests { assert!(result.is_ok()); assert_eq!( result.unwrap(), - vec![Amount { - operator: Operator::Plus, - element: Element::Number(1) - }] + ( + vec![Amount { + operator: Operator::Plus, + element: Element::Number(1) + }], + "" + ) ); let result = parse_amounts("10"); assert!(result.is_ok()); assert_eq!( result.unwrap(), - vec![Amount { - operator: Operator::Plus, - element: Element::Number(10) - }] + ( + vec![Amount { + operator: Operator::Plus, + element: Element::Number(10) + }], + "" + ) ); } @@ -295,20 +299,26 @@ mod parse_many_amounts_tests { assert!(result.is_ok()); assert_eq!( result.unwrap(), - vec![Amount { - operator: Operator::Plus, - element: Element::Variable("asdf".to_string()) - }] + ( + vec![Amount { + operator: Operator::Plus, + element: Element::Variable("asdf".to_string()) + }], + "" + ) ); let result = parse_amounts("nosis"); assert!(result.is_ok()); assert_eq!( result.unwrap(), - vec![Amount { - operator: Operator::Plus, - element: Element::Variable("nosis".to_string()) - }] + ( + vec![Amount { + operator: Operator::Plus, + element: Element::Variable("nosis".to_string()) + }], + "" + ) ); }