A fault, {path, reason}, and the one formatter that words it (spec/01 §3).
The path runs from the value's root: atoms or strings for keys, non-negative integers for
list positions. A key the type does not know stays at its own spelling, a string. Every
fault is reported, never just the first, ordered by path (sort/1).
format/2 words a fault as path: words, and sentence/2 an {:error, reason}'s term
as one sentence. The words come from the caller first: each function given as
words: in turn, answering words or nil for a reason it doesn't word; then base's
general words (general/1); then the reason as its term. So a package words its own
reasons, and may word a general one in its own terms:
iex> Macaw.Base.Fault.format({[:frames, "main", "wdth"], :unknown_key})
"frames.main.wdth: is not a key of this node"
iex> Macaw.Base.Fault.format({[], {:newer_schema, 4, 3}}, root: "design")
"design: needs schema v4, this is v3"
iex> Macaw.Base.Fault.format({[:x], :unknown_key}, words: &if(&1 == :unknown_key, do: "is not a key it takes"))
"x: is not a key it takes"The formatter is visualize's Visualize.Chart.Validator.format/1 and the general part of
its wording, by way of Macaw.Layers.Fault, with Macaw.Data.Fault's sentence and its
order of words merged in.
Summary
Types
format/2's and sentence/2's options.
Where a fault is: keys and list positions from the value's root.
A fault: where, and why.
A package's words for a reason, nil for a reason it doesn't word.
Functions
A fault as path: words.
A path as a person reads it: keys joined by ., an index as [i], and the empty path
as the root's name.
The general words for a reason every package may answer, nil for any other: a
package's own reasons are its own to word, given as words:.
An {:error, reason}'s term as one sentence: each fault of a list as path: words,
joined by "; ", one fault the same way, or a bare reason's words. The empty path is
named "value" unless root: says otherwise; words: is as format/2's.
Faults ordered by path, keeping the given order of faults at one path.
A type's words as {:type, type} reads them after "must be": base's general types, and
the compounds of any type, each part worded by base or by leaf, a package's words for
its own leaf types (nil for one it doesn't word). nil when a part has no words, so
the whole reason falls to the package.
Types
format/2's and sentence/2's options.
@type path() :: [atom() | String.t() | non_neg_integer()]
Where a fault is: keys and list positions from the value's root.
A fault: where, and why.
A package's words for a reason, nil for a reason it doesn't word.
Functions
A fault as path: words.
Options:
A path as a person reads it: keys joined by ., an index as [i], and the empty path
as the root's name.
iex> Macaw.Base.Fault.format_path([:marks, 0, :channels])
"marks[0].channels"
iex> Macaw.Base.Fault.format_path([])
"document"A module in a path reads as Elixir writes it, and an element that is no name or index is shown as its term:
iex> Macaw.Base.Fault.format_path([:readers, String])
"readers.String"
iex> Macaw.Base.Fault.format({[[nil], %{}], :unknown_key})
"[nil].%{}: is not a key of this node"
The general words for a reason every package may answer, nil for any other: a
package's own reasons are its own to word, given as words:.
iex> Macaw.Base.Fault.general({:enum, [:asc, :desc]})
"must be one of :asc, :desc"
iex> Macaw.Base.Fault.general({:limit, :bytes, 10, 12})
nil
An {:error, reason}'s term as one sentence: each fault of a list as path: words,
joined by "; ", one fault the same way, or a bare reason's words. The empty path is
named "value" unless root: says otherwise; words: is as format/2's.
iex> Macaw.Base.Fault.sentence([{[:temp], :required}, {[:at, 1], {:type, :integer}}])
"temp: is required; at[1]: must be an integer"
iex> Macaw.Base.Fault.sentence(:required)
"is required"
Faults ordered by path, keeping the given order of faults at one path.
A type's words as {:type, type} reads them after "must be": base's general types, and
the compounds of any type, each part worded by base or by leaf, a package's words for
its own leaf types (nil for one it doesn't word). nil when a part has no words, so
the whole reason falls to the package.
iex> Macaw.Base.Fault.type_words({:list, :pos_integer})
"a list of a positive integer"
iex> Macaw.Base.Fault.type_words({:list, :color}, &if(&1 == :color, do: "a color"))
"a list of a color"
iex> Macaw.Base.Fault.type_words({:list, :color})
nil
A reason's words: the caller's words: in turn, then general/1, then the reason shown
with inspect/1. Never fails, whatever the reason.