Enuma (Enuma v0.1.0)

View Source

Enuma is a library for defining and working with Rust like Enums in Elixir.

Example

defmodule MyEnum do
  use Enuma

  defenum do
    item :foo
    item :bar, args: [integer()]
    item :baz, args: [String.t()]
  end
end

Enuma will create macros for each item, allowing you to match on the enum values and check if a value is of a specific type.

iex> require MyEnum
MyEnum

iex> MyEnum.foo() = :foo
true

iex> MyEnum.bar(x) = {:bar, 1}
{:bar, 1}

It will also generate macros for checking if a value is of a specific type. (e.g MyEnum.is_foo(value)). These macros can be used in pattern matching and guards.

Summary

Functions

defenum(list)

(macro)

Defines an enum with the given items.

from_string(value, type_module)

from_string!(value, type_module)

is_valid(value, type_module)

(macro)

item(name, opts \\ [])

(macro)

Defines an item with the given name and options.

Options

  • :args - a list of arguments to be passed to the item macro

to_string(key, type_module)

valid?(value, type_module)