2020-10-31 14:03:18 +00:00
|
|
|
use super::dice::{AdvancementRoll, DiceRoll, DiceRollModifier};
|
|
|
|
use crate::parser::DiceParsingError;
|
|
|
|
|
|
|
|
//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)?;
|
|
|
|
let amounts = crate::parser::parse_amounts(amounts_str)?;
|
|
|
|
|
|
|
|
Ok(DiceRoll {
|
|
|
|
amounts: amounts,
|
|
|
|
modifier: modifier,
|
|
|
|
})
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_advancement_roll(input: &str) -> Result<AdvancementRoll, DiceParsingError> {
|
|
|
|
let input = input.trim();
|
|
|
|
let target: u32 = input.parse().map_err(|_| DiceParsingError::InvalidAmount)?;
|
|
|
|
|
|
|
|
if target <= 100 {
|
|
|
|
Ok(AdvancementRoll {
|
|
|
|
existing_skill: target,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(DiceParsingError::InvalidAmount)
|
|
|
|
}
|
2020-10-31 13:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:03:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2020-11-04 20:09:39 +00:00
|
|
|
use crate::parser::{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 {
|
2020-11-04 20:09:39 +00:00
|
|
|
amounts: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}],
|
2020-10-31 14:03:18 +00:00
|
|
|
modifier: DiceRollModifier::Normal
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-04 20:09:39 +00:00
|
|
|
amounts: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}],
|
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 {
|
2020-11-04 20:09:39 +00:00
|
|
|
amounts: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}],
|
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 {
|
2020-11-04 20:09:39 +00:00
|
|
|
amounts: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}],
|
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 {
|
2020-11-04 20:09:39 +00:00
|
|
|
amounts: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}],
|
2020-10-31 20:29:55 +00:00
|
|
|
modifier: DiceRollModifier::OnePenalty
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:03:18 +00:00
|
|
|
#[test]
|
|
|
|
fn regular_roll_accepts_whitespacen() {
|
|
|
|
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());
|
|
|
|
assert_eq!(AdvancementRoll { existing_skill: 60 }, result.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn advancement_roll_rejects_big_numbers() {
|
|
|
|
assert!(parse_advancement_roll("3000").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn advancement_roll_rejects_invalid_input() {
|
|
|
|
assert!(parse_advancement_roll("abc").is_err());
|
|
|
|
}
|
2020-10-31 13:52:46 +00:00
|
|
|
}
|