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

A channel declaration (spec/04 §1.2): a typed slot of a layer kind, with its role.

`%Macaw.Frame.Layer.Channel{}` holds:

  * `name` - the channel's name, a string (`"x"`, `"y0"`, `"value"`, `"series"`);
  * `required` - `true`, `false`, `{:unless, names}` (required unless all of those are
    given) or `{:one_of, names}` (one of a group is required);
  * `role` - what the reading is: `:position`, `:edge`, `:size`, `:share`, `:angle`,
    `:radius`, `:color`, `:series`, `:geometry` or `:text` (`roles/0`);
  * `family` - the scale family a `:position`, `:edge`, `:radius` or `:color` channel
    reads by default (`"x"`, `"y"`, `"angle"`, `"r"`, `"color"`), or `nil`;
  * `types` - the `macaw_data` types a field read by the channel may hold;
  * `constant` - whether a number may stand in for a field;
  * `scale_end` - whether `{:scale, :min}` and `{:scale, :max}` may be given;
  * `doc` - its meaning, for a person.

visualize declared these in one table (`Visualize.Chart.Schema.Mark`'s `@marks`); here
each kind declares its own (`c:Macaw.Frame.Layer.channels/0`).

**Channels declare the Frame's slot roles** (`roles/3`): every field a layer's channels
and its steps' field options read from the slot before any step derives a column is a
role of that slot, its `type` the intersection of the `types` of the channels reading
it. `bind/3` holds a surface's binding of those roles to a source's fields to the
types.

    iex> alias Macaw.Frame.Layer.Channel
    iex> x = Channel.new("x", role: :position, family: "x", types: [:time, :number])
    iex> {x.name, x.required, x.constant, x.scale_end}
    {"x", false, true, true}

# `requirement`

```elixir
@type requirement() :: boolean() | {:unless, [String.t()]} | {:one_of, [String.t()]}
```

# `role`

```elixir
@type role() ::
  :position
  | :edge
  | :size
  | :share
  | :angle
  | :radius
  | :color
  | :series
  | :geometry
  | :text
```

# `slot_role`

```elixir
@type slot_role() :: %{
  name: String.t(),
  type: [atom()],
  window: boolean(),
  many: boolean(),
  optional: boolean()
}
```

A slot role a layer declares (spec/04 §1.2; macaw_component 01 §1.3): the field name the
document writes, its types, and its `window`, `many` and `optional` flags.

# `t`

```elixir
@type t() :: %Macaw.Frame.Layer.Channel{
  constant: boolean(),
  doc: String.t(),
  family: String.t() | nil,
  name: String.t(),
  required: requirement(),
  role: role(),
  scale_end: boolean(),
  types: [atom()]
}
```

# `allows`

```elixir
@spec allows(t(), term()) :: :ok | {:error, :constant | :scale_end}
```

Whether a reading is allowed on a channel (spec/04 §1.2, §1.3): a field always; a number
where the channel takes a constant; `{:scale, :min | :max}` where it takes a scale end;
`{:scale, :boundary}` on a positional channel. `{:error, reason}` names what is refused,
as the validator reports it at the channel.

# `bind`

```elixir
@spec bind([slot_role()], map(), Macaw.Data.Schema.t()) ::
  :ok | {:error, [Macaw.Base.Fault.t(), ...]}
```

A surface's binding of a layer's roles to a source's fields, held to the roles' types
(§1.2, `test:layers-channels-types`): `bindings` maps a role's name to the source's
field (a role it lacks reads its own name), `schema` is the source's
`Macaw.Data.Schema`. A field whose schema type is not among its role's types is
`{[:roles, role], {:type, role, type}}`; a required role bound to a field the schema
lacks is `{[:roles, role], {:undeclared, :field, field}}`. `:ok` when every role holds.

# `check`

```elixir
@spec check(term()) :: [Macaw.Base.Fault.t()]
```

The faults of a channel declaration (the kit's `:declarations` check): its name a
non-empty string, its role and family known, its types `macaw_data`'s, its requirement
one of the four forms. `[]` for a good one.

# `families`

```elixir
@spec families() :: [String.t(), ...]
```

The scale families a channel may read by default (§1.2).

# `field`

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

The field a reading names: a field's name or `{:field, f}`, else `nil`.

# `new`

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

A channel named `name` with the fields of `opts` (a keyword list or map of the struct's
keys). A `:series` channel takes no constant and no scale end; a `:geometry` or a
`:text` channel no scale end. A name that is no string, or an option the struct lacks,
is a type fault.

    iex> Macaw.Frame.Layer.Channel.new("series", role: :series).constant
    false
    iex> Macaw.Frame.Layer.Channel.new(:x, [])
    {:error, [{[:name], {:type, :string}}]}

# `read_as`

```elixir
@spec read_as(term(), term(), term()) :: term()
```

The name a model's channel table (`c:Macaw.Frame.Coordinates.channel_scale/3`, spec/03
§2.2) reads a layer's channel by: the channel's own, except two of the second cut's
(spec/04 §5.1). A `polyline`'s `dimension` is its position along `x` and reads as `x`
does. An `interval` given `y` and no `x` draws across, so its `lo` and `hi` are
positions along `x` and read as `x0` and `x1` do. `channels` are the layer's given
channels, a map or their names.

    iex> Macaw.Frame.Layer.Channel.read_as("interval", "lo", %{"y" => {:field, "k"}})
    "x0"
    iex> Macaw.Frame.Layer.Channel.read_as("interval", "lo", %{"x" => {:field, "k"}})
    "lo"
    iex> Macaw.Frame.Layer.Channel.read_as("polyline", "dimension", %{})
    "x"

# `roles`

```elixir
@spec roles() :: [role(), ...]
```

The roles a channel may have (§1.2).

# `roles`

```elixir
@spec roles([t()], map(), list(), keyword()) :: [slot_role()]
```

The slot roles a layer declares (§1.2): every field its channels and its steps' field
options read from the slot **before any step derives a column**. `channels` are the
kind's declarations, `given` the layer's channel readings (`%{name => reading}`, a
reading a field's name, `{:field, f}`, a number or a scale end), and `steps` the
layer's step nodes. Options: `operations:` the operations registry (macaw_data's by
default), `context:` the step context a spatial step's outputs are judged in (the
Frame registry's `step_context`, a frame), `window:` the channels read as a time axis
(a gap-breaking kind's `["x"]`).

A role's `type` is the intersection of the types of the channels that read it (every
type for a field only a step reads); `window: true` where a window channel reads it;
`many: true` where a step's list option names it; `optional: true` where every channel
reading it is optional and no step reads it. A field a step derives (a stack's `"y1"`)
is the step's output, not a role. Roles are sorted by name.

    iex> alias Macaw.Frame.Layer.Channel
    iex> channels = [Channel.new("x", required: true, types: [:time, :number]),
    ...>   Channel.new("y", required: true, types: [:number])]
    iex> Channel.roles(channels, %{"x" => "t", "y" => "cpu"}, [], window: ["x"])
    [%{name: "cpu", type: [:number], window: false, many: false, optional: false},
     %{name: "t", type: [:time, :number], window: true, many: false, optional: false}]

# `types`

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

The `macaw_data` types a channel may list (`:term` for any value, `:any` likewise).

---

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