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)?;
|
|
|
|
|
2021-03-18 20:54:49 +00:00
|
|
|
Ok(DiceRoll { modifier, amounts })
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_advancement_roll(input: &str) -> Result<AdvancementRoll, DiceParsingError> {
|
|
|
|
let input = input.trim();
|
2021-03-18 20:54:49 +00:00
|
|
|
let amounts = crate::parser::parse_amounts(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::*;
|
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());
|
2021-03-18 20:54:49 +00:00
|
|
|
assert_eq!(
|
|
|
|
AdvancementRoll {
|
|
|
|
existing_skill: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(60)
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
existing_skill: vec![Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Variable(String::from("abc"))
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-18 20:54:49 +00:00
|
|
|
fn advancement_roll_allows_complex_expressions() {
|
|
|
|
let result = parse_advancement_roll("3 + abc + bob - 4");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(
|
|
|
|
AdvancementRoll {
|
|
|
|
existing_skill: vec![
|
|
|
|
Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Number(3)
|
|
|
|
},
|
|
|
|
Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Variable(String::from("abc"))
|
|
|
|
},
|
|
|
|
Amount {
|
|
|
|
operator: Operator::Plus,
|
|
|
|
element: Element::Variable(String::from("bob"))
|
|
|
|
},
|
|
|
|
Amount {
|
|
|
|
operator: Operator::Minus,
|
|
|
|
element: Element::Number(4)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
result.unwrap()
|
|
|
|
);
|
2020-10-31 14:03:18 +00:00
|
|
|
}
|
2020-10-31 13:52:46 +00:00
|
|
|
}
|