Module: Qi::Styles

Defined in:
lib/qi/styles.rb

Overview

Pure validation function for player styles.

A style is a String label denoting a movement tradition or game family (e.g., "C", "S", "X"). Semantic validation (e.g., SIN compliance) is the responsibility of the encoding layer (FEEN, PON, etc.).

Examples:

Validate a style

Qi::Styles.validate(:first, "C") #=> "C"

Constant Summary collapse

MAX_STYLE_BYTESIZE =
255

Class Method Summary collapse

Class Method Details

.validate(side, style) ⇒ String

Validates a single player style and returns it.

The style must not be nil, must be a String, and must not exceed MAX_STYLE_BYTESIZE bytes. The validated value is returned as-is (no coercion, no allocation).

Examples:

Valid style

Qi::Styles.validate(:first, "C") #=> "C"

Nil style

Qi::Styles.validate(:first, nil)
# => ArgumentError: first player style must not be nil

Non-string style

Qi::Styles.validate(:second, :chess)
# => ArgumentError: second player style must be a String

Oversized style

Qi::Styles.validate(:first, "A" * 256)
# => ArgumentError: first player style exceeds 255 bytes

Parameters:

  • side (Symbol) —

    :first or :second, used in error messages.

  • style (Object) —

    the style value to validate.

Returns:

  • (String) —

    the validated style.

Raises:

  • (ArgumentError) —

    if the style is nil or not a String.

  • (ArgumentError) —

    if the style exceeds MAX_STYLE_BYTESIZE bytes.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qi/styles.rb', line 42

def self.validate(side, style)
  if style.nil?
    raise ::ArgumentError, "#{side} player style must not be nil"
  end

  unless style.is_a?(::String)
    raise ::ArgumentError, "#{side} player style must be a String"
  end

  if style.bytesize > MAX_STYLE_BYTESIZE
    raise ::ArgumentError, "#{side} player style exceeds #{MAX_STYLE_BYTESIZE} bytes"
  end

  style
end