# `Macaw.Frame.Catalog`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/catalog.ex#L1)

Catalog fragments (spec/13 §1): a ready-made frame document, or a ready-made set of
layers, registered under an id so a page places a line chart or a sankey diagram without
writing one. Copied from visualize's `Visualize.Chart.Presets` (`names/0`, `defaults/1`,
`design/2`); its assign mapping `sources/2` is not carried over: the data reaches the
Frame through slots bound to sources, the size is the Frame's and colors are theme
roles.

A catalog fragment is a module implementing this behavior (§1.1). Every callback is pure
and answers data. Its `fragment/0` is a fragment of the frame document type
(`Macaw.Frame.Document`) whose free variables are exactly its parameters, each declared
under `vars` with its default, so the kernel's composition places it as any stored
fragment.

- **The registry** (`registry/1`) is built from the packages a Frame module lists, as
  its layer kinds are (spec/04 §2): `macaw_frame`'s own package always in, and two
  modules of one id `{[:catalog, id], {:named_twice, module, other}}`.
- **The store** (`store/1`, §1.3) presents a registry's catalog modules as a read-only
  `Macaw.Layers.Store`: `get/2` resolves an id to its fragment, `list/1` answers each id
  with its doc, parameters and slots, and every write is refused.
- **A use site** (`place/3`, §1.2) is `%{"ref" => id, "bind" => %{name => value},
  "stack" => [fragment]}`: the fragment fetched, its parameters bound by value (or to a
  state source as `{:state, name}`, left for the Frame's `"vars.<name>"`), the stacked
  fragments laid over it, and the whole validated as one design. A diagram's use site
  may give `"text"` (spec/13 §7.3): the text read by the fragment's text form
  (`text_form/0`) fills the fragment's slots with static rows and binds the parameters
  its header sets (`text_bind/1`), so a diagram is authored as text or bound to sources
  from the one fragment.
- **The behavior's rules** (`check/1`, §1.1, §2): an id in its namespace, a fragment
  with no function, its free variables exactly its parameters.

    iex> {:ok, store} = Macaw.Frame.Catalog.store([])
    iex> {:ok, entry} = Macaw.Layers.Store.get(store, "charts.line")
    iex> entry.id
    "charts.line"

# `parameter`

```elixir
@type parameter() :: %{
  :type =&gt; parameter_type(),
  optional(:default) =&gt; term(),
  doc: String.t()
}
```

A catalog parameter: its type, its default (absent: required) and its doc.

# `parameter_type`

```elixir
@type parameter_type() ::
  :number
  | :integer
  | :boolean
  | :string
  | :text
  | {:enum, [String.t()]}
  | :color
  | :scale_kind
  | :margin
  | :url
  | {:list, :number | :string}
```

A catalog parameter's type (§1.1).

# `registry`

```elixir
@type registry() :: %{required(String.t()) =&gt; module()}
```

A registry of catalog modules by id.

# `store`

```elixir
@type store() :: {module(), registry()}
```

The read-only store's assign: `Macaw.Frame.Catalog.Store` and its registry.

# `doc`

```elixir
@callback doc() :: String.t()
```

What it is, for a person and a builder's library.

# `fixtures`

```elixir
@callback fixtures() :: [map()]
```

The fixtures its goldens, gallery round trip and kit runs use (spec/14 §1).

# `fragment`

```elixir
@callback fragment() :: map()
```

The fragment: string names, its parameters as variables, its fields as its slots' roles.

# `id`

```elixir
@callback id() :: String.t()
```

The fragment's id in the catalog store, `"<namespace>.<name>"`.

# `kind`

```elixir
@callback kind() :: :design | :layers
```

`:design` for a whole frame document of one frame; `:layers` for layers placed in one.

# `namespace`

```elixir
@callback namespace() :: :charts | :maps | :diagrams
```

Its namespace (§2).

# `parameters`

```elixir
@callback parameters() :: [{String.t(), parameter()}]
```

Its parameters in order, each `%{type, default, doc}`; one without a default is required.

# `slots`

```elixir
@callback slots() :: %{required(String.t()) =&gt; map()}
```

The data slots it reads, each role with its requirement (`Macaw.Component.Slot`).

# `text_bind`
*optional* 

```elixir
@callback text_bind(design :: map()) :: %{required(String.t()) =&gt; term()}
```

The parameters a text read by the text form sets, from the form's design (its header's).

# `text_form`
*optional* 

```elixir
@callback text_form() :: module()
```

A diagram's text form (spec/13 §7.3), a `Macaw.Frame.TextForm` whose design's slots are
the fragment's: a use site's `"text"` is read by it into the slots' static rows.

# `catalog?`

```elixir
@spec catalog?(term()) :: boolean()
```

Whether a module is a catalog fragment: loaded, and declaring this behavior.

# `check`

```elixir
@spec check(term()) :: :ok | {:error, [Macaw.Base.Fault.t(), ...]}
```

`:ok`, or every way a catalog module breaks the behavior's rules (§1.1, §2): an id
outside its namespace (`{:id, id}`), a module outside its namespace's prefix
(`{:module, prefix}`), a kind or namespace of the wrong shape, a parameter of an unknown
type, a function in the fragment (`:function`), a parameter the fragment does not use
(`{:unused, name}`) and a free variable it does not declare (`{:undeclared, name}`), a
parameter without its declaration under `vars` (`{:no_declaration, name}`), no fixture
(`:no_fixtures`), a doc that is no text, and a diagram without a text form
(`{[:text_form], :required}`, spec/13 §7.3).

# `fetch`

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

The store's fetch (macaw_layers 02 §7): a function from an id to `{:ok, fragment}` or
`:error`, for `Macaw.Frame.Document.flatten/2` and `compile/3`'s `fetch:`.

# `namespaces`

```elixir
@spec namespaces() :: [atom(), ...]
```

The namespaces (§2): `:charts`, `:maps` and `:diagrams`.

# `place`

```elixir
@spec place(term(), term(), keyword()) ::
  {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A use site of a catalog fragment resolved through `store` (§1.2): `{:ok, design}`, the
fragment with its parameters bound and the site's `stack` laid over it, validated whole
through `registry:` (the Frame's, `Macaw.Frame`'s by default); or every fault at once.

The site is `%{"ref" => id, "bind" => %{name => value}, "stack" => [fragment]}` (keys
read by spelling). A value is checked against its parameter's type, `{:type, type}` at
`["bind", name]`; `{:state, source}` leaves the parameter for the Frame's
`"vars.<name>"`; a name the fragment does not take is `:unknown_param` there; a required
parameter left unbound is `{:unbound, name}` there; an id the store lacks is `{:unknown,
:catalog, id}` at `["ref"]`. An element of `stack` may itself be a use site of a
catalog fragment (a map layer placed in `maps.map`, §6): its fragment with its
parameters bound, those not given at their defaults, its faults at `["stack", i |
path]`. With `schemas:` (each slot's `Macaw.Data.Schema`, by the
roles' names), a scale its bound field's type cannot take is `{:scale_type, scale,
type}` and a field missing an attribute a role requires `{:requires, role, key}`, both at
`["slots", slot, role]`.

    iex> {:ok, store} = Macaw.Frame.Catalog.store([])
    iex> {:ok, design} = Macaw.Frame.Catalog.place(%{"ref" => "charts.line", "bind" => %{"title" => "CPU"}}, store)
    iex> design.meta.name
    "CPU"

# `prefix`

```elixir
@spec prefix(term()) :: String.t() | nil
```

The module prefix of a namespace (§2): `"Macaw.Frame.Charts."` for `:charts`.

# `registry`

```elixir
@spec registry(term()) :: {:ok, registry()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The catalog modules of `Macaw.Frame.Package` and the listed packages by id (§1.3):
`{:ok, %{id => module}}`, or every fault at once, a module that is no package
`{[module], :not_a_package}` and an id two modules declare `{[:catalog, id],
{:named_twice, module, other}}`.

    iex> {:ok, registry} = Macaw.Frame.Catalog.registry([])
    iex> registry["charts.line"]
    Macaw.Frame.Charts.Line

# `store`

```elixir
@spec store(term()) :: {:ok, store()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The read-only fragment store of a registry's catalog modules (§1.3), as a
`Macaw.Layers.Store` assign: from a Frame module's registry (its `__registry__/0`, or
the map itself, its `catalogs` entry), a Frame module, or a list of packages.
`{:ok, {Macaw.Frame.Catalog.Store, registry}}`, or the registry's faults.

---

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