sashite-pmn 1.0.0 → 1.1.0
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.
- checksums.yaml +4 -4
- data/lib/sashite/pmn/action.rb +9 -9
- data/lib/sashite/pmn/error.rb +44 -11
- data/lib/sashite/pmn/move.rb +10 -10
- data/lib/sashite/pmn.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a51482a92eec36293c9fe583574f7d4981bd953f36846459515ac1ba58a37dc6
|
4
|
+
data.tar.gz: 01a42bd2bc3ca44550d681573b96d821f67c58cacc2df0dd5b7bccc2a4f4801f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b9775836efcc6db95dada230c631d78f6918c0efc3c9692f04c2d5a7d0c1041e90b390023d49fcb4e75ca6e166dc3af6f6821fa0563e54994a101e607f39e32
|
7
|
+
data.tar.gz: 7d46c954a1e4a1c696a6f9cd222dc80785b6c8a54b0c26210e0045c8ee8671562262e2074c2b14612e21fb24a359bd301d4422bceff73b8c80748c353b1fa6ba
|
data/lib/sashite/pmn/action.rb
CHANGED
@@ -30,8 +30,8 @@ module Sashite
|
|
30
30
|
# @param source [String] CELL or "*"
|
31
31
|
# @param destination [String] CELL or "*"
|
32
32
|
# @param piece [String, nil] QPI string (optional)
|
33
|
-
# @raise [
|
34
|
-
# @raise [
|
33
|
+
# @raise [Error::Location] if source/destination are invalid
|
34
|
+
# @raise [Error::Piece] if piece is provided but invalid
|
35
35
|
def initialize(source, destination, piece = nil)
|
36
36
|
validate_source!(source)
|
37
37
|
validate_destination!(destination)
|
@@ -115,7 +115,7 @@ module Sashite
|
|
115
115
|
end
|
116
116
|
|
117
117
|
# @param other [Object]
|
118
|
-
# @return [Boolean] equality by
|
118
|
+
# @return [Boolean] equality by source, destination, piece
|
119
119
|
def ==(other)
|
120
120
|
return false unless other.is_a?(Action)
|
121
121
|
|
@@ -156,28 +156,28 @@ module Sashite
|
|
156
156
|
# ---------- Internal validation helpers -------------------------------
|
157
157
|
|
158
158
|
# @param src [String]
|
159
|
-
# @raise [
|
159
|
+
# @raise [Error::Location]
|
160
160
|
def validate_source!(src)
|
161
161
|
return if valid_location?(src)
|
162
162
|
|
163
|
-
raise
|
163
|
+
raise Error::Location, "Invalid source location: #{src.inspect}"
|
164
164
|
end
|
165
165
|
|
166
166
|
# @param dst [String]
|
167
|
-
# @raise [
|
167
|
+
# @raise [Error::Location]
|
168
168
|
def validate_destination!(dst)
|
169
169
|
return if valid_location?(dst)
|
170
170
|
|
171
|
-
raise
|
171
|
+
raise Error::Location, "Invalid destination location: #{dst.inspect}"
|
172
172
|
end
|
173
173
|
|
174
174
|
# @param qpi [String, nil]
|
175
|
-
# @raise [
|
175
|
+
# @raise [Error::Piece]
|
176
176
|
def validate_piece!(qpi)
|
177
177
|
return if qpi.nil?
|
178
178
|
return if Qpi.valid?(qpi)
|
179
179
|
|
180
|
-
raise
|
180
|
+
raise Error::Piece, "Invalid piece QPI format: #{qpi.inspect}"
|
181
181
|
end
|
182
182
|
|
183
183
|
# @param location [String]
|
data/lib/sashite/pmn/error.rb
CHANGED
@@ -2,19 +2,52 @@
|
|
2
2
|
|
3
3
|
module Sashite
|
4
4
|
module Pmn
|
5
|
-
# Base
|
6
|
-
|
5
|
+
# Base error namespace for PMN.
|
6
|
+
#
|
7
|
+
# Usage patterns:
|
8
|
+
# rescue Sashite::Pmn::Error => e
|
9
|
+
# rescue Sashite::Pmn::Error::Move
|
10
|
+
# rescue Sashite::Pmn::Error::Location, Sashite::Pmn::Error::Piece
|
11
|
+
class Error < ::StandardError
|
12
|
+
# Raised when a PMN move (sequence) is malformed or invalid.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# begin
|
16
|
+
# Sashite::Pmn::Move.new("e2") # Incomplete action
|
17
|
+
# rescue Sashite::Pmn::Error::Move => e
|
18
|
+
# warn "Invalid move sequence: #{e.message}"
|
19
|
+
# end
|
20
|
+
class Move < Error; end
|
7
21
|
|
8
|
-
|
9
|
-
|
22
|
+
# Raised when an atomic action is malformed or fails validation.
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# begin
|
26
|
+
# Sashite::Pmn::Action.new("invalid", "e4", "C:P")
|
27
|
+
# rescue Sashite::Pmn::Error::Action => e
|
28
|
+
# warn "Invalid atomic action: #{e.message}"
|
29
|
+
# end
|
30
|
+
class Action < Error; end
|
10
31
|
|
11
|
-
|
12
|
-
|
32
|
+
# Raised when a location is neither a valid CELL coordinate nor HAND ("*").
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# begin
|
36
|
+
# Sashite::Pmn::Action.new("ZZ99", "e4", "C:P")
|
37
|
+
# rescue Sashite::Pmn::Error::Location => e
|
38
|
+
# warn "Invalid location: #{e.message}"
|
39
|
+
# end
|
40
|
+
class Location < Action; end
|
13
41
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
42
|
+
# Raised when a piece identifier is not valid QPI format.
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
# begin
|
46
|
+
# Sashite::Pmn::Action.new("e2", "e4", "NotQPI")
|
47
|
+
# rescue Sashite::Pmn::Error::Piece => e
|
48
|
+
# warn "Invalid piece QPI: #{e.message}"
|
49
|
+
# end
|
50
|
+
class Piece < Action; end
|
51
|
+
end
|
19
52
|
end
|
20
53
|
end
|
data/lib/sashite/pmn/move.rb
CHANGED
@@ -23,7 +23,7 @@ module Sashite
|
|
23
23
|
# Create a Move from PMN elements (variadic only).
|
24
24
|
#
|
25
25
|
# @param pmn_elements [Array<String>] passed as individual args
|
26
|
-
# @raise [
|
26
|
+
# @raise [Error::Move] if called with a single Array or if invalid
|
27
27
|
#
|
28
28
|
# @example
|
29
29
|
# Move.new("e2","e4","C:P")
|
@@ -31,7 +31,7 @@ module Sashite
|
|
31
31
|
def initialize(*pmn_elements)
|
32
32
|
# single-array form is intentionally not supported (entropy reduction)
|
33
33
|
if pmn_elements.size == 1 && pmn_elements.first.is_a?(Array)
|
34
|
-
raise
|
34
|
+
raise Error::Move,
|
35
35
|
'PMN must be passed as individual arguments, e.g. Move.new("e2","e4","C:P")'
|
36
36
|
end
|
37
37
|
|
@@ -170,14 +170,14 @@ module Sashite
|
|
170
170
|
# Validation ------------------------------------------------------------
|
171
171
|
|
172
172
|
def validate_array!(array)
|
173
|
-
raise
|
174
|
-
raise
|
173
|
+
raise Error::Move, "PMN must be an array, got #{array.class}" unless array.is_a?(Array)
|
174
|
+
raise Error::Move, "PMN array cannot be empty" if array.empty?
|
175
175
|
|
176
|
-
raise
|
176
|
+
raise Error::Move, "All PMN elements must be strings" unless array.all?(String)
|
177
177
|
|
178
178
|
return if valid_length?(array)
|
179
179
|
|
180
|
-
raise
|
180
|
+
raise Error::Move, "Invalid PMN array length: #{array.size}"
|
181
181
|
end
|
182
182
|
|
183
183
|
# Valid lengths: (size % 3 == 0) OR (size % 3 == 2), minimum 2.
|
@@ -204,21 +204,21 @@ module Sashite
|
|
204
204
|
actions << Action.new(array[index], array[index + 1], array[index + 2])
|
205
205
|
index += 3
|
206
206
|
else
|
207
|
-
raise
|
207
|
+
raise Error::Move, "Invalid action group at index #{index}"
|
208
208
|
end
|
209
209
|
end
|
210
210
|
|
211
211
|
actions
|
212
|
-
rescue
|
212
|
+
rescue Error::Action => e
|
213
213
|
# Normalize action-level errors as move-level errors during parsing
|
214
|
-
raise
|
214
|
+
raise Error::Move, "Invalid action while parsing move at index #{index}: #{e.message}"
|
215
215
|
end
|
216
216
|
|
217
217
|
def validate_actions!
|
218
218
|
actions.each_with_index do |action, i|
|
219
219
|
next if action.valid?
|
220
220
|
|
221
|
-
raise
|
221
|
+
raise Error::Move, "Invalid action at position #{i}: #{action.inspect}"
|
222
222
|
end
|
223
223
|
end
|
224
224
|
end
|
data/lib/sashite/pmn.rb
CHANGED
@@ -27,12 +27,12 @@ module Sashite
|
|
27
27
|
#
|
28
28
|
# @param pmn_array [Array<String>] flat array of PMN elements
|
29
29
|
# @return [Sashite::Pmn::Move]
|
30
|
-
# @raise [Sashite::Pmn::
|
30
|
+
# @raise [Sashite::Pmn::Error::Move] if the array or any action is invalid
|
31
31
|
#
|
32
32
|
# @example
|
33
33
|
# Sashite::Pmn.parse(["e2","e4","C:P"]).actions.size # => 1
|
34
34
|
def self.parse(pmn_array)
|
35
|
-
raise
|
35
|
+
raise Error::Move, "PMN must be an array, got #{pmn_array.class}" unless pmn_array.is_a?(Array)
|
36
36
|
|
37
37
|
Move.new(*pmn_array)
|
38
38
|
end
|