Skuld.Effects.Port.EffectfulContract (skuld v0.23.0)

View Source

Generates an effectful behaviour from a HexPort.Contract.

use Skuld.Effects.Port.EffectfulContract reads the operations from a plain HexPort contract and generates:

  • Effectful @callback declarations with computation(return_type) return types on the using module
  • __port_operations__/0 — copied from the HexPort contract
  • __port_effectful__?/0 — marker for effectful resolver auto-detection

The effectful contract module is the effectful behaviour. Effectful implementations declare @behaviour MyApp.Todos.Effectful.

Usage

# Plain contract (HexPort)
defmodule MyApp.Todos.Contract do
  use HexPort.Contract

  defport get_todo(id :: String.t()) :: {:ok, Todo.t()} | {:error, term()}
end

# Effectful contract (Skuld)
defmodule MyApp.Todos.Effectful do
  use Skuld.Effects.Port.EffectfulContract,
    hex_port_contract: MyApp.Todos.Contract
end

# Effectful facade
defmodule MyApp.Todos do
  use Skuld.Effects.Port.Facade, contract: MyApp.Todos.Effectful
end

Effectful Implementation

defmodule MyApp.Todos.EffectfulImpl do
  @behaviour MyApp.Todos.Effectful

  def get_todo(id) do
    Comp.pure({:ok, %Todo{id: id}})
  end
end

Options

  • :hex_port_contract (required) — the HexPort contract module that defines __port_operations__/0 via use HexPort.Contract.