feen 5.0.0.beta8 → 5.0.0.beta10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Feen
4
- module Parser
5
- # Handles parsing of the games turn section of a FEEN string
6
- module GamesTurn
7
- # Error messages for games turn parsing
8
- ERRORS = {
9
- invalid_type: "Games turn must be a string, got %s",
10
- empty_string: "Games turn string cannot be empty",
11
- invalid_format: "Invalid games turn format. Expected format: UPPERCASE/lowercase or lowercase/UPPERCASE"
12
- }.freeze
13
-
14
- # Pattern matching the FEEN specification for games turn
15
- # <games-turn> ::= <game-id-uppercase> "/" <game-id-lowercase>
16
- # | <game-id-lowercase> "/" <game-id-uppercase>
17
- VALID_GAMES_TURN_PATTERN = %r{
18
- \A # Start of string
19
- (?: # Non-capturing group for alternatives
20
- (?<uppercase_first>[A-Z]+) # Named group: uppercase identifier first
21
- / # Separator
22
- (?<lowercase_second>[a-z]+) # Named group: lowercase identifier second
23
- | # OR
24
- (?<lowercase_first>[a-z]+) # Named group: lowercase identifier first
25
- / # Separator
26
- (?<uppercase_second>[A-Z]+) # Named group: uppercase identifier second
27
- )
28
- \z # End of string
29
- }x
30
-
31
- # Parses the games turn section of a FEEN string
32
- #
33
- # @param games_turn_str [String] FEEN games turn string
34
- # @return [Array<String>] Array containing [active_player, inactive_player]
35
- # @raise [ArgumentError] If the input string is invalid
36
- #
37
- # @example Valid games turn string with uppercase first
38
- # GamesTurn.parse("CHESS/shogi")
39
- # # => ["CHESS", "shogi"]
40
- #
41
- # @example Valid games turn string with lowercase first
42
- # GamesTurn.parse("chess/SHOGI")
43
- # # => ["chess", "SHOGI"]
44
- def self.parse(games_turn_str)
45
- validate_input_type(games_turn_str)
46
-
47
- match = VALID_GAMES_TURN_PATTERN.match(games_turn_str)
48
- raise ::ArgumentError, ERRORS[:invalid_format] unless match
49
-
50
- extract_game_identifiers(match)
51
- end
52
-
53
- # Validates that the input is a non-empty string
54
- #
55
- # @param str [String] Input string to validate
56
- # @raise [ArgumentError] If input is not a string or is empty
57
- # @return [void]
58
- private_class_method def self.validate_input_type(str)
59
- raise ::ArgumentError, format(ERRORS[:invalid_type], str.class) unless str.is_a?(::String)
60
- raise ::ArgumentError, ERRORS[:empty_string] if str.empty?
61
- end
62
-
63
- # Extracts game identifiers from regexp match captures
64
- #
65
- # @param match [MatchData] Regexp match data with named captures
66
- # @return [Array<String>] Array containing [active_player, inactive_player]
67
- private_class_method def self.extract_game_identifiers(match)
68
- captures = match.named_captures
69
-
70
- if captures["uppercase_first"]
71
- [captures["uppercase_first"], captures["lowercase_second"]]
72
- else
73
- [captures["lowercase_first"], captures["uppercase_second"]]
74
- end
75
- end
76
- end
77
- end
78
- end