pub fn parse(s: &str) -> Result<Vec<DiceTerm>, &'static str> {
        // Convert to lowercase and strip whitespace for simplicity in parsing.
        let mut s = s.to_lowercase();
        s.retain(|c| !c.is_whitespace());

        let dice_regex = Regex::new(r"(?x)
(?P<dice>(?P<count_sign>[+-])?(?P<count>\d+)?d(?P<sides>\d+)) # XdY, where X is optional and can be negative
|
(?P<constant>[+-]?\d+) # a constant offset, which can be negative
|
.+ # Any unrecognized characters (error)
").unwrap();

        dice_regex
            .captures_iter(&s)
            .map(|captures| {
                if let Some(_) = captures.name("dice") {
                    // Matches [count_sign][count]d[sides]

                    let mut count = captures
                        .name("count")
                        .map(|x| x.as_str())
                        .unwrap_or("1")
                        .to_owned();
                    // Prepend the sign if present.
                    if let Some(sign) = captures.name("count_sign") {
                        count.insert_str(0, sign.as_str());
                    }
                    // Convert to an integer now that we've accounted for optional values.
                    let count: i32 = count.parse().unwrap();

                    let sides = captures
                        .name("sides")
                        .unwrap()
                        .as_str()
                        .parse()
                        .unwrap();
                    Ok(DiceTerm::Dice { count, sides })
                } else if let Some(constant) = captures.name("constant") {
                    // Matches [constant]
                    Ok(DiceTerm::Constant(constant.as_str().parse().unwrap()))
                } else {
                    Err("Could not parse dice expression.")
                }
            })
            .collect()
    }
/// Parses the given string as dice terms. Returns an error message if any value cannot be parsed.
Parses the given string as dice terms. Returns an error message if any value cannot be parsed.
Parses the given string as dice terms . Returns an error message if any value cannot be parsed .
pub fn parse(s: &str) -> Result<Vec<DiceTerm>, &'static str> {
        // Convert to lowercase and strip whitespace for simplicity in parsing.
        let mut s = s.to_lowercase();
        s.retain(|c| !c.is_whitespace());
        let dice_regex = Regex::new(r"(?x)
(?P<dice>(?P<count_sign>[+-])?(?P<count>\d+)?d(?P<sides>\d+)) # XdY, where X is optional and can be negative
|
(?P<constant>[+-]?\d+) # a constant offset, which can be negative
|
.+ # Any unrecognized characters (error)
").unwrap();
        dice_regex
            .captures_iter(&s)
            .map(|captures| {
                if let Some(_) = captures.name("dice") {
                    // Matches [count_sign][count]d[sides]
                    let mut count = captures
                        .name("count")
                        .map(|x| x.as_str())
                        .unwrap_or("1")
                        .to_owned();
                    // Prepend the sign if present.
                    if let Some(sign) = captures.name("count_sign") {
                        count.insert_str(0, sign.as_str());
                    }
                    // Convert to an integer now that we've accounted for optional values.
                    let count: i32 = count.parse().unwrap();
                    let sides = captures
                        .name("sides")
                        .unwrap()
                        .as_str()
                        .parse()
                        .unwrap();
                    Ok(DiceTerm::Dice { count, sides })
                } else if let Some(constant) = captures.name("constant") {
                    // Matches [constant]
                    Ok(DiceTerm::Constant(constant.as_str().parse().unwrap()))
                } else {
                    Err("Could not parse dice expression.")
                }
            })
            .collect()
    }
pub fn parse ( s : & str ) -> Result < Vec < DiceTerm > , & ' static str > { // Convert to lowercase and strip whitespace for simplicity in parsing. let mut s = s . to_lowercase ( ) ; s . retain ( | c | ! c . is_whitespace ( ) ) ; let dice_regex = Regex :: new ( r"(?x)
(?P<dice>(?P<count_sign>[+-])?(?P<count>\d+)?d(?P<sides>\d+)) # XdY, where X is optional and can be negative
|
(?P<constant>[+-]?\d+) # a constant offset, which can be negative
|
.+ # Any unrecognized characters (error)
" ) . unwrap ( ) ; dice_regex . captures_iter ( & s ) . map ( | captures | { if let Some ( _ ) = captures . name ( " " ) { // Matches [count_sign][count]d[sides] let mut count = captures . name ( " " ) . map ( | x | x . as_str ( ) ) . unwrap_or ( " " ) . to_owned ( ) ; // Prepend the sign if present. if let Some ( sign ) = captures . name ( " " ) { count . insert_str ( 0 , sign . as_str ( ) ) ; } // Convert to an integer now that we've accounted for optional values. let count : i32 = count . parse ( ) . unwrap ( ) ; let sides = captures . name ( " " ) . unwrap ( ) . as_str ( ) . parse ( ) . unwrap ( ) ; Ok ( DiceTerm :: Dice { count , sides } ) } else if let Some ( constant ) = captures . name ( " " ) { // Matches [constant] Ok ( DiceTerm :: Constant ( constant . as_str ( ) . parse ( ) . unwrap ( ) ) ) } else { Err ( " " ) } } ) . collect ( ) }
Parses the given string as dice terms.
Parses the given string as dice terms .
s
&str
s
&str