Keep/Drop Function #92
No reviewers
Labels
No Label
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: projectmoon/tenebrous-dicebot#92
Loading…
Reference in New Issue
No description provided.
Delete Branch "kg333/tenebrous-dicebot:keep_drop_function"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This adds a keep/drop function to basic dice rolls, allowing D&D 5e advantage/disadvantage respectively.
Thank you for your pull request! I have made some comments. The most important one at the moment is getting rid of the
unwrap()
calls so that errors are propagated up the chain instead of potentially causing a panic (though not sure how possible that actually is). The remainder are currently stylistic.Some questions we need to think about:
keep
anddrop
variables on the struct into an enum so that only can be present.d
to represent the drop, mostly because thed
is also used for the die roll itself. But I also can't think of a better letter/word to use for indicating the drop.@ -37,0 +36,4 @@
// parse main dice expression
let (mut input, (count, _, sides)) = tuple((digit1, tag("d"), digit1))(input)?;
// check for keep expression (i.e. D&D 5E Advantage)
I think it is better to document what the keep expression actually is here, so tacking on "1dXkY" would be useful.
And since pattern matches are expressions, I think we can rewrite these two match blocks. Something like this:
The same should be doable for the drop expression. I think this way you can also get rid of the
mut
on input, becauselet
in rust is not just a variable assignment. It actually creates a whole new variable binding.Note: I haven't tested the above, but it should work. Or some variation of it should work.
@ -37,0 +63,4 @@
let count: u32 = count.parse().unwrap();
// don't allow keep greater than number of dice, and don't allow keep zero
let mut keep: u32 = keep.parse().unwrap();
Instead of
unwrap
, you should make use of the features of the Result type. While you will see examples of Rust code around the internet littered with unwrap, you don't actually want to use it except under a few circumstances:In this case, might be best to combine parsing and validation:
This will send up parsing errors back up the call chain and keep the boundary checks in place for a successful parse.
This comment also apples to the
drop
parsing.This is proving more difficult than I anticipated: it looks like nom is using its own error type? Not sure how to resolve this one, although I agree unwrap is a problem.
After looking at it further, the weird error type is irrelevant. Every possible arm of the match at this point either assigns Drop or Keep, or leaves a default Keep = Count if the parse fails.
@ -20,3 +20,3 @@
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DiceRoll(pub Vec<u32>);
// array of rolls in order, how many dice to keep, and how many to drop
Add a third
/
to get the Rustdoc working. It may also be good to more thoroughly describe what keep and drop mean here, namely that they mean keeping the highest or dropping the highest rolls.I'll give those changes a stab. While in theory you could have both "drop highest" and "keep highest i.e. drop lowest" on the same roll, I didn't provide a way to enter it, so an enum make sense here.
Regarding 'd' for drop, I was intending to match Roll20, but on second glance, it uses 'd' for dropping lowest dice, not highest dice. Roll20 instead uses "dh" for the function I'm trying to implement here.
Alternatively, Avrae uses 'p' to indicate dropped dice, with "ph" indicating dropped highest. It also has a native advantage/disadvantage call, but that works somewhat differently.
Thinking a change to "dh" for now, thoughts?
EDIT: Leaving it for tonight, but still need to do the enum conversion.
Enum conversion is complete for dice.rs. I left the separate usizes on DiceRoll in place though - I couldn't see a way to update it without complicating DiceRoll::total unnecessarily.
Bot has been rebuilt and function look OK.
@ -18,2 +19,3 @@
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}d{}", self.count, self.sides)
match self.keep_drop {
KeepOrDrop::Keep(keep) => {
Since you have if-else clauses here, it might make more sense to have a third enum member in addition to Keep and Drop, which indicates that we do nothing special with the roll.
Added another enum member called None and it's much cleaner. Although members with no type give my C-programmer soul the heebie-jeebies.
@ -37,0 +39,4 @@
// check for keep expression to keep highest dice (2d20k1)
let (keep, input) = match tuple::<&str, _, (_, _), _>((tag("k"), digit1))(input) {
// if ok, keep expression is present
Ok(r) => (r.1.1, r.0),
Rust supports tuple destructuring in pattern matching, which may be useful here and the other match:
rest
being the rest of the input in this case.Ooh, that's much more human-readable, thanks!
@ -37,0 +58,4 @@
let keep_drop = match keep.parse::<u32>() {
// Ok, there's a keep value, check and create Keep
Ok(i) => {
if i > count || i == 0 {
You can make this a bit more concise with yet another match expression:
This is mostly a stylistic choice though.
Merging as it is now, with some immediate refactorings to follow up.