Skip to content

QuinnWilton/strucord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Allows for easy interop with Erlang records.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages