# `KiwiCodec`
[🔗](https://github.com/open-pencil/kiwi_codec/blob/v0.7.0/lib/kiwi_codec.ex#L1)

Encodes and decodes Kiwi messages as Elixir structs.

`use KiwiCodec, kind: :message`, `:struct`, or `:enum` defines a module for a
schema definition with the `KiwiCodec.DSL` macros, and `mix kiwi.gen` or
`compile_schema!/2` generates such modules from a schema. `encode/1` and
`decode/3` convert their structs to and from Kiwi binaries.

Data written with a newer schema decodes when `decode/3` is given that schema:
fields the modules do not define are skipped. `KiwiCodec.SchemaInterpreter`
works without modules, for schemas loaded at runtime.

# `compile_schema!`

```elixir
@spec compile_schema!(
  String.t(),
  keyword()
) :: [module()]
```

Compiles `.kiwi` schema text into generated Elixir modules in memory.

This is a convenience for tests and tooling. For application code, prefer the
`mix kiwi.gen` task so generated modules are written to source files.

# `decode`

```elixir
@spec decode(binary(), module(), keyword()) :: struct()
```

Decodes a binary as the given Kiwi module.

Options:

  * `:schema` - the schema the data was written with, as a `KiwiCodec.Schema`
    or a prepared `KiwiCodec.SchemaInterpreter` schema. Message fields the
    generated modules do not define are skipped using it. Without it they
    raise `KiwiCodec.DecodeError`.

# `encode`

```elixir
@spec encode(struct()) :: binary()
```

Encodes a Kiwi struct.

# `encode_to_iodata`

```elixir
@spec encode_to_iodata(struct()) :: iodata()
```

Encodes a Kiwi struct to iodata.

# `parse_schema!`

```elixir
@spec parse_schema!(String.t()) :: KiwiCodec.Schema.t()
```

Parses `.kiwi` schema text into a schema AST.

---

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