# `Angelus.Spice.WorkerProtocol`
[🔗](https://github.com/MonsignorEduardo/angelus/blob/v0.0.2/lib/angelus/spice/worker_protocol.ex#L1)

Encodes requests and decodes responses for the `spice_worker` Port protocol.

Frame format (matching Erlang `packet: 4`):
  [4 bytes big-endian uint32: payload length][payload: UTF-8 JSON]

Elixir opens the Port with `packet: 4`, so the VM handles the framing
automatically.  This module only deals with the JSON payload layer.

Request schema:
  %{"id" => integer, "op" => string, ...op-specific fields...}

Response schema (success):
  %{"id" => integer, "ok" => true, "result" => term}

Response schema (error):
  %{"id" => integer, "ok" => false, "error" => string}

# `request_id`

```elixir
@type request_id() :: non_neg_integer()
```

# `coerce_state`

```elixir
@spec coerce_state(map()) :: {:ok, map()} | {:error, :invalid_state_result}
@spec coerce_state(term()) :: {:error, :invalid_state_result}
```

Coerces a `state` result map (string keys from JSON) into an Elixir map
with atom keys and typed values.

# `decode`

```elixir
@spec decode(binary()) ::
  {:ok, request_id(), term()}
  | {:error, request_id(), term()}
  | {:error, :decode_error, binary()}
@spec decode(term()) :: {:error, :decode_error, term()}
```

Decodes a raw binary response from the worker.

Returns `{:ok, id, result}` or `{:error, id, reason}`.
Returns `{:error, :decode_error, binary}` if JSON is malformed.

# `encode_clear_kernels`

```elixir
@spec encode_clear_kernels(request_id()) :: binary()
```

Encodes a clear_kernels request.

# `encode_load_default_kernels`

```elixir
@spec encode_load_default_kernels(request_id(), String.t()) :: binary()
```

Encodes a load_default_kernels request.

# `encode_load_kernels`

```elixir
@spec encode_load_kernels(request_id(), [String.t()]) :: binary()
```

Encodes a load_kernels request with explicit paths.

# `encode_ping`

```elixir
@spec encode_ping(request_id()) :: binary()
```

Encodes a ping request.

# `encode_state`

```elixir
@spec encode_state(request_id(), String.t(), float()) :: binary()
```

Encodes a state request.

Parameters are fixed for v0.1:
  observer: "EARTH"
  frame:    "ECLIPJ2000"
  abcorr:   "LT+S"

# `encode_utc_to_et`

```elixir
@spec encode_utc_to_et(request_id(), String.t()) :: binary()
```

Encodes a utc_to_et request.

---

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