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

The conventions every layout shares (spec/08 §1): options checked once and every fault
answered at once, vocabularies read by spelling, and output as columns.

A layout assigns positions to things that have none of their own: the nodes of a
hierarchy (`Hierarchy`, `Tree`, `Cluster`, `Pack`, `Partition`, `Treemap`), the nodes and
links of a network (`Graph`, `Chord`, `Sankey`, `Force`) and the cells of a hexagonal
binning (`Hexbin`). Every layout is a pure function of its options and its input.

- **Options** (§1.1). A layout with options is built by `new/1`, which reads every
  option its table lists through `check/2`: a key given as a string is read by its
  spelling, an unknown key is `{[key], :unknown_key}`, and a bad value is a fault with
  the option as its path, every fault at once, ordered by `Macaw.Base.Fault.sort/1`.
- **Vocabularies** (§1.2). An enumerated option is one of the layout's own atoms, or the
  string of its spelling, read by `spell/2`; no atom is ever made from text.
- **Columns** (§1.4). Each layout's `columns` function answers a map from column name,
  a string, to a list, every list of one length, with a `"row"` column naming the input
  row each output row came from and shapes as `Macaw.Draw.IR.Path` values under
  `"path"`. `columns/2` builds one from records.

Input drawn from data never raises: a missing parent, an unknown node, a cycle, a zero
or `nil` value each has a defined result or a fault in the answer.

# `error`

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

A refusal: faults with their paths.

# `schema`

```elixir
@type schema() :: [{atom(), term(), type()}]
```

A layout's option table: each option's key, default and type.

# `type`

```elixir
@type type() ::
  :number
  | :non_negative_number
  | :positive_number
  | :non_negative_integer
  | :positive_integer
  | :boolean
  | :size
  | :positive_size
  | {:function, non_neg_integer()}
  | {:member, [atom()]}
  | {:nil_or, type()}
  | {:either, [type()], atom()}
  | {:check, (term() -&gt; {:ok, term()} | {:error, term()})}
```

The type of one option, as `check/2` reads it:

- `:number`, `:non_negative_number`, `:positive_number`, `:non_negative_integer`,
  `:positive_integer`, `:boolean`: a value of that kind;
- `:size`: two non-negative numbers, a tuple or a list, read as a tuple;
  `:positive_size` the same of positive numbers;
- `{:function, arity}`: a function of that arity, its fault `{:type, :function<arity>}`;
- `{:member, members}`: one of `members`, an atom or the string of its spelling;
- `{:nil_or, type}`: `nil` or a value of `type`;
- `{:either, [type], name}`: a value of any of the types, its fault `{:type, name}`;
- `{:check, fun}`: `fun.(value)` answers `{:ok, value}`, `{:error, reason}` (one fault
  at the option) or `{:error, [{path, reason}]}` (faults below the option).

# `check`

```elixir
@spec check(keyword() | map(), schema()) ::
  {:ok, %{required(atom()) =&gt; term()}} | error()
```

Reads `opts`, a keyword list or a map, against `schema` (§1.1): every option the schema
lists, its default where it is not given, each value checked against its type.

Answers `{:ok, values}`, a map from every schema key to its value, or
`{:error, faults}`, every fault at once and sorted. A string key is read as the atom key
of the same spelling; a key the schema does not list is `{[key], :unknown_key}`, the key
as given. Options that are not a keyword list or a map are `{[], {:type, :options}}`.

    iex> schema = [{:padding, 0, :non_negative_number}, {:round, false, :boolean}]
    iex> Macaw.Draw.Layout.check(%{"padding" => 2}, schema)
    {:ok, %{padding: 2, round: false}}
    iex> Macaw.Draw.Layout.check([padding: -1, wat: 1], schema)
    {:error, [{[:padding], {:type, :non_negative_number}}, {[:wat], :unknown_key}]}

# `columns`

```elixir
@spec columns([map()], [{String.t(), term()}]) ::
  %{required(String.t()) =&gt; [term()]} | error()
```

Output columns from records (§1.4): for each `{name, key}` of `fields`, the column
`name`, a string, holding `Map.get(record, key)` of every record, in record order. Every
column has one length, the records' count.

    iex> Macaw.Draw.Layout.columns([%{x: 1, y: 2}, %{x: 3, y: 4}], [{"x", :x}, {"y", :y}])
    %{"x" => [1, 3], "y" => [2, 4]}

# `spell`

```elixir
@spec spell(term(), [atom()]) :: {:ok, atom()} | :error | error()
```

Reads `value` against a vocabulary (§1.2): a member atom as it is, or a string that spells
a member as that member. Answers `{:ok, atom}`, or `:error` for anything else, never
making an atom from text.

    iex> Macaw.Draw.Layout.spell("slice_dice", [:squarify, :slice_dice])
    {:ok, :slice_dice}
    iex> Macaw.Draw.Layout.spell("spiral", [:squarify, :slice_dice])
    :error

---

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