Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.26 KB

README.md

File metadata and controls

51 lines (37 loc) · 1.26 KB

Strucord

Allows for easy interop with Erlang records.

By using it in a module, it autogenerates a struct for the given record, along with functions for converting between the struct and the record.

I primarily use this for calling Gleam code from Elixir.

Installation

def deps do
  [
    {:strucord, "~> 0.1.0"}
  ]
end

Example

Using the module will define three functions, with_record/2, from_record/1, and to_record/1.

with_record/2 takes a struct as argument, and a function that takes a record as argument. It will convert the struct to a record, pass it to the function, and then convert the result of the function back to a struct.

from_record/1 takes a record as argument, and returns a struct.

to_record/1 takes a struct as argument, and returns a record.

defmodule Emulator do
  use Strucord,
    name: :emulator,
    from: "gen/src/chip8@emulator_Emulator.hrl"

  def init() do
    from_record(:chip8@emulator.init())
  end

  def reset(%Emulator{} = emulator) do
    with_record(emulator, fn record ->
      :chip8@emulator.reset(record)
    end)
  end

  def load_rom(%Emulator{} = emulator, rom) when is_binary(rom) do
    with_record(emulator, fn record ->
      :chip8@emulator.load_rom(record, rom)
    end)
  end
end