Macaw.Layers.Vocabulary (macaw_layers v0.2.2)

Copy Markdown View Source

A closed vocabulary read from a string (spec/01 §2).

A table maps each member's spelling to the member, built from the vocabulary's own list. A reader looks a document's string up in it and never makes an atom of one. Nor does it ask the node whether such an atom exists (String.to_existing_atom/1), which would let a document probe the atom table and make a read depend on which modules are loaded. A string the table lacks is the caller's to refuse at its path.

Build a table once, in a module attribute, wherever the list is known at compile time:

@anchors Vocabulary.table([:title, :subtitle, :caption])
Vocabulary.fetch(@anchors, "title")   #=> {:ok, :title}
Vocabulary.fetch(@anchors, "footer")  #=> :error

Ported from visualize's Visualize.Vocabulary.

Summary

Types

A vocabulary's members by their spelling.

Functions

The member a string spells, from a table or, for a list known only at run time and a few members long, from the list itself. Anything that is not a string is :error.

The member a string spells, or the string itself: a value a validator then refuses by type at its path.

The table of a list of atoms.

Types

table()

@type table() :: %{required(String.t()) => atom()}

A vocabulary's members by their spelling.

Functions

fetch(table, string)

@spec fetch(table() | [atom()], term()) :: {:ok, atom()} | :error

The member a string spells, from a table or, for a list known only at run time and a few members long, from the list itself. Anything that is not a string is :error.

read(vocabulary, string)

@spec read(table() | [atom()], String.t()) :: atom() | String.t()

The member a string spells, or the string itself: a value a validator then refuses by type at its path.

table(members)

@spec table([atom()]) :: table()

The table of a list of atoms.