# `Macaw.Draw.Generators`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/generators.ex#L1)

Styles and colors a design names and the frame computes at render (spec/07 §25;
visualize spec/14 §3.13, D-177), copied from visualize's `Visualize.Generators`: the
stored call, the host module's macro, and the registry.

A design stores **`{:generate, name, args}`** where a style or a color goes: `name` the
generator's name as a string, `args` a list of literal terms or field bindings
`{:field, f}`, read per datum.

    iex> Macaw.Draw.Generators.generate(:tenant_style, [{:field, "tenant"}])
    {:ok, {:generate, "tenant_style", [{:field, "tenant"}]}}

A host defines generators as ordinary, tested Elixir:

    defmodule MyApp.Generators do
      use Macaw.Draw.Generators

      defgenerator tenant_style(tenant_id, ctx) do
        MyApp.Tenants.theme(tenant_id, ctx.assigns.viewer).message_style
      end
    end

`use Macaw.Draw.Generators` imports `defgenerator/2` and compiles `__generators__/0`,
the `{name, arity}` of every generator in definition order, each once. The last
parameter is the context, `%{theme: theme, params: params, assigns: assigns}`. A
generator must be pure in its arguments and context; **one that raises is a bug and
surfaces as one**: nothing rescues it.

**Registration is by module, from an explicit list** (`registry/1`); no application
environment is read. A design's name is matched by spelling (`lookup/3`, `check/2`), and
no atom is made from it. `call/4` resolves a stored call through a field reader the
caller supplies, so the library never sees a row's shape.

# `call`

```elixir
@type call() :: {:generate, String.t(), [term()]}
```

A stored generator call.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `registry`

```elixir
@type registry() :: %{required({atom(), arity()}) =&gt; module()}
```

Each generator `{name, arity}` by the module that defines it.

# `call`

```elixir
@spec call(registry(), call(), (term() -&gt; term()), map()) ::
  {:ok, term()} | {:error, term()}
```

Resolves a stored call: each `{:field, f}` argument given to `read`, a 1-arity function
reading a field of the datum being drawn, each literal as it is, the context last; the
generator's result as `{:ok, result}`, or `check/2`'s `{:error, reason}`. The generator
is called as it is: a raise is its own and passes through.

# `check`

```elixir
@spec check(registry(), call()) :: :ok | {:error, tuple()} | error()
```

What the document's validator asks of a stored call: `:ok`, `{:error, {:undeclared,
:generator, name}}` for a name the registry lacks, or `{:error, {:arity, :generator,
name, n}}` for one registered at another arity, `n` the arguments given (the context
counts one more).

# `defgenerator`
*macro* 

A generator: `defgenerator name(arg, …, ctx) do … end` is a public `def` of that head,
recorded under the module's `__generators__/0`. The last parameter is the context. A
generator of several clauses is defined clause by clause and listed once. A head
without at least the context parameter is a `CompileError`.

# `defines?`

```elixir
@spec defines?(registry(), atom() | String.t()) :: boolean() | error()
```

Whether the registry defines a generator of the name, by spelling, at any arity.

# `generate`

```elixir
@spec generate(atom() | String.t()) :: {:ok, call()} | error()
```

`generate(name, [])`.

# `generate`

```elixir
@spec generate(atom() | String.t(), [term()]) :: {:ok, call()} | error()
```

The stored call `{:generate, name, args}` of an atom or string name, held as its string:
`{:ok, call}`, or `{[:name], {:type, :name}}` for a name that is not a plain word and
`{[:args], {:type, :list}}` for arguments that are no list.

# `lookup`

```elixir
@spec lookup(registry(), atom() | String.t(), arity()) ::
  {:ok, {atom(), module()}} | :error | error()
```

The generator whose atom's string is `name` at `arity`: `{:ok, {atom, module}}`, or
`:error`. No atom is made from `name`.

# `registry`

```elixir
@spec registry([module()]) :: {:ok, registry()} | error()
```

The registry of `modules`: `{:ok, registry}`, each `{name, arity}` by the module that
defines it, or `{:error, faults}`: `{[i], {:not_generators, module}}` for a module that
is not loaded or defines no `__generators__/0`, and `{[i], {:duplicate, name, arity,
other}}` for a generator two modules define, at the later module's index.

---

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