Skip to content

Commit

Permalink
feat: handle single apostophe similarly to asterisk in the hand parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Iipin committed Jan 13, 2024
1 parent 37c9d3b commit ddc7c53
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const SPECIAL_HATSU: char = 'g';
const SPECIAL_CHUN: char = 'r';
const SPECIAL_ANY: char = '?';

const POSITION_MODIFIER: char = '*';
const POSITION_MODIFIER_ASTERISK: char = '*';
const POSITION_MODIFIER_APOSTROPHE: char = '\'';
const GROUP_SEPARATOR: char = '_';

#[derive(Debug)]
Expand All @@ -41,8 +42,8 @@ impl HandParser {
/// tile value prefixes
/// * `E`, `S`, `W`, `N` - winds
/// * `w`, `g`, `r` - dragons
/// * `*` or `**` - tile value prefix that means that a tile is rotated
/// (single asterisk) or rotated and shifted (double asterisk)
/// * `*` or `'` - tile value prefix that means that a tile is rotated.
/// Repeat twice to rotate and shift
/// * `_` - tile group separator
///
/// # Examples
Expand Down Expand Up @@ -111,7 +112,9 @@ impl HandParser {
SUITE_MANZU | SUITE_PINZU | SUITE_SOUZU | SUITE_HONOR => self.handle_suite(char),
SPECIAL_TON | SPECIAL_NAN | SPECIAL_SHAA | SPECIAL_PEI | SPECIAL_HAKU
| SPECIAL_HATSU | SPECIAL_CHUN | SPECIAL_ANY => self.handle_special_symbol(char),
POSITION_MODIFIER => self.handle_position_modifier(),
POSITION_MODIFIER_ASTERISK | POSITION_MODIFIER_APOSTROPHE => {
self.handle_position_modifier()
}
GROUP_SEPARATOR => self.handle_group_separator(),
_ => Err(HandParseErrorType::InvalidCharacter),
};
Expand Down Expand Up @@ -403,6 +406,34 @@ mod tests {
HandTile::new(TON, TilePlacement::Normal),
]
);

let hand = HandParser::parse("11'1''1m");
assert!(hand.is_ok());
let hand = hand.unwrap();
assert_eq!(hand.groups().len(), 1);
assert_eq!(
hand.hand_tiles().collect::<Vec<HandTile>>(),
vec![
HandTile::new(II_MAN, TilePlacement::Normal),
HandTile::new(II_MAN, TilePlacement::Rotated),
HandTile::new(II_MAN, TilePlacement::RotatedAndShifted),
HandTile::new(II_MAN, TilePlacement::Normal),
]
);

let hand = HandParser::parse("EE'E*'E");
assert!(hand.is_ok());
let hand = hand.unwrap();
assert_eq!(hand.groups().len(), 1);
assert_eq!(
hand.hand_tiles().collect::<Vec<HandTile>>(),
vec![
HandTile::new(TON, TilePlacement::Normal),
HandTile::new(TON, TilePlacement::Rotated),
HandTile::new(TON, TilePlacement::RotatedAndShifted),
HandTile::new(TON, TilePlacement::Normal),
]
);
}

#[test]
Expand Down

0 comments on commit ddc7c53

Please sign in to comment.