# `Sashite.Pon`
[🔗](https://github.com/sashite/pon.ex/blob/v1.0.0/lib/sashite/pon.ex#L1)

PON (Position Object Notation) implementation for Elixir.

PON encodes a Position of a two-player, turn-based board game as a JSON
object with four fields: `board`, `hands`, `styles` and `turn`. This
library converts PON documents to and from `Qi` positions, the model that
`Sashite.Feen` also uses, so a position can move between the two formats.

See the [PON Specification v1.0.0](https://sashite.dev/specs/pon/1.0.0/).

## Examples

    iex> {:ok, position} =
    ...>   Sashite.Pon.parse(~s({
    ...>     "board": [["k^", null], [null, "K^"]],
    ...>     "hands": {"first": ["P"], "second": []},
    ...>     "styles": {"first": "W", "second": "w"},
    ...>     "turn": "first"
    ...>   }))
    iex> position.shape
    [2, 2]
    iex> position.first_player_hand
    %{"P" => 1}
    iex> Sashite.Pon.dump(position)
    ~s({"board":[["k^",null],[null,"K^"]],"hands":{"first":["P"],"second":[]},"styles":{"first":"W","second":"w"},"turn":"first"})

    iex> Sashite.Pon.valid?(~s({"board": [], "hands": {"first": [], "second": []}, "styles": {"first": "W", "second": "w"}, "turn": "first"}))
    false

# `dump`

```elixir
@spec dump(Qi.t()) :: String.t()
```

Serializes a `Qi` position into a compact PON document.

Hand items are written in byte order of their EPIN tokens, so that a
position has a single serialization. A `Qi` position can hold any string,
so the position is checked first: every piece, on the board or in a hand,
must be an EPIN token, the first player style an uppercase SIN token and
the second player style a lowercase one.

## Examples

    iex> Qi.new([4], first_player_style: "W", second_player_style: "w")
    ...> |> Qi.board_diff([{0, "K^"}, {3, "k^"}])
    ...> |> Qi.second_player_hand_diff([{"q", 1}, {"P", 1}])
    ...> |> Qi.toggle()
    ...> |> Sashite.Pon.dump()
    ~s({"board":["K^",null,null,"k^"],"hands":{"first":[],"second":["P","q"]},"styles":{"first":"W","second":"w"},"turn":"second"})

    iex> Qi.new([2], first_player_style: "W", second_player_style: "w")
    ...> |> Qi.board_diff([{1, "W:K"}])
    ...> |> Sashite.Pon.dump()
    ** (ArgumentError) square 1 holds "W:K", which is not an EPIN token

## Errors

Raises `ArgumentError` if the position holds a piece or a style that PON
cannot encode.

# `max_string_length`

```elixir
@spec max_string_length() :: pos_integer()
```

Returns the maximum size of a PON document, in bytes.

Larger inputs are rejected before they are decoded. The limit leaves room
for the largest board that `Qi` can hold (65,025 squares) in an indented
document.

## Examples

    iex> Sashite.Pon.max_string_length()
    1_048_576

# `parse`

```elixir
@spec parse(String.t()) :: {:ok, Qi.t()} | {:error, atom()}
```

Parses a PON document into a `Qi` position.

Returns `{:ok, position}`, or `{:error, reason}` where `reason` is an atom
naming the first check that failed:

| Reason | Cause |
|--------|-------|
| `:not_a_string` | The input is not a binary |
| `:input_too_long` | The input exceeds `max_string_length/0` bytes |
| `:invalid_json` | The input is not a JSON text (RFC 8259) |
| `:duplicate_key` | An object has two members with the same name |
| `:not_an_object` | The root is not a JSON object |
| `:missing_property` | `board`, `hands`, `styles` or `turn` is missing |
| `:unknown_property` | The root has a property other than these four |
| `:invalid_board` | `board` is not an array |
| `:exceeds_max_dimensions` | The board has more than 3 dimensions |
| `:dimension_size_exceeds_limit` | A dimension has more than 255 elements |
| `:too_many_squares` | The board has more than 65,025 squares |
| `:dimensional_coherence_violation` | The board is not rectangular |
| `:empty_board` | The board has no square |
| `:invalid_hands` | `hands` is not an object with the arrays `first` and `second` only |
| `:invalid_piece_token` | A square is neither `null` nor an EPIN token, or a hand item is not an EPIN token |
| `:invalid_styles` | `styles` is not an object with `first` and `second` only |
| `:invalid_style_token` | `styles.first` is not an uppercase SIN token, or `styles.second` not a lowercase one |
| `:invalid_turn` | `turn` is neither `"first"` nor `"second"` |
| `:too_many_pieces` | There are more pieces than squares |

## Examples

    iex> {:ok, position} =
    ...>   Sashite.Pon.parse(~s({
    ...>     "board": [null, "P", null],
    ...>     "hands": {"first": [], "second": ["p", "p"]},
    ...>     "styles": {"first": "W", "second": "w"},
    ...>     "turn": "second"
    ...>   }))
    iex> position.board
    {nil, "P", nil}
    iex> position.second_player_hand
    %{"p" => 2}
    iex> position.turn
    :second

    iex> Sashite.Pon.parse(~s({
    ...>   "board": [["a", "b"], ["c"]],
    ...>   "hands": {"first": [], "second": []},
    ...>   "styles": {"first": "W", "second": "w"},
    ...>   "turn": "first"
    ...> }))
    {:error, :dimensional_coherence_violation}

    iex> Sashite.Pon.parse(~s({"turn": "first", "turn": "second"}))
    {:error, :duplicate_key}

# `parse!`

```elixir
@spec parse!(String.t()) :: Qi.t()
```

Parses a PON document into a `Qi` position, raising `ArgumentError` if it
is not valid.

## Examples

    iex> position =
    ...>   Sashite.Pon.parse!(~s({
    ...>     "board": [null],
    ...>     "hands": {"first": [], "second": []},
    ...>     "styles": {"first": "C", "second": "c"},
    ...>     "turn": "first"
    ...>   }))
    iex> position.shape
    [1]

    iex> Sashite.Pon.parse!("[]")
    ** (ArgumentError) invalid PON document: not_an_object

# `valid?`

```elixir
@spec valid?(term()) :: boolean()
```

Reports whether the input is a valid PON document.

Never raises: any term other than a valid PON document returns `false`.

## Examples

    iex> Sashite.Pon.valid?(~s({
    ...>   "board": [[null, null], [null, null]],
    ...>   "hands": {"first": [], "second": []},
    ...>   "styles": {"first": "J", "second": "s"},
    ...>   "turn": "first"
    ...> }))
    true

    iex> Sashite.Pon.valid?(nil)
    false

---

*Consult [api-reference.md](api-reference.md) for complete listing*
