ArtNet.Packet.EnumTable (ArtNet v0.1.0)

View Source

DSL for defining integer-backed enum tables.

ArtNet.Packet.EnumTable maps atom values used by packet structs to the integer codes used on the wire. Packet schemas use enum table modules with the {:enum_table, Module} field format. Bit-field schemas may also use enum tables with the same format.

defmodule ArtNet.Packet.EnumTable.Priority do
  use ArtNet.Packet.EnumTable

  defenumtable([bit_size: 8],
    dp_all: 0x00,
    dp_low: 0x40,
    dp_med: 0x80,
    dp_high: 0xC0
  )
end

A generated enum table module receives:

  • bit_size/0 - returns the declared size in bits.
  • one zero-arity function per enum key, returning that key's integer code.
  • to_code/1 - converts an atom key to {:ok, integer} or :error.
  • to_atom/1 - converts an integer code to {:ok, atom} or :error.
  • @type type - union type of the declared atom keys.

At compile time, each integer code is checked against :bit_size. Values must be integers in 0..(2 ** bit_size - 1).

Summary

Functions

Defines an enum table.

Functions

defenumtable(opts, table)

(macro)

Defines an enum table.

The first argument is an option list and must include :bit_size. The second argument is a keyword list mapping atom keys to integer codes. Entries may also use {code, opts} with a :description option for generated docs.

The generated module documents bit_size/0, each zero-arity enum key function, to_code/1, and to_atom/1.

defenumtable([bit_size: 2],
  disabled: {0, description: "Disabled state."},
  input: {1, description: "Input state."},
  output: {2, description: "Output state."}
)