# `ExGram.Cast`
[🔗](https://github.com/rockneurotiko/ex_gram/blob/0.65.0/lib/ex_gram/cast.ex#L1)

Helper module to convert plain values returned from Telegram to ExGram models.

This module handles the conversion of plain maps (JSON responses from the Telegram
Bot API) into strongly-typed ExGram model structs. It recursively processes nested
structures, arrays, and polymorphic types (subtypes).

See `ExGram.Model.Subtype` for how polymorphic types are handled.

# `type_def`

```elixir
@type type_def() :: atom() | {:array, type_def()} | [type_def()] | nil
```

# `cast`

```elixir
@spec cast(any(), type_def()) :: {:ok, any()} | {:error, ExGram.Error.t()}
```

Converts the given plain value to ExGram models.

  iex> ExGram.Cast.cast(%{message_id: 3, chat: %{id: 5}}, ExGram.Model.Message)
  {:ok, %ExGram.Model.Message{message_id: 3, chat: %ExGram.Model.Chat{id: 5}}}

  iex> ExGram.Cast.cast(true, ExGram.Model.Message)
  {:error, %ExGram.Error{message: "Expected a map for type ExGram.Model.Message, got: true"}}

# `cast!`

```elixir
@spec cast!(any(), type_def()) :: any()
```

Converts the given plain value to ExGram models.

Raises an error if the conversion fails. See `cast/2` for more details.

---

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