RustlerMatchSpec
Erlang-style match specifications for Rustler NIFs.
RustlerMatchSpec provides a small Elixir DSL and a Rust evaluator for selecting
and projecting compact native events without exposing large native data
structures to the BEAM.
Usage
import RustlerMatchSpec
spec =
match_spec do
{:event, kind, value} when kind in [:import, :url] ->
{kind, value}
end
RustlerMatchSpec.select([
{:event, :import, "./app.js"},
{:event, :env, "MODE"},
{:event, :url, "./font.woff2"}
], spec)
#=> [{:import, "./app.js"}, {:url, "./font.woff2"}]
For repeated use, compile once:
selector = RustlerMatchSpec.compile(spec)
RustlerMatchSpec.select(events, selector)
The macro compiles to plain match-spec-shaped data:
[{match_head, match_guards, match_body}]
Native integration
Rust NIF crates can implement MatchEvent for their own event types and reuse
the same selector evaluator. This lets a NIF match tuple-shaped patterns such as
{:event, kind, value} without allocating whole intermediate event tuples.
Use cases include parser events, trace events, resource metadata, or any native stream where Elixir should decide which compact facts to keep.
Development
mix deps.get
mix ci