SCRC v0.5.1 Scrc.Mapper View Source

Mapper implements a custom DSL to map an SCRC Binary string to typed native fields and vice versa.

defmodule Scrc.InitData do
  use Scrc.Mapper

  defstruct name: "SCR",
            angles: [0, 0]

  prefix :name, :string
  field "init", :angles, [:float]
end
iex> data = Scrc.InitData.from_binary("SCR(init 0 2)")
%Scrc.InitData{angles: [0.0, 2.0], name: "SCR"}

iex> Scrc.InitData.to_binary(data)
"SCR(init 0.0 2.0)"

Link to this section Summary

Functions

Declare a field with name (external and internal, type and options

Declare a prefix field with name internal and type (String in most cases)

Link to this section Types

Link to this type option() View Source
option() ::
  {:clip, {number(), number()}} | {:clip, [number()]} | {:default, number()}
Link to this type options() View Source
options() :: [option()]
Link to this type payload_field() View Source
payload_field() :: {String.t(), atom(), type(), options()}
Link to this type payload_fields() View Source
payload_fields() :: [payload_field()]
Link to this type type() View Source
type() :: :float | :integer | [:float] | [:integer]

Link to this section Functions

Link to this macro field(name, key, type, opts \\ []) View Source (macro)
field(String.t(), atom(), type(), options()) :: any()

Declare a field with name (external and internal, type and options.

  • name - Field specifier how it appears in the binary
  • key - Field specifier to use in the struct
  • type - Can be :float or :integer. Also allows lists using [:float] and [:integer]
  • opts - Allows to specifiy clipping and default values

    • default: value - Specify default value
    • clip: {min, max} - Clips the number to min-max
    • clip: [val1, val2, ..., valn] - Clips the number to one of the values - nil if not in the list
Link to this macro prefix(key, type, opts \\ []) View Source (macro)
prefix(atom(), :string, any()) :: any()

Declare a prefix field with name internal and type (String in most cases).

  • key - Field specifier to use in the struct
  • type - Ignored