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

A fact of a slot's schema in a design (spec/02 §7.3).

`%Macaw.Frame.Fact{slot, field, key}` is the attribute `key` of `field` in the schema
bound to `slot`: a field's `label`, its `unit`, the `order` of its states. It stands
wherever a value can, and in a text as the hole `#{fact(slot, field, key)}`;
application replaces it by the attribute, as it replaces a variable by its value.
Reading a fact is a requirement: a schema that does not give the attribute is
`{:missing_attribute, slot, field, key}`.

The keys are `keys/0`: the attributes of `Macaw.Data.Schema.attributes/0` and two
derived from a field's declared values, `:names` and `:colors`. `shape/1` says what
shape a key's value is: a `:scalar`, a `:list` or a `:map`. Only a scalar is a word
(`word?/1`), so only a scalar key's fact stands in a text's hole.

    iex> Macaw.Frame.Fact.new("primary", "latency", :unit)
    {:ok, %Macaw.Frame.Fact{slot: "primary", field: "latency", key: :unit}}

# `shape`

```elixir
@type shape() :: :scalar | :list | :map
```

The shape of a fact key's value: one word, a list or a map.

# `t`

```elixir
@type t() :: %Macaw.Frame.Fact{field: String.t(), key: atom(), slot: String.t()}
```

# `fetch`

```elixir
@spec fetch(Macaw.Data.Schema.t(), term(), term(), map()) :: {:ok, term()} | :error
```

What the fact key gives `field` in `schema`: `{:ok, value}`, or `:error` when it gives
nothing. `:values` is the bare values, `:status` each state's role as the theme's typed
color (`{:color, "good"}`), `:names` the map of `Macaw.Data.Schema.value_names/2`, and
`:colors` each declared value's **declared color** (spec/01 §7): the color the page or
design declares for it in `colors`, the render context's field-keyed mapping
(`Macaw.Component.Context`'s `colors`, `%{field => %{value => color}}`), else its
`status` role as the theme's typed color, so a field's colors are declared once, for
every Frame and the shared Legend alike. The schema holds no color (macaw_data#446). A
derived key is given only when not empty.

    iex> schema = Macaw.Data.Schema.new!(%{"state" => %{type: :category, values: [
    ...>   %{value: "ok", name: "OK"}, "unknown"]}})
    iex> Macaw.Frame.Fact.fetch(schema, "state", :values)
    {:ok, ["ok", "unknown"]}
    iex> Macaw.Frame.Fact.fetch(schema, "state", :names)
    {:ok, %{"ok" => "OK"}}
    iex> Macaw.Frame.Fact.fetch(schema, "state", :unit)
    :error
    iex> schema = Macaw.Data.Schema.new!(%{"state" => %{type: :category,
    ...>   values: ["ok", "down", "unknown"],
    ...>   status: %{"ok" => :good, "down" => :bad}}})
    iex> Macaw.Frame.Fact.fetch(schema, "state", :colors)
    {:ok, %{"ok" => {:color, "good"}, "down" => {:color, "bad"}}}
    iex> Macaw.Frame.Fact.fetch(schema, "state", :colors, %{"state" => %{"down" => {:color, "info"}}})
    {:ok, %{"ok" => {:color, "good"}, "down" => {:color, "info"}}}

# `keys`

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

The fact keys, in order: the description attributes, then `:names` and `:colors`.

    iex> Macaw.Frame.Fact.keys() |> Enum.take(-3)
    [:values, :names, :colors]

# `new`

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

A fact: `{:ok, fact}` with the slot and the field as strings (an atom given in Elixir is
read as its string), or every fault by the argument's name: a slot or field that is no
name `{:type, :name}`, a key that is no fact key `{:enum, keys}`.

    iex> Macaw.Frame.Fact.new(:primary, :latency, :nope)
    {:error, [{[:key], {:enum, Macaw.Frame.Fact.keys()}}]}

# `shape`

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

The shape of a fact key's value, whatever schema gives it: `:scalar`, `:list` or `:map`;
`{:error, [{[], {:enum, keys}}]}` for a term that is no fact key.

    iex> Macaw.Frame.Fact.shape(:unit)
    :scalar
    iex> Macaw.Frame.Fact.shape(:names)
    :map

# `word?`

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

Whether a fact of the key stands in a text's hole: its shape is `:scalar`.

    iex> Macaw.Frame.Fact.word?(:label)
    true
    iex> Macaw.Frame.Fact.word?(:order)
    false

---

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