# `Plushie.Protocol.Decode`
[🔗](https://github.com/plushie-ui/plushie-elixir/blob/v0.7.2/lib/plushie/protocol/decode.ex#L1)

Wire protocol decoding.

Deserializes binary payloads (MessagePack or JSON) into string-keyed
maps, then dispatches them into typed Elixir event structs. Handles
scoped ID splitting, binary field normalization, and the full set of
renderer protocol message types.

# `decode`

```elixir
@spec decode(data :: binary(), format :: Plushie.Protocol.format()) ::
  {:ok, map()} | {:error, term()}
```

Decodes a wire-format binary into a string-keyed map without dispatch.

Unlike `decode_message/2` which dispatches into Elixir event structs, this
returns the raw deserialized map. Used by script and test helpers that
handle renderer responses (query_response, interact_response, etc.) directly.

# `decode_event`

```elixir
@spec decode_event(event :: map()) :: Plushie.Event.delivered_t()
```

Decodes a renderer event map into a typed Plushie event struct.

Accepts either a raw event payload from `interact_step` / `interact_response`
or a full protocol event message with `"type" => "event"`.

Raises on unknown or malformed events. The renderer and SDK are lock-step;
an unrecognised event is a protocol bug, not a forward-compatibility concern.
Every event from the renderer must include `window_id`.

# `decode_message`

```elixir
@spec decode_message(data :: binary(), format :: Plushie.Protocol.format()) ::
  Plushie.Protocol.decode_result()
```

Decodes a protocol message into an event struct or tuple.

Returns `{:error, reason}` on parse failure or an unrecognised message shape.

## Examples

    iex> Plushie.Protocol.Decode.decode_message(~s({"type":"event","family":"click","id":"btn_save","window_id":"main"}), :json)
    %Plushie.Event.WidgetEvent{type: :click, id: "btn_save", window_id: "main", value: nil}

    iex> match?({:error, {:decode_failed, _}}, Plushie.Protocol.Decode.decode_message("not json"))
    true

# `decode_message!`

```elixir
@spec decode_message!(data :: binary(), format :: Plushie.Protocol.format()) ::
  Plushie.Protocol.decoded_message()
```

Strict variant of `decode_message/2`.

Raises `Plushie.Protocol.Error` when the payload cannot be decoded into a
valid protocol message.

---

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