2020-10-31 14:03:18 +00:00
|
|
|
use super::dice::{AdvancementRoll, DiceRoll, DiceRollModifier};
|
2021-05-21 14:44:03 +00:00
|
|
|
use crate::parser::dice::DiceParsingError;
|
2020-10-31 14:03:18 +00:00
|
|
|
|
|
|
|
//TOOD convert these to use parse_amounts from the common dice code.
|
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
fn parse_modifier(input: &str) -> Result<DiceRollModifier, DiceParsingError> {
|
2020-10-31 20:29:55 +00:00
|
|
|
if input.ends_with("bb") {
|
2020-11-04 20:09:39 +00:00
|
|
|
Ok(DiceRollModifier::TwoBonus)
|
2020-10-31 20:29:55 +00:00
|
|
|
} else if input.ends_with("b") {
|
2020-11-04 20:09:39 +00:00
|
|
|
Ok(DiceRollModifier::OneBonus)
|
2020-10-31 20:29:55 +00:00
|
|
|
} else if input.ends_with("pp") {
|
2020-11-04 20:09:39 +00:00
|
|
|
Ok(DiceRollModifier::TwoPenalty)
|
2020-10-31 20:29:55 +00:00
|
|
|
} else if input.ends_with("p") {
|
2020-11-04 20:09:39 +00:00
|
|
|
Ok(DiceRollModifier::OnePenalty)
|
2020-10-31 20:29:55 +00:00
|
|
|
} else {
|
2020-11-04 20:09:39 +00:00
|
|
|
Ok(DiceRollModifier::Normal)
|
2020-10-31 20:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
//Make diceroll take a vec of Amounts
|
|
|
|
//Split based on :, send first part to parse_modifier.
|
|
|
|
//Send second part to parse_amounts
|
2020-10-31 14:03:18 +00:00
|
|
|
pub fn parse_regular_roll(input: &str) -> Result<DiceRoll, DiceParsingError> {
|
2020-11-04 20:09:39 +00:00
|
|
|
let input: Vec<&str> = input.trim().split(":").collect();
|
2020-10-31 14:03:18 +00:00
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
let (modifiers_str, amounts_str) = match input[..] {
|
|
|
|
[amounts] => Ok(("", amounts)),
|
|
|
|
[modifiers, amounts] => Ok((modifiers, amounts)),
|
|
|
|
_ => Err(DiceParsingError::UnconsumedInput),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
let modifier = parse_modifier(modifiers_str)?;
|
2021-05-21 14:44:03 +00:00
|
|
|
let amount = crate::parser::dice::parse_single_amount(amounts_str)?;
|
2021-05-13 20:24:17 +00:00
|
|
|
Ok(DiceRoll { modifier, amount })
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_advancement_roll(input: &str) -> Result<AdvancementRoll, DiceParsingError> {
|
|
|
|
let input = input.trim();
|
2021-05-21 14:44:03 +00:00
|
|
|
let amounts = crate::parser::dice::parse_single_amount(input)?;
|
2020-10-31 14:03:18 +00:00
|
|
|
|
2021-03-18 20:54:49 +00:00
|
|
|
Ok(AdvancementRoll {
|
|
|
|
existing_skill: amounts,
|
|
|
|
})
|
2020-10-31 13:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:03:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-05-21 14:44:03 +00:00
|
|
|
use crate::parser::dice::{Amount, Element, Operator};
|
2020-10-31 14:03:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_single_number() {
|
|
|
|
let result = parse_regular_roll("60");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
DiceRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
amount: Amount {
|
2020-11-04 20:09:39 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
},
|
2020-10-31 14:03:18 +00:00
|
|
|
modifier: DiceRollModifier::Normal
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:24:17 +00:00
|
|
|
#[test]
|
|
|
|
fn regular_roll_rejects_complex_expressions() {
|
|
|
|
let result = parse_regular_roll("3 + abc + bob - 4");
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:29:55 +00:00
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_two_bonus() {
|
2020-11-04 20:09:39 +00:00
|
|
|
let result = parse_regular_roll("bb:60");
|
2020-10-31 20:29:55 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
DiceRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
amount: Amount {
|
2020-11-04 20:09:39 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
},
|
2020-10-31 20:29:55 +00:00
|
|
|
modifier: DiceRollModifier::TwoBonus
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_one_bonus() {
|
2020-11-04 20:09:39 +00:00
|
|
|
let result = parse_regular_roll("b:60");
|
2020-10-31 20:29:55 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
DiceRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
amount: Amount {
|
2020-11-04 20:09:39 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
},
|
2020-10-31 20:29:55 +00:00
|
|
|
modifier: DiceRollModifier::OneBonus
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_two_penalty() {
|
2020-11-04 20:09:39 +00:00
|
|
|
let result = parse_regular_roll("pp:60");
|
2020-10-31 20:29:55 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
DiceRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
amount: Amount {
|
2020-11-04 20:09:39 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
},
|
2020-10-31 20:29:55 +00:00
|
|
|
modifier: DiceRollModifier::TwoPenalty
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_one_penalty() {
|
2020-11-04 20:09:39 +00:00
|
|
|
let result = parse_regular_roll("p:60");
|
2020-10-31 20:29:55 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
DiceRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
amount: Amount {
|
2020-11-04 20:09:39 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
},
|
2020-10-31 20:29:55 +00:00
|
|
|
modifier: DiceRollModifier::OnePenalty
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:03:18 +00:00
|
|
|
#[test]
|
2021-05-13 20:24:17 +00:00
|
|
|
fn regular_roll_accepts_whitespace() {
|
2020-10-31 14:03:18 +00:00
|
|
|
assert!(parse_regular_roll("60 ").is_ok());
|
|
|
|
assert!(parse_regular_roll(" 60").is_ok());
|
|
|
|
assert!(parse_regular_roll(" 60 ").is_ok());
|
2020-10-31 20:29:55 +00:00
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
assert!(parse_regular_roll("bb:60 ").is_ok());
|
|
|
|
assert!(parse_regular_roll(" bb:60").is_ok());
|
|
|
|
assert!(parse_regular_roll(" bb:60 ").is_ok());
|
2020-10-31 20:29:55 +00:00
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
assert!(parse_regular_roll("b:60 ").is_ok());
|
|
|
|
assert!(parse_regular_roll(" b:60").is_ok());
|
|
|
|
assert!(parse_regular_roll(" b:60 ").is_ok());
|
2020-10-31 20:29:55 +00:00
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
assert!(parse_regular_roll("pp:60 ").is_ok());
|
|
|
|
assert!(parse_regular_roll(" pp:60").is_ok());
|
|
|
|
assert!(parse_regular_roll(" pp:60 ").is_ok());
|
2020-10-31 20:29:55 +00:00
|
|
|
|
2020-11-04 20:09:39 +00:00
|
|
|
assert!(parse_regular_roll("p:60 ").is_ok());
|
|
|
|
assert!(parse_regular_roll(" p:60").is_ok());
|
|
|
|
assert!(parse_regular_roll(" p:60 ").is_ok());
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn advancement_roll_accepts_whitespacen() {
|
|
|
|
assert!(parse_advancement_roll("60 ").is_ok());
|
|
|
|
assert!(parse_advancement_roll(" 60").is_ok());
|
|
|
|
assert!(parse_advancement_roll(" 60 ").is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn advancement_roll_accepts_single_number() {
|
|
|
|
let result = parse_advancement_roll("60");
|
|
|
|
assert!(result.is_ok());
|
2021-03-18 20:54:49 +00:00
|
|
|
assert_eq!(
|
|
|
|
AdvancementRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
existing_skill: Amount {
|
2021-03-18 20:54:49 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
2021-05-13 20:24:17 +00:00
|
|
|
}
|
2021-03-18 20:54:49 +00:00
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn advancement_roll_allows_big_numbers() {
|
|
|
|
assert!(parse_advancement_roll("3000").is_ok());
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-18 20:54:49 +00:00
|
|
|
fn advancement_roll_allows_variables() {
|
|
|
|
let result = parse_advancement_roll("abc");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
AdvancementRoll {
|
2021-05-13 20:24:17 +00:00
|
|
|
existing_skill: Amount {
|
2021-03-18 20:54:49 +00:00
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Variable(String::from("abc"))
|
2021-05-13 20:24:17 +00:00
|
|
|
}
|
2021-03-18 20:54:49 +00:00
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-13 20:24:17 +00:00
|
|
|
fn advancement_roll_rejects_complex_expressions() {
|
2021-03-18 20:54:49 +00:00
|
|
|
let result = parse_advancement_roll("3 + abc + bob - 4");
|
2021-05-13 20:24:17 +00:00
|
|
|
assert!(result.is_err());
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
2020-10-31 13:52:46 +00:00
|
|
|
}
|