Exclosured.Events (exclosured v0.1.4)

Copy Markdown

Generate Elixir structs from annotated Rust structs.

Annotate a Rust struct with /// exclosured:event and this module generates a corresponding Elixir struct with defstruct, type specs, and a from_payload/1 helper for converting JSON maps.

Usage

defmodule MyApp.Events do
  use Exclosured.Events, source: "native/wasm/my_mod/src/lib.rs"
end

Rust Side

/// exclosured:event
pub struct ProgressEvent {
    pub percent: u32,
    pub stage: String,
}

Generated Elixir

defmodule MyApp.Events.ProgressEvent do
  defstruct [:percent, :stage]

  @type t :: %__MODULE__{
    percent: integer(),
    stage: String.t()
  }

  def from_payload(%{"percent" => percent, "stage" => stage}) do
    %__MODULE__{percent: percent, stage: stage}
  end
end

Supported Rust Types

  • u8, u16, u32, u64, i8, i16, i32, i64, usize, isize -> integer()
  • f32, f64 -> float()
  • String, &str -> String.t()
  • bool -> boolean()
  • Vec<T> -> list(T)
  • Option<T> -> T | nil