Search documentation of Plushie

# Plushie.Protocol.Decode (Plushie v0.7.2)

Copy Markdown View Source

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.

## Summary

### Functions

decode(data, format \\ :msgpack)

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

decode_event(event)

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

decode_message(data, format \\ :msgpack)

Decodes a protocol message into an event struct or tuple.

decode_message!(data, format \\ :msgpack)

Strict variant of `decode_message/2`.

## Functions

# decode(data, format \\ :msgpack)

    @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(event)

    @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(data, format \\ :msgpack)

    @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!(data, format \\ :msgpack)

    @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.