Macaw.Frame.Document (macaw_frame v0.1.0)

Copy Markdown View Source

The frame document: the macaw_layers document type a Frame draws (spec/02).

A design is a BEAM term that names its data slots, its frames and their coordinate models, its layers and its labels, without a function anywhere in it (§1.1). This module is its type, implementing four behaviors:

BehaviorWhat it suppliesspec/02
Macaw.Layers.Documentvalidate/1: every fault of a value, ordered by path§10
Macaw.Base.Versioncurrent/0 and step/2, the type's own version line, from 1§9
Macaw.Layers.Fragmentthe merge rules, identities, kinds and placement of its fragments§8, §16
Macaw.Layers.Formits tags, keys and atoms and its values' map form§11

The root kind is :design. A value built in Elixir is judged by check/2, with no encoding and no limit; a document from elsewhere is read by read/2 through the kernel's gates. The value carries no version key: the version is the kernel's "$version" stamp. Nothing raises on input: every public function answers {:ok, value}, :ok or {:error, faults}, every fault by path.

iex> design = %{frames: %{"main" => %{kind: "cartesian"}}}
iex> Macaw.Frame.Document.check(design)
{:ok, %{frames: %{"main" => %{kind: "cartesian"}}}}
iex> Macaw.Frame.Document.root()
:design

Summary

Types

A frame document: a :design node.

A fault: the path to the value, and why (Macaw.Base.Fault).

A fragment of the type: a design, or a part of one (§16).

Functions

The atoms a $atom may spell (§9.3).

A text with its variable holes substituted by bindings (§7.4), written back as a string: a literal value written into the text, a variable renaming the hole. Any other value as it is.

A value built in Elixir canonicalized (Macaw.Frame.Document.Migration.canonical/1) and judged (§13.1): {:ok, design} in its canonical shape, or every fault by path. Options: profile: (:roles by default, :open to allow literal colors, §1.6), registry: (%{layers: %{name => module}, models: %{name => module}, operations: registry, step_context: map}, the registries the Frame module fixes, and the context its spatial steps run under; without layers or models a kind's or a model's name is not looked up, without operations steps go through macaw_data's own package), generators: and urls:.

The type check of a design's bindings against its slots' schemas (§7.3): a field read through a scale its schema type cannot take is {:invalid_for, type} at the channel.

A value chosen by a parameter (§7.5): {:case, var(name), cases}, the value cases gives the variable's value once it is resolved, each case a value read by its spelling. Arguments that are no name and no map of cases answer {:error, faults}.

A design compiled once per document change (§12.1): {:ok, %Macaw.Frame.Compiled{}}, what the Frame's init/2 holds, sized at the context's size; or the faults of the first step that found any, so a binding fault is never mixed with a validator fault.

The union of two fragments meant to be disjoint (§8.1): Macaw.Layers.Fragment.compose/3 with this type; a name the two declare differently is :conflict at its path.

A design's contract (§7.3): %{slot => %{field => [key, …]}}, each slot's requires and every attribute a fact of it reads, keys in Macaw.Frame.Fact.keys/0 order.

The type's current version (§9.1): 1.

What a Frame drawing design declares (§12.3), without a context, every bindable variable at its default: slots, writes, local, events, announces, stylesheets, proxy_routes and isolation. The local keys are named under the placeholder group "<group>", which the cell's sync setting names; registry as compile/3's.

The value under one of the type's tags, its payload read; :default for a payload the type does not read, which the reader refuses at its path.

A whole design as inline sites, one kind each, keyed where the design keyed them (§8.4).

A value's own map form (§11.2), :default for anything the kernel writes.

Provenance (§15.1): Macaw.Layers.Fragment.explain/2 with this type, for the layers of a stack (each a fragment, or {name, fragment}), one entry per leaf of the stacked result, %{path, value, layer, overrode}, recomputed by stack/1's own merge. A term that is no list of fragments answers [].

A fact (§7.3): %Macaw.Frame.Fact{}, the attribute key of field in the schema bound to slot; {:error, faults} for arguments that are none.

A composite flattened through fetch (§16.4): Macaw.Layers.Composite.flatten/3 with this type, {:ok, design, %{missing: [...], keyless: [...]}}, a missing reference masked at its site and reported, or {:error, {:cycle, chain}}. A fragment that is no composite flattens to itself, canonical.

One fault as a line a person reads, the root named design (§10.1).

The variables a fragment or a stack still demands (§15.2), each with its uses, its parameter declaration and the types expected where it stands.

The variables a value of the type's own syntax holds (§7.4, §7.5): a text's variable holes, and the variables a choice holds (the one it is on and those of its cases), at the key the text or the choice stands at; [] elsewhere.

The identity of an element of a :concat list (§8.3): a layer's and a label's id, an axis's {scale, side}, and nil for anything else, which appends.

The ids left at reference sites resolved through the fetch (§8.4, D-103): a style inlined, a scale or slot declared under a synthesized key, a missing one reported.

A key's merge rule in the kernel's terms (§1.5): its rule, the node kind of a node-valued key, and the node kind of a list's or a name map's members; nil for a key the kind does not have. budget is a kind of its own at the design (§16.1).

The keys a fragment's map form reads as atoms (§11.1).

The kinds of a fragment (§16.1), :design first.

Two values of a style's part laid by the merge rule (§3.5), for the {:type, :style} rule: key by key, or the later whole but for what no type decides when the two name different types (a paint's opacity, a stroke's width, dash, join, cap, placement).

Whether the map at path is keyed by names (§11.1).

A style part written as a shorthand on one side and as a part on the other, both as the part (§3.1), so the two merge key by key (§8.4).

Where a site's contribution of kind lands in a design (§16.4): a layer or a label appended to its list, an axis to the frame "main"'s axes and a scale under its key there, a name-keyed kind under the site's key (:keyless without one), a frame merged as a node, the rest under its design key.

A keyed whole-design contribution placed under its key (§8.4, §16.4): its frames as the key, its layers and labels retargeted, their ids prefixed by the key.

A design from elsewhere, its JSON text or its map form, read through the kernel's gates and judged, then canonical (§9.2, §11.1, §13.1). Options: limits: (the kernel's bytes, depth and items and this type's ticks, iterations and counts), urls: (the host's Macaw.Layers.Url.Policy, closed when not given), profile:, vocabulary:, registry: and generators:, as check/2 takes them.

The root kind: :design (§1.1).

A site's contribution with its composite's defaults under what it places, the composite's farther and each node's nearer (§8.4, D-174).

The cascade of fragments meant to overlap (§8.1): Macaw.Layers.Fragment.stack/2 with this type, the higher layer's word winning; a layer may be named, {name, fragment}. A term that is no list of fragments answers {:error, faults}.

The step from one version to the next (§9.1); the line has none yet.

The type's tag table (§11.2), Macaw.Frame.Document.Tags.

What of a lower part a higher one keeps (§3.5, §8.4): a part whose type changes keeps only what no type decides.

The type's validate/1 (§10): :ok, or every fault of the value by path, under the default profile and with no registry. check/2 takes the reader's options.

Per-kind validity (§16.1): a fragment of kind validated alone, needing no key and looking up no reference. kind is one of kinds/0.

A variable (§7.1): %Macaw.Layers.Var{name: name}, its name a string. A name that is no name answers {:error, faults}.

The kind a singular wrapper key names (§16.1), or nil.

Types

design()

@type design() :: map()

A frame document: a :design node.

fault()

@type fault() :: Macaw.Base.Fault.t()

A fault: the path to the value, and why (Macaw.Base.Fault).

fragment()

@type fragment() :: map()

A fragment of the type: a design, or a part of one (§16).

Functions

atoms()

@spec atoms() :: Macaw.Base.Vocabulary.table()

The atoms a $atom may spell (§9.3).

bind_holes(kind, key, value, bindings)

@spec bind_holes(term(), term(), term(), term()) :: term()

A text with its variable holes substituted by bindings (§7.4), written back as a string: a literal value written into the text, a variable renaming the hole. Any other value as it is.

check(value, opts \\ [])

@spec check(term(), term()) :: {:ok, design()} | {:error, [fault(), ...]}

A value built in Elixir canonicalized (Macaw.Frame.Document.Migration.canonical/1) and judged (§13.1): {:ok, design} in its canonical shape, or every fault by path. Options: profile: (:roles by default, :open to allow literal colors, §1.6), registry: (%{layers: %{name => module}, models: %{name => module}, operations: registry, step_context: map}, the registries the Frame module fixes, and the context its spatial steps run under; without layers or models a kind's or a model's name is not looked up, without operations steps go through macaw_data's own package), generators: and urls:.

iex> Macaw.Frame.Document.check(%{frames: %{main: %{kind: :cartesian}}})
{:ok, %{frames: %{"main" => %{kind: "cartesian"}}}}
iex> Macaw.Frame.Document.check(%{layers: []})
{:error, [{[:frames], :required}]}

check_types(design, schemas)

@spec check_types(term(), term()) :: :ok | {:error, [fault(), ...]}

The type check of a design's bindings against its slots' schemas (§7.3): a field read through a scale its schema type cannot take is {:invalid_for, type} at the channel.

choose(name, cases)

@spec choose(term(), term()) ::
  {:case, Macaw.Layers.Var.t(), map()} | {:error, [fault(), ...]}

A value chosen by a parameter (§7.5): {:case, var(name), cases}, the value cases gives the variable's value once it is resolved, each case a value read by its spelling. Arguments that are no name and no map of cases answer {:error, faults}.

iex> Macaw.Frame.Document.choose(:shape, %{"pie" => 0, "donut" => 0.6})
{:case, %Macaw.Layers.Var{name: "shape"}, %{"donut" => 0.6, "pie" => 0}}

compile(design, context, opts \\ [])

@spec compile(term(), term(), term()) ::
  {:ok, Macaw.Frame.Compiled.t()} | {:error, [fault(), ...]}

A design compiled once per document change (§12.1): {:ok, %Macaw.Frame.Compiled{}}, what the Frame's init/2 holds, sized at the context's size; or the faults of the first step that found any, so a binding fault is never mixed with a validator fault.

context is the component context's fields compilation reads (a Macaw.Component.Context when macaw_component joins with the Frame, #11): theme: (the page's, the default theme without one), size: ({width, height}, {600, 400}), schemas: (each slot's Macaw.Data.Schema, its facts read from it), standalone: (true when the Frame draws alone, so the design's theme applies), sync: (the cell's group), units:, zone: and metrics: (the text measurer, a module implementing Macaw.Layers.Text.Metrics, resolved once with the theme's font; the estimate without one, any other module {:type, :metrics} at [:context, :metrics]). Options: vars: (each variable's value, {:state, source} for one bound to state), fetch: (a store's fetch, for a composite's references), registry: (%{layers: %{name => module}, models: %{name => module}, operations: registry, step_context: map}; macaw_frame's models when it gives none; layer kinds join macaw_frame's package with #10), generators:, urls:, profile:, limits:, ceiling: (1000), motion: (:full or :reduce) and themes: (the host's themes by name).

The steps (§12.1): flatten, resolve variables, resolve facts, validate the whole, resolve the theme, resolve kinds and models, resolve styles, set up the scales, plan regions and declarations.

iex> design = %{frames: %{"main" => %{kind: "cartesian"}}}
iex> {:ok, compiled} = Macaw.Frame.Document.compile(design, %{size: {300, 200}})
iex> compiled.models["main"].plot
%{width: 240, height: 150}

compose(a, b)

@spec compose(term(), term()) :: {:ok, fragment()} | {:error, [fault(), ...]}

The union of two fragments meant to be disjoint (§8.1): Macaw.Layers.Fragment.compose/3 with this type; a name the two declare differently is :conflict at its path.

contract(design)

@spec contract(term()) :: %{
  required(String.t()) => %{required(String.t()) => [atom()]}
}

A design's contract (§7.3): %{slot => %{field => [key, …]}}, each slot's requires and every attribute a fact of it reads, keys in Macaw.Frame.Fact.keys/0 order.

current()

@spec current() :: pos_integer()

The type's current version (§9.1): 1.

declarations(design, registry)

@spec declarations(term(), term()) :: {:ok, keyword()} | {:error, [fault(), ...]}

What a Frame drawing design declares (§12.3), without a context, every bindable variable at its default: slots, writes, local, events, announces, stylesheets, proxy_routes and isolation. The local keys are named under the placeholder group "<group>", which the cell's sync setting names; registry as compile/3's.

iex> {:ok, declared} = Macaw.Frame.Document.declarations(%{frames: %{"main" => %{kind: "cartesian"}}}, %{})
iex> {declared[:announces], declared[:events]}
{["point"], []}

decode_value(tag, payload)

@spec decode_value(term(), term()) :: {:ok, term()} | :default

The value under one of the type's tags, its payload read; :default for a payload the type does not read, which the reader refuses at its path.

iex> Macaw.Frame.Document.decode_value("$unit", [12, "pt"])
{:ok, {12, :pt}}
iex> Macaw.Frame.Document.decode_value("$unit", [12, "parsec"])
:default

decompose(design)

@spec decompose(term()) :: [Macaw.Layers.Use.t()]

A whole design as inline sites, one kind each, keyed where the design keyed them (§8.4).

encode_value(term)

@spec encode_value(term()) :: {:ok, term()} | :default

A value's own map form (§11.2), :default for anything the kernel writes.

iex> Macaw.Frame.Document.encode_value({12, :pt})
{:ok, %{"$unit" => [12, "pt"]}}
iex> Macaw.Frame.Document.encode_value({:field, "sha"})
{:ok, %{"$field" => "sha"}}

explain(layers)

@spec explain(term()) :: [map()]

Provenance (§15.1): Macaw.Layers.Fragment.explain/2 with this type, for the layers of a stack (each a fragment, or {name, fragment}), one entry per leaf of the stacked result, %{path, value, layer, overrode}, recomputed by stack/1's own merge. A term that is no list of fragments answers [].

iex> [entry] = Macaw.Frame.Document.explain([{"house", %{meta: %{name: "a"}}}, {"mine", %{meta: %{name: "b"}}}])
iex> {entry.path, entry.value, entry.layer, entry.overrode}
{[:meta, :name], "b", "mine", ["house"]}

fact(slot, field, key)

@spec fact(term(), term(), term()) :: Macaw.Frame.Fact.t() | {:error, [fault(), ...]}

A fact (§7.3): %Macaw.Frame.Fact{}, the attribute key of field in the schema bound to slot; {:error, faults} for arguments that are none.

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

flatten(fragment, fetch)

@spec flatten(term(), term()) ::
  {:ok, design(), %{missing: list(), keyless: list()}}
  | {:error, {:cycle, list()} | [fault(), ...]}

A composite flattened through fetch (§16.4): Macaw.Layers.Composite.flatten/3 with this type, {:ok, design, %{missing: [...], keyless: [...]}}, a missing reference masked at its site and reported, or {:error, {:cycle, chain}}. A fragment that is no composite flattens to itself, canonical.

format(fault)

@spec format(term()) :: String.t()

One fault as a line a person reads, the root named design (§10.1).

iex> Macaw.Frame.Document.format({[:layers, 0, :kind], :required})
"layers[0].kind: is required"

free_vars(fragment)

@spec free_vars(term()) :: [map()]

The variables a fragment or a stack still demands (§15.2), each with its uses, its parameter declaration and the types expected where it stands.

holes(kind, key, value)

@spec holes(term(), term(), term()) :: [Macaw.Layers.Var.t()]

The variables a value of the type's own syntax holds (§7.4, §7.5): a text's variable holes, and the variables a choice holds (the one it is on and those of its cases), at the key the text or the choice stands at; [] elsewhere.

identity(kind, arg2)

@spec identity(term(), term()) :: term()

The identity of an element of a :concat list (§8.3): a layer's and a label's id, an axis's {scale, side}, and nil for anything else, which appends.

inline(design, fetch)

@spec inline(term(), term()) :: {map(), [{term(), list()}]}

The ids left at reference sites resolved through the fetch (§8.4, D-103): a style inlined, a scale or slot declared under a synthesized key, a missing one reported.

key(kind, key)

@spec key(term(), term()) :: Macaw.Layers.Fragment.spec() | nil

A key's merge rule in the kernel's terms (§1.5): its rule, the node kind of a node-valued key, and the node kind of a list's or a name map's members; nil for a key the kind does not have. budget is a kind of its own at the design (§16.1).

iex> Macaw.Frame.Document.key(:design, :layers)
%{merge: :concat, node: nil, element: :layer}
iex> Macaw.Frame.Document.key(:style, :fill)
%{merge: {:type, :style}, node: :paint, element: nil}

keys()

@spec keys() :: Macaw.Base.Vocabulary.table()

The keys a fragment's map form reads as atoms (§11.1).

kinds()

@spec kinds() :: [atom(), ...]

The kinds of a fragment (§16.1), :design first.

merge(arg1, lower, higher)

@spec merge(term(), term(), term()) :: term()

Two values of a style's part laid by the merge rule (§3.5), for the {:type, :style} rule: key by key, or the later whole but for what no type decides when the two name different types (a paint's opacity, a stroke's width, dash, join, cap, placement).

name_map?(path)

@spec name_map?(term()) :: boolean()

Whether the map at path is keyed by names (§11.1).

iex> Macaw.Frame.Document.name_map?([:frames, "main", :scales])
true
iex> Macaw.Frame.Document.name_map?([:frames, "main"])
false

normalize(kind, k, lower, higher)

@spec normalize(term(), term(), term(), term()) :: {term(), term()}

A style part written as a shorthand on one side and as a part on the other, both as the part (§3.1), so the two merge key by key (§8.4).

place(kind, contribution, key)

@spec place(term(), term(), term()) :: {:ok, map()} | :keyless

Where a site's contribution of kind lands in a design (§16.4): a layer or a label appended to its list, an axis to the frame "main"'s axes and a scale under its key there, a name-keyed kind under the site's key (:keyless without one), a frame merged as a node, the rest under its design key.

place_whole(contribution, key)

@spec place_whole(term(), term()) :: {:ok, map()}

A keyed whole-design contribution placed under its key (§8.4, §16.4): its frames as the key, its layers and labels retargeted, their ids prefixed by the key.

read(input, opts \\ [])

@spec read(term(), term()) :: {:ok, design()} | {:error, [fault(), ...]}

A design from elsewhere, its JSON text or its map form, read through the kernel's gates and judged, then canonical (§9.2, §11.1, §13.1). Options: limits: (the kernel's bytes, depth and items and this type's ticks, iterations and counts), urls: (the host's Macaw.Layers.Url.Policy, closed when not given), profile:, vocabulary:, registry: and generators:, as check/2 takes them.

iex> Macaw.Frame.Document.read(~s({"frames": {"main": {"kind": "cartesian"}}}))
{:ok, %{frames: %{"main" => %{kind: "cartesian"}}}}
iex> Macaw.Frame.Document.read(~s({"$version": 2, "frames": {}}))
{:error, [{[], {:newer_schema, 2, 1}}]}

root()

@spec root() :: :design

The root kind: :design (§1.1).

scope(composite, contribution, key)

@spec scope(term(), term(), term()) :: term()

A site's contribution with its composite's defaults under what it places, the composite's farther and each node's nearer (§8.4, D-174).

stack(layers)

@spec stack(term()) :: fragment() | {:error, [fault(), ...]}

The cascade of fragments meant to overlap (§8.1): Macaw.Layers.Fragment.stack/2 with this type, the higher layer's word winning; a layer may be named, {name, fragment}. A term that is no list of fragments answers {:error, faults}.

step(document, from)

@spec step(term(), term()) :: {:ok, map()} | {:error, [fault(), ...]}

The step from one version to the next (§9.1); the line has none yet.

tags()

@spec tags() :: module()

The type's tag table (§11.2), Macaw.Frame.Document.Tags.

under(kind, lower, higher)

@spec under(term(), term(), term()) :: term()

What of a lower part a higher one keeps (§3.5, §8.4): a part whose type changes keeps only what no type decides.

validate(value)

@spec validate(term()) :: :ok | {:error, [fault(), ...]}

The type's validate/1 (§10): :ok, or every fault of the value by path, under the default profile and with no registry. check/2 takes the reader's options.

validate(fragment, kind)

@spec validate(term(), term()) :: :ok | {:error, [fault(), ...]}

Per-kind validity (§16.1): a fragment of kind validated alone, needing no key and looking up no reference. kind is one of kinds/0.

iex> Macaw.Frame.Document.validate(%{style: %{extends: "nowhere"}}, :style)
:ok

var(name)

@spec var(term()) :: Macaw.Layers.Var.t() | {:error, [fault(), ...]}

A variable (§7.1): %Macaw.Layers.Var{name: name}, its name a string. A name that is no name answers {:error, faults}.

iex> Macaw.Frame.Document.var(:unit)
%Macaw.Layers.Var{name: "unit"}

wrapper(key)

@spec wrapper(term()) :: atom() | nil

The kind a singular wrapper key names (§16.1), or nil.

iex> Macaw.Frame.Document.wrapper(:layer)
:layer
iex> Macaw.Frame.Document.wrapper(:frames)
nil